Hello,
I use the code below to export a table from access to excel. I now want unprotect the spreadsheet on open and protect it on close.
I have looked at the 'excelsheet.Unprotect Password:= "password in quotes" ', but I have also read that it doesn't work with objects?
Private Sub cmdExport1_Click()
Dim strTQName As String
Dim strSheetName As String
Dim strFilePath As String
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.
strTQName = "tblDolsData"
strFilePath = "C:\Users\Paul\Desktop\backup"
strSheetName = "Dols Data"
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Set rst = CurrentDb.OpenRecordset(strTQName)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strFilePath)
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets(strSheetName)
xlWSh.Range("a2").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("a2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
End Sub
Any help would be great.
Thank you.