CommandBarComboBox error in VBA/Word on Mac

K

Kent Tessman

I'm having difficulty programmatically creating a toolbar with combobox
controls on it. It works fine on various combinations of Word and Windows,
but on OS X with either Word v.X or Word 2004, it crashes.

Here's a distillation of the code that causes the problem:


Public style_combo As CommandBarComboBox

Sub CreateMyFormattingToolbar()
Dim newbar As CommandBar
Set newbar = CommandBars.Add("My Formatting Toolbar")
newbar.Protection = msoBarNoCustomize
newbar.Position = msoBarTop

Set style_combo = newbar.Controls.Add(msoControlComboBox)
With style_combo
.Width = 120
.AddItem "Normal"
.AddItem "Title"
.OnAction = "StyleComboChanged"
End With

newbar.Visible = True
End Sub


When run from within the application, Word tells me:

"Method 'Width' of object 'CommandBarComboBox' failed"

and

"Method 'OnAction' of object 'CommandBarComboBox' failed" (if I comment out
the .Width line).

Run from the immediate window, the error is: "Automation Error"

Now, leaving aside the fact that Width and OnAction are properties, not
methods, the properties can be read, but not written. Without this working,
I'm at a loss as to how to create a combobox of a given width, or assign an
OnAction for it. Also, later, I need to check ListCount, and I get the same
crash. The AddItem method seems to work fine, however.

Again, all of this works fine in VBA/Word on Windows.
 
S

Steve Rindsberg

I'm having difficulty programmatically creating a toolbar with combobox
controls on it. It works fine on various combinations of Word and Windows,
but on OS X with either Word v.X or Word 2004, it crashes.

Don't set newbar.Protection = msoBarNoCustomize until *after* you've customized
it or PPT Mac barks at you. In a way, it makes more sense than the behavior
under PPT/Win which lets you declare the bar uncustomizable then proceed to
customize it.
 
K

Kent Tessman

Steve Rindsberg said:
Don't set newbar.Protection = msoBarNoCustomize until *after* you've
customized
it or PPT Mac barks at you. In a way, it makes more sense than the
behavior
under PPT/Win which lets you declare the bar uncustomizable then proceed
to
customize it.

Ah, that seems in retrospect like something I should've thought to try.
Thank you very much.

I'm still, however, having difficulty with:

"Method 'ListCount' of object 'CommandBarComboBox' failed"

in a procedure called from my own dialog.

Where style_combo is a public CommandBarComboBox in my code, in my dialog's
UserForm_Initialize() sub, style_combo.ListCount reports back the list count
just fine, but right afterward in the same dialog's AddButton_Click() sub,
although style_combo.AddItem() properly adds a new item to the combobox,
style_combo.ListCount fails with the above error.

I'm confused as to how style_combo.AddItem() can work while
style_combo.ListCount can fail.

Any ideas or suggestions on where I should be looking to track this down?
 
S

Steve Rindsberg

I'm still, however, having difficulty with:
"Method 'ListCount' of object 'CommandBarComboBox' failed"

in a procedure called from my own dialog.

Where style_combo is a public CommandBarComboBox in my code, in my dialog's
UserForm_Initialize() sub, style_combo.ListCount reports back the list count
just fine, but right afterward in the same dialog's AddButton_Click() sub,
although style_combo.AddItem() properly adds a new item to the combobox,
style_combo.ListCount fails with the above error.

I'm confused as to how style_combo.AddItem() can work while
style_combo.ListCount can fail.

Any ideas or suggestions on where I should be looking to track this down?

Can you replicate this in a simple sub?

For instance, once I've used essentially your same code (w/o invoking
protection or positioning the toolbar), I can:

Sub HowMany()
MsgBox Application.Commandbars("name").Controls(1).ListCount
End Sub

It doesn't seem to matter whether the toolbar is protected against
customization or not - I tried it both ways.
 
K

Kent Tessman

Steve Rindsberg said:
Can you replicate this in a simple sub?

This is the smallest example that I can come up with that causes the crash
on a Mac:


Public style_combo As CommandBarComboBox

Sub CreateMyFormattingToolbar()
Dim newbar As CommandBar

Application.CustomizationContext = ActiveDocument
Set newbar = CommandBars.Add("My Formatting Toolbar")

'Add Styles
Set style_combo = newbar.Controls.Add(msoControlComboBox)

newbar.Position = msoBarTop
newbar.Visible = True
End Sub

Sub TestDialog()
UserForm1.Show
EndSub

(and then create a simple dialog UserForm1 with a single button with the
following code:)

Private Sub CommandButton1_Click()
Debug.Print style_combo.Parent.Name ' print "My Formatting Toolbar"
Debug.Print style_combo.ListCount
End Sub


Then, go back to the document, and assign a shortcut like Ctrl+T to
Testdialog. Manually run CreateMyFormattingToolbar to set up everything.
Then Ctrl+T from the document.

This gives me the crash on VBA/Word on Mac _after_ correctly printing "My
Formatting Toolbar" to the immediate window.

Now, the other thing I notice is that if all of this is in a template as
opposed to a document, it doesn't seem to be saving the toolbar with the
document (despite the CustomizationContext). That is, if I AddItem() a
bunch of items to the combo box and save/close the document, when I reopen
it the items aren't there. I'm not sure if these are related or if that's a
whole other issue. The proper(?) behavior happens in Word for Windows.
 
S

Steve Rindsberg

Strange. I don't work much with Word, much less Word on a Mac, but I've tried
your code on Word X -- did everything but assigning a shortcut key (couldn't
get that to behave).

But when I invoke the dialog via Tools, Macro or manually from w/in the IDE, I
get no errors.
 
K

Kent Tessman

Steve Rindsberg said:
Strange. I don't work much with Word, much less Word on a Mac, but I've
tried
your code on Word X -- did everything but assigning a shortcut key
(couldn't
get that to behave).

But when I invoke the dialog via Tools, Macro or manually from w/in the
IDE, I
get no errors.

I guess the shortcut key isn't necessary, since (for me) invoking the dialog
via Tools > Macro and running TestDialog nets the same error for the
'ListCount' method on 'CommandBarComboBox'.

Thanks for going through the trouble of plugging this together and running
it.

But that you're not seeing this crash, too, is puzzling.
 
S

Steve Rindsberg

Thanks for going through the trouble of plugging this together and running
it.

I can't honestly say "My pleasure" because working in VBA on the mac makes me
nuts. ;-) But it's good for my character.
But that you're not seeing this crash, too, is puzzling.

You might bring this up in one of the Public.Mac.Office word groups.
There are some very smart folks there who can surely help.

You might post a copy of your Word file with code included on a web site and
include a link to it (or offer to email it) to save folks a little time.
 
K

Kent Tessman

Steve Rindsberg said:
I can't honestly say "My pleasure" because working in VBA on the mac makes
me
nuts. ;-) But it's good for my character.

Believe me, I hear you.
You might bring this up in one of the Public.Mac.Office word groups.
There are some very smart folks there who can surely help.

You might post a copy of your Word file with code included on a web site
and
include a link to it (or offer to email it) to save folks a little time.

Thanks for the suggestion. I'll put the crashing (for me) version up at
http://www.generalcoffee.com/temp/macrotest.dot and see if the good people
at public.mac.office.word have any insight on it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top