Home |
Search |
Today's Posts |
#1
|
|||
|
|||
Defining and replacing variable text in multiple Word documents?
I want to be able to define a variable in multiple Word documents and then
replace it in all of them simultaneously. For example, I might define a variable "productname" and then replace it with "Acme Widgets" in 20 separate documents. What's the easiest and least expensive way to do this? |
#2
|
|||
|
|||
Lisa,
You can use the following to create the document varialble "productname" and set its value to Acme Widgets. Sub BatchAddVariable Dim MyFile As String Dim PathToUse As String Dim numWordsToUse As Long Dim Counter As Long Dim myDoc As Document 'Create a dynamic array variable, and then declare its initial size Dim DirectoryListArray() As String ReDim DirectoryListArray(1000) '1000 is arbitrary 'Specify folder containing files PathToUse = "C:\Batch Folder\" 'Loop through all the files of *.doc type in the directory by using Dir$ function MyFile = Dir$(PathToUse & "*.doc") 'For each file found add to the array Do While MyFile "" DirectoryListArray(Counter) = MyFile 'Get the next file name MyFile = Dir$ Counter = Counter + 1 Loop 'Reset the size of the array without losing its values by using Redim Preserve ReDim Preserve DirectoryListArray(Counter - 1) Application.ScreenUpdating = False For Counter = 0 To UBound(DirectoryListArray) Set myDoc = Documents.Open(FileName:=PathToUse & DirectoryListArray(Counter), _ Visible:=False) With myDoc .Variables("productname").Value = "Coswell Cogs" .Save .Close End With Next Counter Application.ScreenUpdating = True End Sub Note this will not insert the { DocVariable "productname" } field in your text. You would have to add more code to go to some point in the document, insert the field code and update the fields. Cost $1,000.000.00 :-) See: http://gregmaxey.mvps.org/word_tips.htm |
#3
|
|||
|
|||
Hi ?B?TGlzYUg=?=,
I want to be able to define a variable in multiple Word documents and then replace it in all of them simultaneously. For example, I might define a variable "productname" and then replace it with "Acme Widgets" in 20 separate documents. If I'm following you correctly, what I'd probably do is create a CustomDocumentProperty in the documents containing the "placeholder" text. Use a DocProperty field throughout the document to display that text. When you want to change the value you should be able to use a tool provided by Microsoft - DSOFile.dll - to change the document property without opening the file. Search the KB on the msdn site to locate information and a link to DSOFile.exe. You'll also find an article on using it at word.mvps.org. Cindy Meister INTER-Solutions, Switzerland http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004) http://www.word.mvps.org This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :-) |