|
本帖最后由 monster7788 于 2018-6-11 09:32 编辑
求大神解答,同样的宏运行,在sheet1可以,后面的运行时候提示路径错误,调用的文件是没问题的,这是什么问题??
Private Sub CommandButton_Click()
If TextBox1.Text = "" Then
MsgBox "请输入数字", vbOKOnly, "提示"
Else
Dim mypath, Newname, m, XB, wApp, X, Y
mypath = ThisWorkbook.Path & "\"
X = TextBox1.Text
Y = TextBox2.Text
For m = X To Y Step 6
Newname = Range("B" & m) & Range("z" & m) & ".docx" '给新生成的表起个名称
FileCopy mypath & "模板.docx", mypath & Newname '将模板复制并重命名
Set wApp = CreateObject("word.application") With wApp
.Visible = False
Dim objWordDoc
Set objWordDoc = .Documents.Open(mypath & Newname) '打开我们复制的新文件进行更改
Do While .Selection.Find.Execute("MCT1#") '寻找MCT1这个关键词,将其用表格中的单号来代替
.Selection.Text = Range("B" & m).Text
.Selection.HomeKey Unit:=6
Loop
.Documents.Save
objWordDoc.ExportAsFixedFormat mypath & Range("b" & m) & Range("z" & m) & ".pdf", 17
.Quit
End With
Next
Set wApp = Nothing
MsgBox "已经完成", vbOKOnly, "提示"
End If
End Sub
|
|