I am trying to create user defined variables in Word for which I can set values from an excel file."
macropod's approach works fine, but if you really want to use VBA, it is not all that difficult. However, we need some details.
Do you want to create Document variables on the fly, via code, from Excel? It could be done, but if you just want to have them...then create them in the Word document first. Once they are created, even with a Value of "", you can easily use them (or change the Value) from Excel.
Code:
Option Explicit
Sub Yadda()
Dim appWord As Word.Application
Dim aDoc As Word.Document
Set appWord = CreateObject("Word.Application")
Set aDoc = appWord.Documents.Open _
(Filename:="c:\testarray3.doc")
[COLOR=red]' set the variable that [b]already exists[/b] with a Value
' this demo is explicitly putting a text value[/color red]
aDoc.Variables("Unemployment").Value = "Yadda"
[COLOR=red]' Selection is a property of the application
' NOT the activeDocument, so use appWord[/color red]
With appWord
[COLOR=red]' go the end of the document[/color red]
.Selection.EndKey Unit:=wdStory
[COLOR=red]' type in text plus variable Value[/color red]
.Selection.TypeText "The unemployment rate in " & _
"Boston is " & aDoc.Variables("Unemployment").Value
End With
aDoc.Save
aDoc.Close
Set aDoc = Nothing
appWord.Quit
Set appWord = Nothing
End Sub
This assume the Word file testarray3.doc
already has a variable named "Unemployment".
The code creates an instance of Word, opens the file, sets the variable "Unemployment" with a Value of "Yadda", goes to the end of the document and enters the text plus variable Value. It then saves and closes the file, and destroys the objects.
This assumes early binding and the proper Reference to Word is in the Excel module.
Result?
"The unemployment rate in Boston is Yadda"
You CAN also create the document variables from Excel, if you need to.
Code:
Option Explicit
Sub Yadda()
Dim appWord As Word.Application
Dim aDoc As Word.Document
Set appWord = CreateObject("Word.Application")
Set aDoc = appWord.Documents.Open _
(Filename:="c:\testarray3.doc")
[COLOR=red]' set the variable that [b]already exists[/b] with a Value
' this demo is explicitly putting a text value[/color red]
aDoc.Variables("Unemployment").Value = "Yadda"
[COLOR=red]' CREATE the variable "Car" with a Value[/color red]
aDoc.Variables.Add Name:="Car", _
Value:="Ferrari 550 Maranello"
[COLOR=red]' Selection is a property of the application
' NOT the activeDocument, so use appWord[/color red]
With appWord
[COLOR=red]' go the end of the document[/color red]
.Selection.EndKey Unit:=wdStory
[COLOR=red]' type in text plus variable Value[/color red]
.Selection.TypeText "The unemployment rate in " & _
"Boston is " & aDoc.Variables("Unemployment").Value _
& vbCrLf _
& "I want to drive a " & aDoc.Variables("Car").Value
End With
aDoc.Save
aDoc.Close
Set aDoc = Nothing
appWord.Quit
Set appWord = Nothing
End Sub
Result?
The unemployment rate in Boston is Yadda
I want to drive a Ferrari 550 Maranello
I do want to reiterate that macropod's approach - depending on what your actual requirements are - may be a better/easier way to go.
"A little piece of heaven
without that awkward dying part."
advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)
Gerry