how to use late binding for commandbars

Z

zbud

How do refer to the commandbars in Acc 2003 using late binding.

I was using somethin like this but got the 429 error, using late
binding as not sure all users have the same version of Office.

Function AddMenuItem()

Dim myObj As Object
Dim myMenuBar As Object
Dim ctrl1 As Object

Set myObj = CreateObject("Office.Commandbar")

Set myMenuBar = myObj.CommandBars.ActiveMenuBar
Set newmenu = myMenuBar.Controls.Add(Type:=msoControlPopup,
Temporary:=True)
newmenu.Caption = "OLF-Custom"
Set ctrl1 = newmenu.Controls.Add(Type:=msoControlButton, ID:=1)
ctrl1.Caption = "Make PDF"
ctrl1.TooltipText = "Make PDF in c:\mydocuments"
ctrl1.Style = msoButtonCaption
ctrl1.OnAction = "Repor_to_PDF"

Set ctrl1 = Nothing
Set myMenuBar = Nothing
Set myObj = Nothing

End Function

Is there a way to get a list of all available references on a machine
via vba code?

Bud
 
D

Douglas J. Steele

Which line is causing the 429 error?

Your code definitely isn't going to work even if you get past the 429,
though. You're referring to intrinsic constants msoControlPopup and
msControlButton, and those won't be defined without the reference being set.
(If it's the myMenuBar.Controls.Add line that's causing the error, what's
below might fix it)

You'll need to add the following:

Const msoControlPopup As Long = 10
Const msoControlButton As Long = 1

The fact that you're not getting errors that you're using undefined values
implies that you don't have Option Explicit in your code. You really should:
it can help detect a number of errors. To turn it on for all future modules,
select Tools | Options while in the VB Editor, and look at the Module tab.
Ensure that the "Require Variable Declaration" option is checked.
Unfortunately, you'll have to go and add the line manually to existing
modules.
 
Z

zbud

This is the line that causes the 429 problem
Set myObj = CreateObject("Office.Commandbar")

Thanks for the other tips.

Is there a way in code to generate a list of the available references
on a users machine ?

Bud
 
Z

zbud

This is the line that causes the 429 problem
Set myObj = CreateObject("Office.Commandbar")

Thanks for the other tips.

Is there a way in code to generate a list of the available references
on a users machine ?

Bud
 
D

Douglas J. Steele

Not really. You'd have to go through the registry, and to be honest, I'm not
sure what hives you'd need to examine.

Why do you need to create the command bar programmatically though? Can't you
just create it once, and distribute it with the application?
 
Z

zbud

Thanks for your help, I was concerned about the references if I had run
the function in the distriuted program, but you are correct I can just
permanenetly create the toolbar and not have to include the reference
or use late binding or even run the function on the user's machine.

Thanks

Bud
 

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