首页 > 解决方案 > Excel vba 代码另存为 pdf 不再适用于 excel 365(Windows 10)

问题描述

以前在 Windows 7 (Excel 2013) 上工作的 Excel VBA 代码不再在 Windows 10 (excel 365) 上工作。

现在生成了运行时错误。

Run-time error '-2147024773 (8007007b)'

Document not saved

调试器突出显示从第一个 ActiveSheet.ExportAsFixedFormat 开始的 4 行,一直到 False。

有没有办法改变我的代码,以便在 Windows 10 / excel 365 上运行?

Workbooks("Valuation Grail.xlsm").Save

Dim mydir As String
Dim mydrive As String

'Used activeworkbook.path instead of CurDir() because activeworkbook.path does not change for the same saved workbook
mydir = ActiveWorkbook.Path
mydrive = Left(mydir, 1)

'save original wmu as 2 PDFs
Dim month_end As String
Dim generic_vg As String
Dim archived_vg As String
Dim taa_packet As String

'creates saving format for archived pdf
month_end = Format(WorksheetFunction.EoMonth(Now(), -1), "yyyymmdd")

generic_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\Valuations Report.pdf"
archived_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\" & month_end & "-Valuations.pdf"
taa_packet = mydrive & ":\01\spec_folder2\#Packet Assembly\TAA\" & "12 - Valuation Grail.pdf"


'Saves the generic and archived version of valuation report to spce_folder data folder
ThisWorkbook.Sheets(Array("Table", "Table_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        generic_vg, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        archived_vg, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
ThisWorkbook.Sheets("Summary").Select

'saves the pe tabs to the oshea packet assembly folder
ThisWorkbook.Sheets(Array("PE_Summary", "TAA_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        taa_packet, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
ThisWorkbook.Sheets("Summary").Select

标签: excelvbawindows-10excel-365

解决方案


ActiveWorkbokk.Path以前返回工作簿所在的完整路径,包括驱动器映射到的字母(Z、Y、X 等)。然后我会使用mydrive仅提取该字母以在我的文件路径中使用。这种方法不再有效。

我改变了 mydrive = "Z" 并且代码现在可以工作了。

理想情况下,我想找到一种新方法来提取该驱动器号,这样我们团队的成员就不必每次都手动更改 mydrive 变量,但这目前有效。


推荐阅读