我有一本工作簿,其中有许多宏可以将各种工作表导出为pdf,并保存在工作簿的相同位置。
我的问题是,如果工作簿保存在桌面上的文件夹中,那么生成的PDF就可以了。
当工作簿保存在网络位置时,不会生成pdf。下面是宏的一个示例:
Sub PDF_CStmtP()
Application.ScreenUpdating = False
ThisWorkbook.Sheets(Array("C Stmt - P")).Select
pdfname = fileSaveName
ChDir ActiveWorkbook.Path & "\"
fileSaveName = "Closing Statement (Purchase)"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
fileSaveName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Application.ScreenUpdating = True
ActiveWorkbook.Sheets("Main Menu").Activate
MsgBox "File Saved " & " " & fileSaveName
End Sub
答案 0 :(得分:2)
您的问题是ChDir命令,请参阅此处获取解释:https://www.techonthenet.com/excel/formulas/chdir.php
这一点的重要部分是" CHDIR语句允许您更改当前驱动器上的当前目录。如果您需要更改驱动器,请首先尝试使用CHDRIVE语句。"
当您尝试保存到网络驱动器时,您正在将驱动器号从C:\更改为网络驱动器映射到的任何内容,在我的情况下,它是U:\。
对代码的简单修复是将路径从ChDir移动到文件名中,因此您的代码应如下所示:
Sub PDF_CStmtP()
Application.ScreenUpdating = False
ThisWorkbook.Sheets(Array("C Stmt - P")).Select
pdfname = fileSaveName
'ChDir ActiveWorkbook.Path & "\"
fileSaveName = ActiveWorkbook.Path & "\" & "Closing Statement (Purchase)"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= fileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.ScreenUpdating = True
ActiveWorkbook.Sheets("Main Menu").Activate
MsgBox "File Saved " & " " & fileSaveName
End Sub
您可以进行一些其他编辑以清理它,但这将解决手头的问题。
**根据有关消息框的注释,您可以将代码更改为:
Sub PDF_CStmtP()
Application.ScreenUpdating = False
ThisWorkbook.Sheets(Array("C Stmt - P")).Select
pdfname = "Closing Statement (Purchase)"
'ChDir ActiveWorkbook.Path & "\"
fileSaveName = ActiveWorkbook.Path & "\" & pdfname
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= fileSaveName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.ScreenUpdating = True
ActiveWorkbook.Sheets("Main Menu").Activate
MsgBox "File Saved " & " " & pdfname
End Sub