Macro Variable Question

Hi, I have a Macro in MS Word 2003 that gets a string value from the Windows Registry and puts that value into a String variable. I can output my variable to a message box, but I want to place the value into my document instead.

How do I get a variable from my Macro into my current document. -- Let's assume my variable is called: myStringValue and it contained the String "MS Word 2003".

I would like to have my document contain the variable String "MS Word 2003" in it.

Not sure how to do that...

Thank you for your help,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Add a textbox form field to the document. Go to that form field's properties, format the field, and give it a name such as txtAppName. The name is the important part for this to work.
In your macro code, add this line
AcitveDocument.FormFields("txtAppName").Result = myStringValue
Serge FournierAnalyst ProgrammerCommented:
you can also write your value directly without making an active tag

with the world object to write in the document

i used it in outlook to add a signature when outlook had word as editor

here is an example:

dont forget, its from outlook, might need a few modifications

Sub sign()

   '=== replace in word object editor
   Dim objDoc As Word.Document
   Dim objSel As Word.Selection
   Dim a As Variant
   'On Error Resume Next
   '=== get a Word.Selection from the open Outlook item

   Set objDoc = Application.ActiveInspector.WordEditor
   Set objSel = objDoc.Windows(1).Selection
   Call ini_dep
   '=== now do what you want with the Selection

'    .Name = "Times New Roman"
'    .Size = 10
'    .Bold = False
'    .Italic = False
'    .Underline = wdUnderlineNone
'    .UnderlineColor = wdColorAutomatic
'    .Strikethrough = False
'    .DoubleStrikeThrough = False
'    .Outline = False
'    .Emboss = False
'    .Shadow = False
'    .Hidden = False
'    .SmallCaps = False
'    .AllCaps = False
'    .Color = wdColorAutomatic
'    .Engrave = False
'    .Superscript = False
'    .Subscript = False
'    .Spacing = 0
'    .Scaling = 100
'    .Position = 0
'    .Kerning = 0
'    .Animation = wdAnimationNone

   objSel.Font.Name = "Arial"
   objSel.Font.Size = 10
   If usenam = "fournier.serge" Then
      a = "Serge Fournier, Prog./Analyst, département des TI" & vbCrLf
      objSel.TypeText a
      objSel.Font.Bold = True
      a = "STAS Inc." & vbCrLf
      objSel.TypeText a
      objSel.Font.Bold = False
      a = "999 ABC: & vbCrLf
      a = a & "Tél.: 111-111-111, ext. 1111" & vbCrLf
      a = a & "Fax 1: 111-111-1111, Fax 2: 111-111-1111" & vbCrLf
      a = a & "Adresse électronique(Email): mailto: abc@bca.zzz" & vbCrLf
      a = a & "MSNLIVE: msnim:add?" & vbCrLf & vbCrLf
      objSel.TypeText a
   End If

   objSel.Font.Name = "Arial"
   objSel.Font.Size = 7.5

   a = "Avis de confidentialité. La présente communication est confidentielle et transmise sous le sceau du secret professionnel. Si vous n'êtes pas le destinataire visé ou son mandataire chargé de lui transmettre cette communication, vous êtes par la présente avisé qu'il est expressément interdit d'en dévoiler la teneur, de la copier, de la distribuer ou de prendre quelque mesure fondée sur l'information qui y est contenue. Si vous avez reçu cette communication par erreur, veuillez nous en aviser immédiatement par téléphone à frais virés et nous retourner l'original par la poste à l'adresse indiquée plus haut." & vbCrLf & vbCrLf

   '=== fra
   objSel.LanguageID = wdFrenchCanadian
   objSel.TypeText a

   '=== eng
   objSel.LanguageID = wdEnglishUS
   a = "Confidential Information. This communication and any attachments are private and may contain legally privileged information. If you are not the authorized recipient, the copying or distribution of this communication or any attachments is prohibited and you must not read, print or act in reliance of this communication. If you have received this communication by mistake, we would appreciate if you could advise us collect call and mail the document at the address indicated above." & vbCrLf
   objSel.TypeText a

   objSel.LanguageID = wdFrenchCanadian
   Set objDoc = Nothing
   Set objSel = Nothing

Open in new window

Serge FournierAnalyst ProgrammerCommented:
oops all my hpone numbers and email are in there hehe

ah well... as long as i did not put any passwords ;)
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Chris BottomleySoftware Quality Lead EngineerCommented:

I have posted a request via the request attention button for you

The main problem is telling the code where to put the text.

'This will put it at a bookmark that you have previously inserted.

ActiveDocument.Bookmarks"MyBookmark").Range.Text = myStringValue

'This uses a built-in bookmark to place it at the end of the document.

ActiveDocument.Bookmarks("\EndOfDoc").Range.Text = myStringValue

'This will place it is a cell of a table

ActiveDocument.Tables(1).Cell(2,3).Range.Text = myStringValue

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_FulanoAuthor Commented:
Thank you all...this note will cover my comments to you all.

Lee555J5, I tried your way, but found that I could not make it work. Its probably a good way, but I had trouble with it.

WildBoy85, although I'm sure you way is a good way as well, it was a bit too much for me to follow. I got a little lost.

GrahamSkan's approach was simple and gave me what I needed. - I only have a coulpe of questions before I close the post.

GrahamSkan: once you have the Bookmark set where you want it, how do you move beyond it? The space bar just pushed it forward and doesn't step over it. Also, is there a way to see where the Bookmarks are set?

Thanks to all,
Mr_FulanoAuthor Commented:
GrahamSkan in addition to the questions above, I noticed that regardless where I put the Bookmark, my value always gets inserted at the end of my document. Is there a way to change that and have it insert into the body of the document where I've placing my Bookmark?

Chris BottomleySoftware Quality Lead EngineerCommented:

Have you replaced the bookmark name "\EndOfDoc" which is the end of the document with your bookmark name?

I don't really understand the query about the spacebar.

There is an option to make the user-added bookmark positions visible (Tools>Options>View tab). The beginning and end look like [grey square brackets]. Or you can use Edit/GoTo to locate one by name.

Mr_FulanoAuthor Commented:
GrahamSkan and Chris, thank you both for your help. Chris hit the nail on the head when he suggested replacing the "\EndOfDoc" with my Bookmark. I had tried to do that at first, but I did -- "\myBookmark" instead of just "myBookmark". It works out well.

Thank you both for your help.
Mr_FulanoAuthor Commented:
Thank you both. You were both very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.