Macro syntax to insert an autotext with a macrobutton

R

Rob Benz

I need to insert an autotext entry via a macrobutton that invokes a macro.
The macrobutton runs the macro OK, but when I try to add syntax to make the
macro insert a properly-defined autotext entry, it always fails.
 
J

Jay Freedman

I need to insert an autotext entry via a macrobutton that invokes a macro.
The macrobutton runs the macro OK, but when I try to add syntax to make the
macro insert a properly-defined autotext entry, it always fails.

Your question doesn't include enough detail to know what the problem is.

Where do you want to insert the autotext? Replacing the macrobutton, or at some
other place? If the latter, how should the macro recognize that place?
(bookmark, table cell, etc.)

What do you mean by "fails"? Do you get an error message? If so, exactly what
does it say? If there is no message, what does happen?

Is the document protected for forms?

It would probably help to post the code you do have, so maybe it would become
clear why it's failing.
 
P

Pesach Shelnitz

Hi Rob,

I'm also not sure about what you are asking, but I'll try to answer based on
my understanding of your posting. Jay's question about where you want to
insert the AutoText needs an answer, but I'll assume that you want the
AutoText to replace the MACROBUTTON field. In that case, you can use the
following macro, which is called OK and inserts an Autotext entry named
"abc." If it is launched by a MACROBUTTON field in your document, it will
replace the MACROBUTTON field.

Sub OK()
NormalTemplate.AutotextEntries("abc").Insert Where:= _
Selection.Range, RichText:=True
Selection.Font.Reset
End Sub

In your document you need a MACROBUTTON field that looks like {MACROBUTTON
OK Click here} or {MACROBUTTON OK OK}.

In Word 2007, this macro will work only for AutoText entries saved in the
Normal template. For building blocks saved in Building Blocks, use
Templates(1).BuildingBlockEntries("abc").Insert with the same parameters.
 
R

Rob Benz

Pesach's post allowed me to get this working for a Word 2007 template - this
macro starts the user out with a blank word 2007 .doc that has a macrobutton
named "Add a new Definable Feature". In an Autotext entry named
DefinableFeature, I have a simple word table stored (2 columns, 4 rows.)

The desired action now works for Word 2007...each time the user presses the
macrobutton, a new copy of the table is inserted, which allows the user to
build a cumulative series of tables.

But Pesach's caution that the syntax has to be changed depending on the
version of Word, is an unexpected problem. I need to be able to distribute
this as a template to all users....whether they have Word 2003 or Word
2007...and be sure it will work for everyone. What's the best way to do
that, if you are not sure who has what version of Word?

Sub DefinableFeature()
Options.ButtonFieldClicks = 1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeBackspace
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MACROBUTTON DefinableFeature Feature", PreserveFormatting:=False
Selection.TypeParagraph
Selection.TypeParagraph
Templates(1).BuildingBlockEntries("DefinableFeature").Insert Where:= _
Selection.Range, RichText:=True
Selection.Font.Reset
Selection.Font.Color = wdColorWhite
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
 
R

Rob Benz

well, it almost works....each time I open the template, the macrobutton
produces the error, "Runtime Error 5941 - The requested member of the
collection does not exist."

But if I first manually invoke the autotext item, the macrobutton will work
OK after that....until I quit the template. Then the above cycle repeats.
 
J

Jay Freedman

Hi Rob,

Include this line before you try to access any AutoText entry:

Templates.LoadBuildingBlocks

Quoting the help for this method:

"Word normally loads building blocks when they are first needed, for example
when a user displays a gallery by using the Ribbon. This method forces Word
to immediately load all building blocks."

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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