Passing a variable from a Userform

R

Roderick O'Regan

I've got a userform with a few buttons each holding a thumbnail
picture.

The user clicks on the appropriate button as if to say "I want this
big picture placed in the document".

Each big picture in question is an AutoText entry named oFig1, oFig2,
and so on.

Below is the code I use in each button at the cmdButton1_Click:
ActiveDocument.AttachedTemplate.AutoTextEntries("oFig1").Insert _
Where:=Selection.Range, RichText:=True

However, the client now wants some major alterations. They still want
to use the same thumbnail selection in a UserForm but I now need to
put the code mentioned above in a Module procedure which does a lot of
other things.

In the code above is the name of the AutoText entry so if the user
pressed cmdButton1 in the userform which asked for oFig1 how, please,
can I pass this string to the Module procedure now holding:
ActiveDocument.AttachedTemplate.AutoTextEntries("XXXXX").Insert _
Where:=Selection.Range, RichText:=True
....and putting it where the XXXX is at present?

Presumably, I'd need a variable (e.g. pString) in the Module
procedure to hold the incoming string which in turn would be passed to
the code as follows:
ActiveDocument.AttachedTemplate.AutoTextEntries("pString").Insert_
Where:=Selection.Range, RichText:=True
....or am I trying to overcomplicate things?
 
J

Jay Freedman

You can declare the variable as a parameter of the procedure:

Sub SomeProcedure(pString As String)
...

and then use it in the Insert statement *without the quote marks*
(because placing quote marks there means it's a literal instead of a
variable name):

ActiveDocument.AttachedTemplate.AutoTextEntries(pString).Insert_
Where:=Selection.Range, RichText:=True

The module procedure has to be called from the procedure that runs on
the button click, in the form

SomeProcedure "oFig1"

--
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