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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search & Replace in Word with VBS

Status
Not open for further replies.

jlorenz1

Instructor
Aug 24, 2005
2
DE
I want to search and replace items in a word document with a vbs-script. I tried everything without success. Thanks in advance Johannes from Germany

Set Word = CreateObject("Word.Application")
Word.Documents.Open("c:\example.doc")
Word.Visible= true
Word.Documents.Add
Set Text = Doc.Content
Text.Find.Execute FindText="one"), ReplaceWith="1", Replace=wdReplaceAll

 
Hi,

I have had the same problem and got solution from here. What I wanted was a script to run a macro (search&replace) against several .doc files.

Create folder "c:\convert" (or whatever name you want), and put (copy) all .doc files that you want to replace items into.

In Word, create macro that searches&replaces (eg. "1" to "one") and save it in Word (eg. "YourMacro").

In Word, set macro security to low or medium, I prefer low.

Now, create vbscript, like this:

''''''''''''''''''''''''''
Dim i, objWord
Dim objFileSys, objFile
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")

For Each objFile in objFileSys.GetFolder("C:\Convert").Files
IF UCase(Right(objFile.Path, 4)) = ".DOC" Then
objWord.Documents.Open(objFile.Path)

objWord.Application.Run "YourMacro"
End If
Next

Set objFileSys = Nothing
objWord.Quit True
Set objWord = Nothing

''''''''''''''''''''''''''''''''

This will not make Word visible, but it will do the job in the background.

You can run this script against any Word macro, just change the name of macro, and, of course, you have to record and save it prior to use.

This should help.

Regards,

 
All parameters are positional. All named constants have to be declared.

[tt]Set Word = CreateObject("Word.Application")
[red]set Doc=[/red]Word.Documents.Open("c:\example.doc")
Word.Visible= true
[red]'[/red]Word.Documents.Add [red]'what for?[/red]
Set Text = Doc.Content
[red]'[/red]Text.Find.Execute FindText="one"[red])[/red], ReplaceWith="1", Replace=wdReplaceAll
[blue]'const wdReplaceAll=2[/blue]
[blue]Text.Find.Execute "one",,,,,,,,,"one","1",2[/blue]
[/tt]
 
correction: The line obviously should be read.
[tt] [blue]Text.Find.Execute "one",,,,,,,,,"1",2[/blue]
[/tt]
 
Thanks you all. This helps me:
Const wdReplaceAll = 2
Const wdFindContinue = 1


sSourceDocPath = "c:\tst\quelle.doc"
sTargetDocPath = "c:\tst\ziel.doc"


strFindText = "etwas"
strTextReplace = "Johannes5655787"


Set objWord = CreateObject("Word.Application")
objWord.Visible = false
objWord.Documents.Open sSourceDocPath


Set objDoc = objWord.ActiveDocument
objDoc.Activate


With objWord.Selection.Find
.ClearFormatting
.Text = strFindText
.Replacement.ClearFormatting
.Replacement.Text = strTextReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.MatchAllWordForms = False


' Function Execute([FindText], [MatchCase], [MatchWholeWord],
' [MatchWildcards], [MatchSoundsLike], [MatchAllWordForms], [Forward],
' [Wrap], [Format], [ReplaceWith], [Replace], [MatchKashida],
' [MatchDiacritics], [MatchAlefHamza], [MatchControl])


.Execute .Text,,,,,,,,,,wdReplaceAll,False,False,False,False


End With
objDoc.SaveAs sTargetDocPath
objDoc.Close False
objWord.Quit True

Johannes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top