这个代码,这么贴出来,可读性太差了吧?
不能替换的原因:
excel中的内容和word文档中的内容不能匹配;
excel中的字符串含有很多空格,而WORd中没有,多以find不到,因此不能做Replace。
检查下文档就好了。
另外,代码我也帖一个吧。
Sub test1()
iweizhi = 14
'生成合同文件
Dim yangbenpath As String
yangbenpath = Application.ActiveWorkbook.Path & Sheets("明细").Cells(iweizhi, 23).Value '获取修改文件《测试.doc》的位置
Dim deskpath As String
Dim weizhi As Variant
Dim WdApp, Wd, i%, Rng
Rng = Sheets("明细").Range("W23:X32")
Application.ScreenUpdating = False
Set WdApp = CreateObject("word.application")
WdApp.Visible = True
With WdApp.Documents.Open(yangbenpath)
WdApp.Visible = True
With .Content
For i = 1 To UBound(Rng)
If .Find.Execute(Rng(i, 1)) Then
.Text = Rng(i, 2)
End If
Next i
End With
.SaveAs "C:\2.doc"
End With
' deskpath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" '获取桌面位置路径
' Wd.SaveAs deskpath & Sheets("明细").Cells(3, 24).Value & ".doc" '另存到桌面,文件名称为合同名
WdApp.Visible = True
Set Wd = Nothing
Set WdApp = Nothing
Application.ScreenUpdating = True
End Sub