Solved

Context Menu's in Word, with VBA

Posted on 2004-10-19
6
870 Views
Last Modified: 2008-01-09
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..
0
Comment
Question by:SiJP
  • 4
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Steiner
Comment Utility
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.
0
 
LVL 11

Accepted Solution

by:
Steiner earned 250 total points
Comment Utility
About the pictures, the closes thing I could get was:
- have a bmp size 15 * 15 pixels ready
- since I could not import it directly, I first added the picture to the document, then transferred it into the clipboard and from there on the button:

Sub Pic()
   Dim shp As Shape
   Set shp = ActiveDocument.Shapes.AddPicture("e:\test\icon.bmp")
   shp.Select
   Selection.CopyAsPicture
   ActiveDocument.CommandBars("Pers").Controls(4).PasteFace
   shp.Delete
End Sub

I'd think there must be a better way, but I could not find it...

Daniel
0
 
LVL 3

Author Comment

by:SiJP
Comment Utility
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!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:Steiner
Comment Utility
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...)
0
 
LVL 3

Author Comment

by:SiJP
Comment Utility
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
0
 
LVL 11

Expert Comment

by:Steiner
Comment Utility
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 ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now