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?
GrahamSkanConnect With a Mentor RetiredCommented:
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

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 ;)
Chris BottomleyCommented:

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

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 BottomleyConnect With a Mentor Commented:

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.