VB Code to update a TextFeild on a Word Document

J

Jeffery B Paarsa

I am looking for a VB sample code to start a Word template document pupulate
a textfeild on the template that has Bookmark id and then print it.
--
Jeff B Paarsa

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/com...3a32ef60&dg=microsoft.public.word.vba.general
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
I am looking for a VB sample code to start a Word template document
pupulate a textfeild on the template that has Bookmark id and then
print it.

Put this sub (and do not change its name) in the ThisDocument module of your
template.
Whenever users double-clicks on the template to create a document, the code
will be automatically executed. If you want to give users a chance to save
the document themselves, remove the ".Close" line from the code.

On aspect of this that you totally left out in your "question" is the origin
of the information to use in order to populate the bookmark. In my example,
I use an input box which is fine for a single element (or maybe two). More
than that will require a userform.
For more on that, see:
http://word.mvps.org/faqs/Userforms/index.htm

Private Sub Document_New()

Dim strUserData As String

strUserData = InputBox("What is your name?", "Name", "Type Here")

If Trim(strUserData) = "" Then 'User clicked Cancel or erased all content
Exit Sub
Else
With ActiveDocument
.Bookmarks("NameBookmark").Range.Text = strUserData
.PrintOut
.Close wdDoNotSaveChanges
End With
End If

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jeffery B Paarsa

Thank you Jean,

I appreciate your help but I want to populate a one time entry of data like
first name, last name to be used for activating and printing 4-5 different
template word documents. Putting this macro on each word template is going
to be repeating the same task over and over. That’s why I would like to
capture the first name, last name thru a VB form script then use the captured
data to populate each and everyone of the word templates and then print them.
Making the user to enter same data over and over is going to be cumbersome
for the user. I read about your wonderful idea on
http://word.mvps.org/faqs/Userforms/index.htm this afternoon but soon I
realized it can not be used for my task.

Thanks for your advice though but I wish you would have direct me to where I
could do this task by only capturing data one time and populating to
different word templates and then print it. Currently I am using Word 2003
and Visual Basic 2005 express edition and waiting for my Visual Studio .net
pro to arrive. I have reached to the point that I would open and create
document by passing the template “in silent or visible mode†but I have been
having hard time to transfer the captured data fields from the form to the
Word opened document.

Regards
 
D

Doug Robbins - Word MVP

Private Sub Document_New()

Dim strUserData As String

strUserData = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "User")

If strUserData = "" Then
strUserData = InputBox("What is your name?", "Name", "Type Here")
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"User") = strUserData
With ActiveDocument
.Bookmarks("NameBookmark").Range.Text = strUserData
.PrintOut
.Close wdDoNotSaveChanges
End With

End Sub



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
Thank you Jean,

I appreciate your help but I want to populate a one time entry of
data like first name, last name to be used for activating and
printing 4-5 different template word documents. Putting this macro

Now you tell us! :)

"template word documents"
Are we discussing templates to produce documents, or just plain documents?
on each word template is going to be repeating the same task over and
over. That's why I would like to capture the first name, last name
thru a VB form script then use the captured data to populate each and
everyone of the word templates and then print them. Making the user

What is this obsession with printing? Will the documents based on these 4/5
templates only be used once?
Should they be saved? Discarded? This information will never be used again?
to enter same data over and over is going to be cumbersome for the
user. I read about your wonderful idea on
http://word.mvps.org/faqs/Userforms/index.htm this afternoon but soon
I realized it can not be used for my task.

Sure it can.
Thanks for your advice though but I wish you would have direct me to
where I could do this task by only capturing data one time and
populating to different word templates and then print it. Currently

I did.
But you are talking about many different things:
Getting data from a user,
Creating multiple documents,
Populating fields in those documents,
Printing,
And then what? You never mentioned what you intend to do with the documents
once they are printed.
I am using Word 2003 and Visual Basic 2005 express edition and
waiting for my Visual Studio .net pro to arrive. I have reached to

What code are you using?
the point that I would open and create document by passing the
template "in silent or visible mode" but I have been having hard time

"create document by passing the template "in silent or visible mode""
What does this means?
to transfer the captured data fields from the form to the Word opened
document.

Are you stating that you have managed to open 4/5 documents simultaneously?


Overall, your goals are not stated clearly, so it is difficult to help.
E.g., Doug has suggested a way to keep the information you would get from
the user in a text file. But, if this information is never needed again,
this is not necessary.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jeffery B Paarsa

Hi Jean,

Yes I have 5-6 different Word templates that I would like to use them and
print 5-6 different Word Documents from them. So the answer to your first
question is to produce Word documents from Word Templates.

No there are no requirements to save the documents after printing. Just to
populate the data fields captured from the VB Form put them on Bookmarked
TextFields and then print it. Captured data from VB Form field screen, will
be used to access database to extract more data for populating the TextFields
on the Word Templates to print the Word Documents "Future plan not right
now".

Yes the different Documents printed from these 5-6 templates will be used
only once and it will be printed once and does not get saved.

By Silent mode I mean that I do not want the user even see the "Word
document Windows" to be popped up and opened.... Just on the back ground
"invisible, silent mode" populate the data on the TextFields/Bookmarked
defined on the Templates print the Documents one after another.

I just received my Visual Studio Pro 2005 today... Haven't installed it.

You are really wonderful on having so much patience and being so through...

Regards
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
Hi Jean,

Yes I have 5-6 different Word templates that I would like to use them
and print 5-6 different Word Documents from them. So the answer to
your first question is to produce Word documents from Word Templates.

No there are no requirements to save the documents after printing.
Just to populate the data fields captured from the VB Form put them
on Bookmarked TextFields and then print it. Captured data from VB
Form field screen, will be used to access database to extract more
data for populating the TextFields on the Word Templates to print the
Word Documents "Future plan not right now".

Yes the different Documents printed from these 5-6 templates will be
used only once and it will be printed once and does not get saved.

By Silent mode I mean that I do not want the user even see the "Word
document Windows" to be popped up and opened.... Just on the back
ground "invisible, silent mode" populate the data on the
TextFields/Bookmarked defined on the Templates print the Documents
one after another.

I just received my Visual Studio Pro 2005 today... Haven't installed
it.

You could do this from VBA... But since you seem bent on doing it with VS...
transfer this VBA code to VB6. If you use VB.Net, more adjustments will be
needed.
Also, before printing the 4-5 documents, I would make sure that I asked the
user to confirm whatever it is they have typed. It would be frustrating as a
user to go to the printer and see you have a typo in all documents...
For this purpose, you could create a message box (see MsgBox in your VB
manuals), or, you could replace all Input Boxes and the message box with a
userform.

'_______________________________________
Option Explicit

Private strUserName As String
Private strUserOther As String

'_______________________________________
Sub Main()

Const myTemplate1 As String = "C:\Templates\Template1.dot"
Const myTemplate2 As String = "C:\Templates\Template2.dot"
Const myTemplate3 As String = "C:\Templates\Template3.dot"
Const myTemplate4 As String = "C:\Templates\Template4.dot"
Const myTemplate5 As String = "C:\Templates\Template5.dot"
Dim wdApp As Word.Application

strUserName = InputBox("What is your name?", "Name", "Type Here")

If Trim(strUserName) = "" Then 'User clicked Cancel or erased all content
Exit Sub
End If

strUserOther = InputBox("What is Other stuff?", "Stuff", "Type Here")

If Trim(strUserOther) = "" Then 'User clicked Cancel or erased all content
Exit Sub
End If

Set wdApp = CreateObject("Word.Application")

CreateDoc wdApp, myTemplate1
CreateDoc wdApp, myTemplate2
CreateDoc wdApp, myTemplate3
CreateDoc wdApp, myTemplate4
CreateDoc wdApp, myTemplate5

wdApp.Quit
Set wdApp = Nothing

End Sub
'_______________________________________

'_______________________________________
Private Sub CreateDoc(myApp As Word.Application, _
strTemplate As String)

Dim wdDoc As Word.Document

Set wdDoc = myApp.Documents.Add(strTemplate, False, wdNewBlankDocument,
False)
With wdDoc
.Bookmarks("NameBookmark").Range.Text = strUserName
.Bookmarks("StuffBookmark").Range.Text = strUserOther
.PrintOut
.Close wdDoNotSaveChanges
End With

End Sub
'_______________________________________

I could have used an array to store the template names, but since you only
have 4-5, it is not much trouble to do it as I did.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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

Top