Reputation: 157
How can I call a word vba macro code from a VB Script:
the word vba macro code is under:
Sub find_replace_vik_42216()
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "abc"
.Replacement.Text = "def"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "pqr"
.Replacement.Text = "xyz"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
End With
Application.ScreenUpdating = True
End Sub
could dear members create a vb script file which contains the above code, so that I call the vb script , in order to run the code.
I have wrecked my brains and troubled google, to no avail. Please help.
Thank you.
Vik
Upvotes: 0
Views: 2288
Reputation: 12602
There is an example of VBScript code which opens the document and make two replacements with given options, more compact form of the .Find.Execute
method used:
Const wdFindContinue = 1
Const wdReplaceOne = 1
Dim objWord, objDocument
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDocument = objWord.Documents.Open("C:\test.docx")
With objWord
.ScreenUpdating = False
With .Selection
.Collapse
With .Find
' .Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
' "abc" -> "def"
.Execute "abc", False, False, False, False, False, True, wdFindContinue, False, "def", wdReplaceOne
' "pqr" -> "xyz"
.Execute "pqr", False, False, False, False, False, True, wdFindContinue, False, "xyz", wdReplaceOne
End With
End With
.ScreenUpdating = True
End With
Upvotes: 1