PDA

View Full Version : Populate word documents with data



SteIT
12-06-2007, 05:21 AM
I have a number of word documents which are updated with data.(company name,dates etc.) and then saved as a copy of the original.

I am looking for the best method to prompt user for the data, then open each word document, populate the required fields (using bookmarks) and save a copy.
I have created a userform to collect the data, but now need to open each word document and populate the required fields(bookmarks?). Any ideas on how this can be done in vba.
:help

Nelviticus
12-06-2007, 09:06 AM
Well if you can write some code to open the documents that you want, this should get you started:


Public Sub DocReplace()
Dim oDoc As Document
Set oDoc = Documents.Open("FileName")
Call BmkReplace(oDoc, "BookmarkName", "ReplaceWithThis")
oDoc.SaveAs ("NewFileName")
oDoc.Close (wdDoNotSaveChanges)
Set oDoc = Nothing
End Sub

Public Sub BmkReplace(oDocument As Document, strBookmark As String, strValue As String)
' Replace text in bookmark at strBookmark with the text strValue
Dim rngBmk As Range
With oDocument
If .Bookmarks.Exists(strBookmark) Then
Set rngBmk = .Bookmarks(strBookmark).Range
rngBmk.Text = strValue
.Bookmarks.Add Name:=strBookmark, Range:=rngBmk
Set rngBmk = Nothing
End If
End With
End Sub

Regards

fumei
12-06-2007, 09:30 AM
This is also posted on Tek-Tips. There is nothing wrong with that, but it is requested that you mention any cross-posting.

Thanks.

SteIT
12-06-2007, 05:23 PM
Thanks for the reply Nelviticus. I have a word document which when opened will display a userform for the user to add the required data. This will then have the data as variables (one called 'customer', which appears in a lot of the documents requiring updates). This variable will then contain the name of the customer. Can this variable be used in your example code.

i.e. can the "ReplaceWithThis" be replaced with a variable name e.g 'customer'


Call BmkReplace(oDoc, "BookmarkName", "ReplaceWithThis")

This question was also posted on Tek-tips, I will try to add any cross references in future.

Thanks

Nelviticus
12-07-2007, 02:39 AM
Yes - replace oDoc with an object that refers to the document you want to fill in (the first function I posted is just a dummy one), replace "BookmarkName" with a string containing the name of the bookmark (or just hard-code the bookmark name) and replace "ReplaceWithThis" with a string containing what you want the bookmark replaced with.

Regards

SteIT
12-10-2007, 04:28 PM
Thanks for the reply Nelviticus. My project is coming along, I can open the files, update the bookmarks and save the files.
:beerchug:

The last thing (I hope) which i cannot work out is a method to open each document within a folder.

I need to open each file within the folder,update bookmarks, save file, then onto the next document with the folder.

Thanks for you help

Regards

steve

TonyJollans
12-10-2007, 05:16 PM
FName = Dir("C:\Path\To\Your\Documents\*.doc")
Do While FName <> ""
MsgBox FName ' Do whatever you want here
FName = Dir
Loop

fumei
12-11-2007, 02:48 PM
SteIT, I have to say this is a little annoying.

1. I posted a fairly extensive response to this question on Tek-Tips, and you did not bother to reply whatsoever.

2. I also gave you a pretty good nudge in the direction on how to do exactly what you are asking - again - here.

"The last thing (I hope) which i cannot work out is a method to open each document within a folder."

My post on Tek-Tips clearly gave you something to work with. Not only did you ignore that, but you posted your thread here (Dec 7th) AFTER my response on Tek-Tips (Dec 6th).

TonyJollans
12-11-2007, 05:09 PM
I'll second what Gerry says. The problem with cross-posting is that volunteers (in this case, me) waste their time providing answers that you already have; this tends to make us unhappy and not inclined to help the people who do it.

See http://www.excelguru.ca/node/7