Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a number of word docs to edit bookmarks

Status
Not open for further replies.

SteIT

Programmer
Nov 22, 2007
18
GB
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 date, 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.
 
There are probably several ways. I'd start by opening a new document, turning on the Macro Recorder, doing what you want manually with a test date, and seeing what the recorded code looks like. Then you can add an inputbox for the date prompt and make any other modifications you think useful.

_________________
Bob Rashkin
 
Break it up into separate procedures though.

If this is an operation that is repeated - and it sounds like it is - then you want a procedure that replaces text in a bookmark, and makes sure the bookmark is not deleted. Normally, if you insert text into a bookmark range, as in:

ActiveDocument.Bookmark("Yadda").Range.Text = "Blah"

Sure, it will put that text there, but it deletes the bookmark. So "Yadda" is gone. Therefore, this can not be repeated.

There are examples of code that allows repeatable bookmark text insertion here.

Second. There are a couple of methods for opening a series of documents.

Word has a built-in Dir function that works very well.

Or, you can use a FileSystemObject. This requires making sure the Reference to FSO is checked.

If your requirements are fairly simple, using Dir will work fine.

So:

1. get the data, which you seem to have already done.
2. Use Dir to open each file
3. make a call to a separate procedure to insert the text into the bookmark.

This is not particularly difficult. There are examples here in the VBA forum.

Look up the Dir function in Word VBA Help and try opening multiple files to see how it works.

You do not state a few issues. Are all the files in the same folder? Are all the changes to the files going to be the same - ie. are they all be changes to ONE date input by the user? If so, the code could certainly be streamlined.

Start with trying to figure out using Dir. When you have specific questions, ask.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top