scott56
09-17-2008, 03:07 AM
Hi,
I would like to create a test to confirm if a DocVariable exists inside a word document. If it does then I will populate it with data. If it does not exist then I want to report an error to the user....
I have the following sample code....the test that does currently work is if the docvariable = "".....any help appreciated
'Now check if word is already running...
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then Set wdApp = CreateObject("Word.Application")
'Get the Template Directory
strTemplateLocation = Worksheets("Sheet1").Range("TemplateDirectory") & "\Letter Template.doc"
'Now open the template document and open a new working documnt to output all templates
On Error GoTo 0
Set wdTemplateDoc = wdApp.Documents.Open(strTemplateLocation)
Set wdWorkingDoc = wdApp.Documents.Add
Set myRange = ActiveWorkbook.Worksheets("Sheet1").Range("FirstNameStartHeading")
i = 1
Do While myRange.Offset(i, 0).Value <> ""
'Update the template fields to populate
wdTemplateDoc.Activate
If wdTemplateDoc.Variables(myRange.Offset(0, 0).Value).Value = "" Then
MsgBox "We don't have a Doc Variable for " & myRange.Offset(0, 0).Value
Exit Do
End If
wdTemplateDoc.Variables(myRange.Offset(0, 0).Value).Value = myRange.Offset(i, 0).Value
wdTemplateDoc.Variables(myRange.Offset(0, 1).Value).Value = myRange.Offset(i, 1).Value
wdTemplateDoc.Variables(myRange.Offset(0, 2).Value).Value = myRange.Offset(i, 2).Value
wdTemplateDoc.Variables(myRange.Offset(0, 3).Value).Value = myRange.Offset(i, 3).Value
I would like to create a test to confirm if a DocVariable exists inside a word document. If it does then I will populate it with data. If it does not exist then I want to report an error to the user....
I have the following sample code....the test that does currently work is if the docvariable = "".....any help appreciated
'Now check if word is already running...
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then Set wdApp = CreateObject("Word.Application")
'Get the Template Directory
strTemplateLocation = Worksheets("Sheet1").Range("TemplateDirectory") & "\Letter Template.doc"
'Now open the template document and open a new working documnt to output all templates
On Error GoTo 0
Set wdTemplateDoc = wdApp.Documents.Open(strTemplateLocation)
Set wdWorkingDoc = wdApp.Documents.Add
Set myRange = ActiveWorkbook.Worksheets("Sheet1").Range("FirstNameStartHeading")
i = 1
Do While myRange.Offset(i, 0).Value <> ""
'Update the template fields to populate
wdTemplateDoc.Activate
If wdTemplateDoc.Variables(myRange.Offset(0, 0).Value).Value = "" Then
MsgBox "We don't have a Doc Variable for " & myRange.Offset(0, 0).Value
Exit Do
End If
wdTemplateDoc.Variables(myRange.Offset(0, 0).Value).Value = myRange.Offset(i, 0).Value
wdTemplateDoc.Variables(myRange.Offset(0, 1).Value).Value = myRange.Offset(i, 1).Value
wdTemplateDoc.Variables(myRange.Offset(0, 2).Value).Value = myRange.Offset(i, 2).Value
wdTemplateDoc.Variables(myRange.Offset(0, 3).Value).Value = myRange.Offset(i, 3).Value