Exporting Query Results to Excel File AND Setting File as READ ONL

M

MJ

I have an associate of mine that is looking for a way to set a file that is
exported from one of our databases as READ ONLY.

The database is currently exporting the results of a query to an Excel file
just fine, but now the customer wants the file set as Read Only. Is there a
simple way to do that as the file is exported?

Thank you in advance for your assistance.
 
A

Arvin Meyer [MVP]

Here's some code that will send a recordset to Excel and protect it with a
password. It can then be opened, but only changed if one has the password.

Sub sCopyFromRS()
'Send records to the first
'sheet in a new workbook
'
Dim rs As DAO.Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Object
Dim objWkb As Object
Dim objSht As Object
Set rs = CurrentDb.OpenRecordset("qryStock", _
dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
Set objXL = CreateObject("Excel.Application")
With objXL
.Visible = True
Set objWkb = .Workbooks.Add
Set objSht = objWkb.Worksheets(1)
With objSht
.Range(.Cells(1, 1), .Cells(intMaxRow, _
intMaxCol)).CopyFromRecordset rs
.Protect "Arvin"
End With

End With
End If
End Sub
 
H

hp

Arvin Meyer said:
Here's some code that will send a recordset to Excel and protect it with a
password. It can then be opened, but only changed if one has the password.

Sub sCopyFromRS()
'Send records to the first
'sheet in a new workbook
'
Dim rs As DAO.Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Object
Dim objWkb As Object
Dim objSht As Object
Set rs = CurrentDb.OpenRecordset("qryStock", _
dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
Set objXL = CreateObject("Excel.Application")
With objXL
.Visible = True
Set objWkb = .Workbooks.Add
Set objSht = objWkb.Worksheets(1)
With objSht
.Range(.Cells(1, 1), .Cells(intMaxRow, _
intMaxCol)).CopyFromRecordset rs
.Protect "Arvin"
End With

End With
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top