如何从VB脚本中调用单词vba宏代码:
单词vba宏代码在:
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
亲爱的成员可以创建一个包含上述代码的vb脚本文件,以便我调用vb脚本来运行代码。
我破坏了我的大脑并困扰谷歌,但无济于事。请帮忙。
谢谢。
维克
答案 0 :(得分:1)
有一个VBScript代码示例,它打开文档并使用给定选项进行两次替换,使用.Find.Execute
method的更紧凑形式:
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