[英]Excel VBA code to work on Mac, Create PDF Function
我已经编写了以下函数。 但是,我无法在Office Mac上使用它。 我不确定找到等效的EXP_PDF.DLL mac的过程
Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim FName As Variant
'Test If the Microsoft Add-in is installed
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
FName = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _ Title:="Create PDF")
'If you cancel this dialog Exit the function
If FName = False Then Exit Function
Else
FName = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(FName) <> "" Then Exit Function
End If
'Now the file name is correct we Publish to PDF
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=FName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(FName) <> "" Then Create_PDF = FName
End If
End Function
无需检查该特定DLL的存在,因为在MacOS下,PDF导出支持是本机的。 如果您删除了Add-in检查并删除了FileFilter字符串,那么您的代码就可以正常工作:
Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim FName As Variant
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FName = Application.GetSaveAsFilename("", Title:="Create PDF")
'If you cancel this dialog Exit the function
If FName = False Then Exit Function
Else
FName = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(FName) <> "" Then Exit Function
End If
'Now the file name is correct we Publish to PDF
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(FName) <> "" Then Create_PDF = FName
End Function
但GetSaveAsFilename
被削弱在MacOS和不允许通过的文件类型过滤文件。 如果需要将用户限制为某种文件类型,则可以使用AppleScript并执行以下操作:
Function Create_PDF_Mac(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim FName As Variant
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
'FName = Application.GetSaveAsFilename("", ".PDF", Title:="Create PDF")
On Error Resume Next
ThePath = MacScript("return (path to documents folder) as String")
TheScript = _
"set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFile to (choose file name with prompt ""Save As File"" " & _
"default name ""untitled.pdf"" default location alias """ & _
ThePath & """ ) as string" & vbNewLine & _
"if theFile does not end with "".pdf"" then set theFile to theFile & "".pdf"" " & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFile"
FName = MacScript(TheScript)
On Error GoTo 0
'If you cancel this dialog Exit the function
If FName = False Then Exit Function
Else
FName = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(FName) <> "" Then Exit Function
End If
'Now the file name is correct we Publish to PDF
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(FName) <> "" Then Create_PDF = FName
End Function
您可以使用OS选择器开关为每个OS运行适当的功能:
#If Mac Then
savedFileName = Create_PDF_Mac(...)
#Else
savedFileName = Create_PDF_PC(...)
#End If
考虑到MacOS中默认VB功能的局限性,这也是Microsof建议的方法 。
这是有关如何在较新版本的Mac Excel中进行操作的指南: https : //www.rondebruin.nl/mac/mac034.htm
重要的是您不能将文件保存到您选择的位置。
必须将其保存到当前用户主目录下的Library/Group Containers/UBF8T346G9.Office
文件夹中,因此在大多数情况下,是/Users/[current user]/Library/Group Containers/UBF8T346G9.Office
。 如果文件夹不存在,则必须创建它。 (请参见上面链接的页面上的代码示例)
罗恩!
请对此投票由MS修复, 网址为 : https : //excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.