[英]Opening word template and saving as .doc to current workbook folder - VBA in excel
在 Excel 中使用 VBA,我打开了一个 word 模板文件。
我正在努力将文件作为 .doc 保存在与活动 excel 工作簿相同的文件夹中。 我不想将它保存在与检索它相同的文件夹中,也不想输入另一个绝对路径名。
我当前的代码不断将 .doc 保存到 C:\\Users\\USER\\Documents 而不是工作簿实际文件夹 (C:\\Users\\USER\\Desktop\\testdocs)
我的代码如下:
Sub OpenTemplateSaveAsDoc()
Dim wApp As word.Application
Dim wDoc As word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Open("C:\Users\USER\Documents\Custom Office Templates\wordtesttemplate.dotm", ReadOnly:=True)
With wDoc
wDoc.SaveAs CurDir & Application.PathSeparator & "MySavedDoc.doc"
End With
End Sub
改变
wDoc.SaveAs CurDir & Application.PathSeparator & "MySavedDoc.doc"
到
wDoc.SaveAs ActiveWorkbook.Path & Application.PathSeparator & "MySavedDoc.doc"
(而且,因为那段代码是用With wDoc
块包装的,你可以只使用.SaveAs ...
而不是wDoc.SaveAs ...
- With
块的唯一目的是让你避免输入With
块中对象的名称。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.