简体   繁体   中英

Excel VBA / Mac (Big Sur) - Cannot access read-only document

I'm trying to write a simple macro to run on my Mac (Excel 16.61, Mac Book Pro running Big Sur 11.4) that copies the visible rows of a table into a new workbook then saves the new workbook as a *.csv file.

The current (non-working) code:

Sub Macro()

    Dim wb as Workbook
    Dim wbOutput As Workbook
    Dim FilePath As String

    Set wb = ThisWorkbook

    FilePath = "/path/to/filename.csv"

    ' Copy the visible rows of a filtered table
    With wb.Sheets("WorksheetName").ListObjects("tblName")
    
        .Range.AutoFilter Field:=18, Criteria1:="TRUE"
        .Range.SpecialCells(xlCellTypeVisible).Copy
    
    End With
    
    ' Paste the copied table rows into a new workbook and save as a *.csv file
    Set wbOutput = Workbooks.Add
    
    wbOutput.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
    wbOutput.SaveAs FileName:=FilePath, FileFormat:=xlCSV, CreateBackup:=False
    wbOutput.Close
 
End Sub

When I run it however I get the following error:

Run-time error '1004': Cannot access read-only document [filename]

Having spent a few hours searching on-line, I'm no closer to a solution. The inte.net's suggestions include:

  • Adding Excel in System Preferences.../Security & Privacy/Files and Folders (I can't see an obvious way of adding a new app, just remove the access rights of apps that already have folder access)
  • The GrantAccessToMultipleFiles function, but adding FilePath in the input array of the function makes no difference.

How can I create a *.csv file from the table?

Ran into the same issue but my file format was.txt but here was my solution after doing some research and getting some solid help from the Mac VBA Guru Ron De Bruin.

The code essentially bypass creating the output files, in my case.txt files in a folder location that has security protocols that cause the Error 1004 message and creates a subfolder in the Microsoft Folder under my User profile which for whatever reason Excel/Mac don't see as a security threat and allows the VBA to create/save the output file(s) into that folder.

Hopefully, you can extract out what you need from the code and Function to get yours to work. One other thing, since the output is going to such a weird folder location I suggest you save the folder path under your favorites on the Finder Left Panel so you can easily get to the files. See the MsgPopup box for the folder location

Sub Create_TxtFiles()

Dim MacroFolder        As String
Dim nW                 As Workbook
Dim ws1                As Worksheet, ws2 As Worksheet
Dim DT                 As String, RelativePath As String, wbNam1 As String, wbNam2 As String, Filepath As String

'Declarations
Set ws1 = ThisWorkbook.Sheets("Extract1")
Set ws2 = ThisWorkbook.Sheets("Extract2")
                    
RelativePath = ThisWorkbook.Path & "/"
DT = Format(CStr(Now), "mm_dd_yyyy hh.mmam/pm")
wbNam1 = "Extract 1 Output"    'Creates the File Name
wbNam2 = "Extract 2 Output"    'Creates the File Name
    
MacroFolder = "Upload Files"

Call CreateFolderinMacOffice2016(MacroFolder)
  
    'set the savepath as the obscure folder vba has access to'
    savepath = Application.DefaultFilePath & MacroFolder & "/"
           
        'copy the Output 1
        ws1.Copy
            ActiveWorkbook.SaveAs savepath & wbNam1 & DT & ".txt", FileFormat:=42
                Workbooks(wbNam1 & DT & ".txt").Close
                
        'copy the Output 2
        ws2.Copy
            ActiveWorkbook.SaveAs savepath & wbNam2 & DT & ".txt", FileFormat:=42
                Workbooks(wbNam2 & DT & ".txt").Close
    
Application.ScreenUpdating = True

  msgbox ("Upload file saved to folder: " & vbNewLine & vbNewLine & savepath)

End Sub
Function CreateFolderinMacOffice2016(NameFolder As String) As String
    'Function to create folder if it not exists in the Microsoft Office Folder
    'Ron de Bruin : 1-Feb-2019
    Dim OfficeFolder As String
    Dim PathToFolder As String
    Dim TestStr As String

    OfficeFolder = Application.DefaultFilePath
    PathToFolder = OfficeFolder & NameFolder

    On Error Resume Next
    TestStr = Dir(PathToFolder & "*", vbDirectory)
    On Error GoTo 0
    If TestStr = vbNullString Then
        MkDir PathToFolder
    End If
    CreateFolderinMacOffice2016 = PathToFolder
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM