Excel VBA代码在Mac上工作,创buildPDFfunction

我已经编码了以下function。 但是,我不能让它在办公室的Mac上工作。 我不知道程序findEXP_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导出支持是本地的。 如果您删除加载项检查并删除FileFilterstring,您的代码只是工作:

 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上被 GetSaveAsFilename ,不允许按文件types过滤文件。 如果您需要将用户限制为某种文件types,则可以使用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 

您可以使用OSselect器开关为每个操作系统运行适当的function:

 #If Mac Then savedFileName = Create_PDF_Mac(...) #Else savedFileName = Create_PDF_PC(...) #End If 

鉴于MacOS默认的VB函数的限制,这也是Microsof'tbuild议的方法 。