Fill Word-template fields

  • Thread starter Thread starter steven
  • Start date Start date
S

steven

Hello,

I've created a Word-template for a letter. I've made some fields in the
template (such as 'customer number', 'name', ...) as follows: insert
field - DocVariabele with name 'customer number' and 'name'. Now I want
to fill these fields from my application and print them:


Dim myWord As New Word.Application
myWord.Documents.Add("c:\template.dot")
'fill the fields
'...
'print
myWord.ActiveDocument.PrintOut()
myWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)


So what code is needed before I print the document to fill my template
fields?

Thanks

Steven
 
You can read/write the document variables collection. Something like this:
(might not be exactly like this!):


For i as Integer = 0 To TheDocument.Variables.Count


Select Case theDocument.Variables.Item(i).Name

Case "a_name"

theDocument.Variables.Item(i).Value = "test"

End Select


Next


Something to note: make sure you clean up all references correctly :)
 
steven said:
Dim myWord As New Word.Application
myWord.Documents.Add("c:\template.dot")
'fill the fields
'...
'print
myWord.ActiveDocument.PrintOut()
myWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)

So what code is needed before I print the document to fill my template
fields?

\\\
Option Strict Off
..
..
..
Dim WordApp As Object = CreateObject("Word.Application")
Dim WordDoc As Object = WordApp.Documents.Open("C:\Hallo.doc")
WordDoc.FormFields("Text1").Result = "Bla"
WordDoc.Save()
WordDoc.Close()
WordApp.Quit()
///
 
Herfried said:
\\\
Option Strict Off
.
.
.
Dim WordApp As Object = CreateObject("Word.Application")
Dim WordDoc As Object = WordApp.Documents.Open("C:\Hallo.doc")
WordDoc.FormFields("Text1").Result = "Bla"
WordDoc.Save()
WordDoc.Close()
WordApp.Quit()
///

Thanks for the tip, but my document doesn't seem to have any items in
the 'FormFields' property, and neither does the 'Fields' and the
'Variables' property. Are there any other properties to look at?

Thanks in advance.

Steven
 
Robin said:
You can read/write the document variables collection. Something like this:
(might not be exactly like this!):


For i as Integer = 0 To TheDocument.Variables.Count
Select Case theDocument.Variables.Item(i).Name
Case "a_name"
theDocument.Variables.Item(i).Value = "test"
End Select
Next


Something to note: make sure you clean up all references correctly :)

Thanks for the tip, but my document doesn't seem to have any items in
the 'Variables' property, and neither does the 'Fields' and the
'FormFields' property. Are there any other properties to look at?

Thanks in advance.

Steven
 
Thanks, that worked (eventually) great.

Steven
\\\
Option Strict Off
.
.
.
Dim WordApp As Object = CreateObject("Word.Application")
Dim WordDoc As Object = WordApp.Documents.Open("C:\Hallo.doc")
WordDoc.FormFields("Text1").Result = "Bla"
WordDoc.Save()
WordDoc.Close()
WordApp.Quit()
///
 

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

Back
Top