[英]Excel VBA code to work on Mac, Create PDF Function
I have coded the following function. 我已经编写了以下函数。 However, I cannot get it to work on office Mac. 但是,我无法在Office Mac上使用它。 I am not sure of the procedure to find the EXP_PDF.DLL mac equivalent 我不确定找到等效的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
There is no need to check for the existence of that specific DLL, because under MacOS, PDF export support is native. 无需检查该特定DLL的存在,因为在MacOS下,PDF导出支持是本机的。 Your code simply works if you remove the Add-in check and remove the FileFilter string: 如果您删除了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
But GetSaveAsFilename
is crippled on MacOS and does not allow filtering files by filetype. 但GetSaveAsFilename
被削弱在MacOS和不允许通过的文件类型过滤文件。 If you need to restrict users to a certain filetype, you can resort to AppleScript and do the following: 如果需要将用户限制为某种文件类型,则可以使用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
The you can use an OS selector switch to run the appropriate function for each OS: 您可以使用OS选择器开关为每个OS运行适当的功能:
#If Mac Then
savedFileName = Create_PDF_Mac(...)
#Else
savedFileName = Create_PDF_PC(...)
#End If
Given the limitations of default VB functions in MacOS, this is Microsof't suggested method as well. 考虑到MacOS中默认VB功能的局限性,这也是Microsof建议的方法 。
Here is a guide on how you can do it in newer versions of Mac Excel: https://www.rondebruin.nl/mac/mac034.htm 这是有关如何在较新版本的Mac Excel中进行操作的指南: https : //www.rondebruin.nl/mac/mac034.htm
What's important is that you can't save your file to a location that is selected by you. 重要的是您不能将文件保存到您选择的位置。
It has to be saved to the folder Library/Group Containers/UBF8T346G9.Office
under the current user's home dir, so /Users/[current user]/Library/Group Containers/UBF8T346G9.Office
in most of the cases. 必须将其保存到当前用户主目录下的Library/Group Containers/UBF8T346G9.Office
文件夹中,因此在大多数情况下,是/Users/[current user]/Library/Group Containers/UBF8T346G9.Office
。 If the folder is not there, you have to create it. 如果文件夹不存在,则必须创建它。 (See the code example on the page linked above) (请参见上面链接的页面上的代码示例)
Kudos to Ron! 罗恩!
Please vote for this to be fixed by MS here: https://excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method 请对此投票由MS修复, 网址为 : https : //excel.uservoice.com/forums/304933-excel-for-mac/suggestions/36531559-fix-exportasfixedformat-method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.