Reputation: 195
I have a code that will export each individual tab as PDF in the same folder where the Excel file is located, it is working as intended on Windows, but failing on mac under the error "Application-defined or object-defined error", I've been researching on VBA use on Mac , but it seems that you need to save it in a completely different folder location that you need previous access to, any solution on this? It can work either on the same folder or in a folder that the user can select from an open dialog window. Below the code:
Option Explicit
Sub WorksheetLoop()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim WS_Count As Integer
Dim I As Integer
Dim fName As String
' Set WS_Count equal to the number of worksheets in the active workbook.
Set wbA = ActiveWorkbook
WS_Count = wbA.Worksheets.Count
strPath = wbA.Path
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
' Begin the loop.
For Each wsA In wbA.Worksheets
wsA.Activate
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = ActiveSheet.Range("D5").Value & ".pdf"
myFile = strPath & strFile
Debug.Print myFile
'export to PDF if a folder was selected
If myFile <> "False" Then
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
End If
Next wsA
MsgBox "Your PDF files have been created: "
Worksheets("Summary").Activate
End Sub
Upvotes: 0
Views: 245
Reputation: 53
This sounds like a problem with "sandboxing" on the Mac. Starting with Office 2016, Apple made Microsoft limit the folders that Excel VBA could save files to.
I'm not exactly sure how you want to adjust your code to make it work, but Ron de Bruin has a great website about Excel VBA, and a particularly helpful section about doing it on the Mac. This particular page talks about the sandboxing issue, and explains which specific folders you should be able to save files to without any problems.
https://www.rondebruin.nl/mac/mac034.htm
Upvotes: 1