回复 5楼 ZHRXJR
麻烦您看下这个程序,没有报错,但是替换不了
,怎么回事。谢谢
Private Sub Command1_Click()
Tmpfile = "C:\Users\Administrator\Desktop\tmp.docx"
If Dir(Tmpfile) <> "" Then
Kill Tmpfile
End If
FileCopy "C:\Users\Administrator\Desktop\报告模板.docx", Tmpfile
Dim myword As Object, mydocument As Object, myselection As Word.Selection
Dim i As Integer
Dim SearchStr As String
Dim ReplaceStr As String
Set myword = CreateObject("Word.Application")
'创建word对象
Set mydocument = myword.Documents.Open(Tmpfile)
'打开指定word文档
Set myselection = myword.Selection '定位文件实例
myword.Visible = False
For i = 0 To 3 Step 1
SearchStr = "A" + Str(i)
ReplaceStr = Text1(i).Text
myselection.Find.ClearFormatting
myselection.Find.Replacement.ClearFormatting
With myselection.Find
.Text = SearchStr
.Replacement.Text = ReplaceStr
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
myselection.Find.Execute Replace:=wdReplaceAll
Next
mydocument.Save
mydocument.Close '关闭文档实例
myword.Quit '关闭WORD实例
Set mydocument = Nothing '清除文件实例
Set myword = Nothing '清除WORD实例
FName = "C:\Users\Administrator\Desktop\" + Text2.Text + ".docx"
If Dir(FName) <> "" Then
MsgBox "文件已存在,请重新命名。", 48, 报告名称错误
Else
Name Tmpfile As FName
End If
MsgBox "完成"
End Sub