adding macro to context menu

B

Bobert

a co-worker asked me if i could create an option in the right click-context
menu to paste as bold. i haven't worked with macros in a long time, but i
think it shouldn't be too hard to create a macro which pastes and then bolds,
but i have no idea how to add that macro to the context menu. any ideas on
how to do this, or how to add this option to the context menu without
creating the macro (which would be just as good)?
 
J

Jay Freedman

a co-worker asked me if i could create an option in the right click-context
menu to paste as bold. i haven't worked with macros in a long time, but i
think it shouldn't be too hard to create a macro which pastes and then bolds,
but i have no idea how to add that macro to the context menu. any ideas on
how to do this, or how to add this option to the context menu without
creating the macro (which would be just as good)?

The article
http://www.word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToToolbar.htm explains
how to put commands and macros onto menus. The second part of steps 4 and 7
deals specifically with the right-click context menus, also called shortcut
menus.

As for the macro, it isn't terribly difficult to write, but it certainly isn't
obvious how to do it. Part of the problem is that the clipboard doesn't
necessarily contain text, and bolding a picture doesn't make sense. Another part
is that with the seemingly obvious command Selection.Paste, Word immediately
loses track of where the pasted material starts. There is a way around these
obstacles, though.

First, in the VBA editor, go to Tools > References and set a reference to the
"Microsoft Forms 2.0 Object Library", which defines the DataObject type. Then
paste in this code:

Sub PasteAndBold()
Dim objData As DataObject
Dim strClip As String
Dim oRg As Range

On Error Resume Next

Set objData = New DataObject
objData.GetFromClipboard
strClip = objData.GetText

If Len(strClip) > 0 Then
Set oRg = Selection.Range
oRg.Text = strClip
oRg.Bold = True
End If
End Sub
 
B

Bobert

wow. thanks.

once i do that, how do i access the PasteAndBold subroutine? will it just
show up in the list of objects?
 
J

Jay Freedman

That's what "putting a macro on a shortcut menu" means -- there will be an item
in the menu (the article explains how to make that item say whatever you want it
to say), and clicking the menu item runs the macro.
 
B

Bobert

i meant once i create the listing in the context menu how do i connect it
with the macro. whatever, i'll figure it out once i get there.

but i actually got a chance to look at this on a machine with word and i
noticed that there is no "Microsoft Forms 2.0 Object Library" in the list of
references in the VB editor. i'm working with word 2003 (which is what my
coworker has). what is the equivalent, or what am i doing wrong?
 
J

Jay Freedman

Please look again for the Forms library, which is installed as part of
Office. Notice that the list in the References dialog isn't completely
alphabetic, so you may need to look at the entire list to find it. If the
library really isn't there:

- Use Windows Explorer to look for the file C:\Windows\System32\FM20.dll. If
it's there, go to Start > Run and enter the command

regsvr32 FM20.dll

After a few seconds you should see a message that FM20.dll was registered.
Then restart Word and look for the reference again.

- If the file FM20.dll is not on the computer, see
http://support.microsoft.com/kb/224305. The link to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaxctrl/html/cpad.asp
that is offered there is old and will automatically redirect you to
http://msdn2.microsoft.com/en-us/library/ms968493.aspx. At the latter page,
download the setuppad.exe file and run it; as a side effect, that will
install FM20.dll.
 
B

Bobert

FM20.dll was on the computer. i re-registered it, but still no "Microsoft
Forms 2.0 Object Library".

there is a "Microsoft Word 11.0 Object Library", a "Microsoft Office 11.0
Object Library", a "Microsoft ActiveX Data Objects 2.0 Library", a"Microsoft
HTML Object Library", a "Microsoft Remote Data Object 2.0", a "Scripting
Object Model", a "Windows Script Host Object Model", an a whole bun of others
that i don't think are relevant. did you mean one of those?
 
J

Jay Freedman

No, it specifically says "Microsoft Forms 2.0 Object Library".

When you click on a library in the list, regardless of whether it's checked, the
bottom part of the dialog tells you the file path and name (labeled "Location").
If that doesn't say C:\Windows\system32\FM20.dll (or whatever the path to that
file is), then that isn't the right library.

Since you reregistered the file, have you rebooted the computer? I'm not sure at
what point the list of available libraries is rebuilt, but rebooting should
force it to happen.
 

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