簡體   English   中英

打開word模板並將.doc另存為當前工作簿文件夾-excel中的VBA

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM