Link to home
Start Free TrialLog in
Avatar of SiJP
SiJP

asked on

Context Menu's in Word, with VBA

Getting some serious brain ache over this ;)

(OS=XP Pro sp2, Word=11.0 sp1)

My set up is as follows.  I've a document template (myForm.Dot) which
contains code to add items to the Context menu's.

The code for each item is as follows:

        Set cb_ctl =
CommandBars(ShortCutMenu(i)).Controls.Add(msoControlButton)
        With cb_ctl
            .Caption = "Use ET" 'Changes for each button
            .OnAction = "=fUseET()" 'Changes for each button
        End With

When I open a new document (based on the template) the menu items are added
to the context menu (yay!)... however, when I click on each item I'm not as
lucky.

I receive the message (and apologies in advance for its length):

------------------------------ SNIP ----------------------------------------
You may encounter this error for the following reasons:
Macro security is set to:
Very High and the application encounters a signed macro, but the macro was
automatically disabled. Use the following procedure to enable the macro:
Select the Tools menu option and then select Macro and Security. In the
resulting Security dialog, set the security level to High by clicking the
High radio button.
Close the file and any other instances of the application currently running
on the computer (close all applications that also use the application you are
currently running).
Open the file again and examine the certificate of trust details and set the
Always trust macros from this publisher box if you trust the certificate
issued by the publisher.
Click the Enable button to allow the macro to run.
High and the application encounters a signed macro, but you selected Disable
when prompted by the macro warning dialog when opening the file. Use the
following procedure to enable the macro:
Close the file and any other instances of the application currently running
on the computer (close all applications that also use the application you are
currently running).
Open the file again and examine the certificate of trust details and set the
Always trust macros from this publisher box if you trust the certificate
issued by the publisher.
Click the Enable button to allow the macro to run.
High or Very High and the macro was not signed and was automatically
disabled by the application. Use the following procedure to enable the macro,
if you have verified you can trust the source of the unsigned macro:
Select the Tools menu option and then select Macro and Security. In the
resulting Security dialog, set the security level to Medium by clicking the
Medium radio button.
Close the file and any other instances of the application currently running
on the computer.
Open the file again and click the Enable button when prompted to allow for
the unsigned macro to run.
When you have accepted the macro, return the macro security level to its
previous setting.
Another instance of your application has a lock on the security settings and
is disallowing any changes to security settings.
It is recommended to close all Office related applications or other
instances of the same application when you attempt to make changes to your
security settings.
Visual Basic for Applications (VBA) is not installed on your computer.
If VBA is not installed, it is not possible to accept certificates of trust
attached to VBA macros. Therefore, any VBA macro attempting to run, even with
an attached certificate of trust, cannot be run because the necessary macro
interpreter is not installed on the system. Two scenarios can cause this
error:
Your administrator chose not to install VBA
The version of Office you are running does not install VBA by default
If you are running an instance of Office that does not provide VBA as an
installable feature, you will need to upgrade your version of Office to run
VBA macros.
The certificate of trust is invalid - (when this occurs, you cannot select
the Enable button).
The certificate is no longer trusted, was revoked by the issuing authority,
or is damaged. There are several other possibilities which you may need to
explore as to why the certificate is no longer recognized as valid by your
system. It is recommended that you not trust or run a macro that has an
invalid certificate.
A troubleshooter help topic is available online regarding this issue.
------------------------------ SNIP ----------------------------------------

After revewing the items in the list, I conclude that none apply to my set
up (Macro settings are medium, VBA obviously installed, certificates are
valid etc).

The Cruncher... where've I gone wrong!

Also, just for kicks... how the heck do I add in images to these buttons
through VBA? I cant seem to find any properties for this...  As an
alternative I've seen the PasteFace function that'll use the clipboard
contents, but I've no idea how to get my icon file onto the clipboard,
programatically.

****************************************
Laugh, and the world laughs with you.  Trip over in the middle of a busy
shopping centre whilst briefly holding a thick vaniilla shake, and watch the
world have hysterics at your expense..
Avatar of Steiner
Steiner

What about the = in front of the OnAction-Parameter? You have to give it a methode name there, and at least in Word 97 = are prohibited.
ASKER CERTIFIED SOLUTION
Avatar of Steiner
Steiner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SiJP

ASKER

Daniel,

Your first suggestion of removing the = made me think of testing this without the parenthasis as well.. which worked!  phew....

I see what you are trying to do with the shape thing, but this is causing me errors.

When openeing a new doc (after implementing the code, albiet adjusted for my command bars), I receive two errors.

1) There is insufficient Memory.  Save the document Now.

"This error appears if there is a problem saving the file.  Usually, the disk is full, RAM memory is low, or there is a permission problem with the drive the file is being saved to.  If the amount of disk space for a paging file is low, save the file to another drive. If the RAM memory is low, increase available RAM.  If permissions to the drive do not allow you to save to that drive, save the file to another drive or request permissions from the administrator to save files to the drive.  Note: This error can occur if the computer is running a version of antivirus software that is not compatible with Office or needs to be updated. This error can also occur if you are saving to a full floppy disk or the floppy disk is too small for the file."

2) Method of 'AddPicture' of object 'Shapes' Failed. (-2147467259)

Any idea's would be appreciated!
The parenthesis, ok just forgot that, glad you could solve that yourself.

Does you new doc base on a huge template? I guess not...

Maybe we can do this step by step. Try adding the picture to your document manually, see if at least that works. If it does, try selection that picture and starting the code beginning with Selection.CopyAsPicture. Maybe we can verify if a least this part works before trying to import the picture.

Maybe it's even an option to keep the picture inside the document instead of a separate bmp? If you set the shape as visible = msoFalse you can hide it. Just don't forget to set visible to msoTrue before  trying to add it to the commandbar-button (at least my Word crashed when copying a hidden picture to the clipboard and the trying to use it as face for the button).

Daniel
(going home now, more of that tomorrow, maybe someone else comes up with a better solution for this one until then...)
Avatar of SiJP

ASKER

Worked out that it was the format of the picture that was screwed.. your code worked fine!

Thanks for your help Daniel.. sorted my problem out a treat :D
Glad it worked and thanks for the grade.
And the part with the picture was quite an interesting problem, never thought of that one before ;-)