By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Fill Word-template fields

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <us**@example.net> wrote in message
news:e6**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #2

P: n/a
"steven" <us**@example.net> schrieb:
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()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #3

P: n/a
Herfried K. Wagner [MVP] wrote:
"steven" <us**@example.net> schrieb:
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()
///


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
Nov 21 '05 #4

P: n/a
Robin Tucker wrote:
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
Nov 21 '05 #5

P: n/a
Thanks, that worked (eventually) great.

Steven

Herfried K. Wagner [MVP] wrote:
"steven" <us**@example.net> schrieb:
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()
///

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.