Hi Everyone,
I'm using a code to export a query from Access to create a new Excel with 1 Sheet and Protect with password, and everything's right.
My reference it was that post:
But I got Error 1004 when I try to use the Code to Protect a Excel with 3 Sheets, Could You help me please?
Thanks for all and best wishes from Brazil.
I'm using a code to export a query from Access to create a new Excel with 1 Sheet and Protect with password, and everything's right.
Code:
Private Sub botton_Click() On Error GoTo Err_botton_Click Dim excelapp As New Excel.Application Dim excelfile As New Excel.Workbook Dim excelsheet As New Excel.Worksheet DoCmd.TransferSpreadsheet acExport, , "query1", "C:\PED.xlsx", False Set excelfile = excelapp.Workbooks.Open("C:\PED.xlsx") Set excelsheet = excelfile.Worksheets.ITEM(1) excelsheet.Protect Password:="secret" excelfile.Save excelapp.ActiveWorkbook.Close True, "C:\PED.xlsx" excelapp.Quit Set excelsheet = Nothing Set excelfile = Nothing Set excelapp = Nothing Exit_botton_Click: Exit Sub Err_botton_Click: MsgBox Err.Number & " - " & Err.Description Resume Exit_botton_Click End Sub
But I got Error 1004 when I try to use the Code to Protect a Excel with 3 Sheets, Could You help me please?
Code:
Private Sub botton_Click() On Error GoTo Err_botton_Click Dim excelapp As New Excel.Application Dim excelfile As New Excel.Workbook Dim excelsheet As New Excel.Worksheet DoCmd.TransferSpreadsheet acExport, , "query1", "C:\PED.xlsx", False DoCmd.TransferSpreadsheet acExport, , "query2", "C:\PED.xlsx", False DoCmd.TransferSpreadsheet acExport, , "query3", "C:\PED.xlsx", False Set excelfile = excelapp.Workbooks.Open("C:\PED.xlsx") Set excelsheet = excelfile.Worksheets.ITEM(1) excelsheet.Protect Password:="secret" [B]'ERROR 1004 in this line [/B]Set excelsheet = excelfile.Worksheets.ITEM(2) excelsheet.Protect Password:="secret" Set excelsheet = excelfile.Worksheets.ITEM(3) excelsheet.Protect Password:="secret" excelfile.Save excelapp.ActiveWorkbook.Close True, "C:\PED.xlsx" excelapp.Quit Set excelsheet = Nothing Set excelfile = Nothing Set excelapp = Nothing Exit_botton_Click: Exit Sub Err_botton_Click: MsgBox Err.Number & " - " & Err.Description Resume Exit_botton_Click End Sub
Comment