【问题标题】:Opening word template and saving as .doc to current workbook folder - VBA in excel打开word模板并保存为.doc到当前工作簿文件夹-excel中的VBA
【发布时间】:2016-12-31 06:21:55
【问题描述】:

在 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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    改变

    wDoc.SaveAs CurDir & Application.PathSeparator & "MySavedDoc.doc"
    

    wDoc.SaveAs ActiveWorkbook.Path & Application.PathSeparator & "MySavedDoc.doc"
    

    (而且,因为那段代码是用With wDoc 块包装的,所以您可以只使用.SaveAs ... 而不是wDoc.SaveAs ... - With 块的唯一目的是让您避免键入With 块内对象的名称。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多