[英]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.