0

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Excelsson
  • 195
  • 3
  • 16
  • I asked a couple of questions about this that shows how to do the path on a mac. – Solar Mike Jun 29 '20 at 20:24
  • Thank you Mike, can't seem to find your question, as I did some research on it as well but was not able to find the way to get the same path as the folder for Mac – Excelsson Jun 29 '20 at 20:31
  • Well, I only have 9 questions on here so it should not have been too much of a challenge... But here is one to give you an idea, if it gives you some help, give it a vote: https://stackoverflow.com/q/30575923/4961700 – Solar Mike Jun 29 '20 at 20:34
  • Or this one: https://stackoverflow.com/q/40690592/4961700 – Solar Mike Jun 29 '20 at 20:35

1 Answers1

1

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

Matt
  • 53
  • 1
  • 11