保存为 pdf 的 Excel vba 代码不再适用于 excel 365 (windows 10)

问题描述

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

现在生成一个运行时错误

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

Document not saved

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

有没有办法更改我的代码,使其在 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,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,OpenAfterPublish:= _
        False
ThisWorkbook.Sheets("Summary").Select

解决方法

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

我更改了 mydrive = "Z",代码现在可以工作了。

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