Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open an existing Read Only Excel document

Status
Not open for further replies.

dpcraney

Technical User
Aug 25, 2009
1
US
I've reviewed the posts for something similar but have been unable to find anything. I have the following code snip...

Dim xlApp As Object
Dim ObjWorkbook As Object
Dim ObjWorksheet As Object



Set xlApp = CreateObject("Excel.Application")
xlApp.Application.DisplayAlerts = False
set ObjWorkbook = xlApp.Workbooks.Open ("S:\Dan\folder\file.xls")
xlApp.Visible = True
Set objWorksheet = xlApp.Sheets("sheet1")

The file "S:\Dan\folder\file.xls" is saved as a password protected file and I want to open a "Read Only" version. In VBA, I know how to do this as...

Workbooks.Open Filename:="s:\Dan\folder\file.xls", _ReadOnly:=True
Set shSheet = Worksheets("Sheet1")

How can I open this file as a read only type? What is the correct syntax?
 
set ObjWorkbook = xlApp.Workbooks.Open ("S:\Dan\folder\file.xls")
If not ObjWorkbook.ReadOnly then
objWorkbook.ChangeFileAccess Mode:=xlReadOnly, WritePassword:="admin"
end if
xlApp.Visible = True

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Okay ignore my former post, it appeared to be working as I had a non visible copy of the workbook open so subsequent opens were ReadOnly by default.

It spawned the following idea however.
Code:
Sub Main
    Dim xlDummy as Object
    Dim xlApp as Object
    Dim xlWkbkDummy as Object
    Dim ObjWorksheet As Object

    'opening a workbook twice for ReadOnly trickery
    Set xlDummy = CreateObject("Excel.Application")
    Set xlApp = CreateObject("Excel.Application")
    xlDummy.DisplayAlerts = False 
    xlApp.DisplayAlerts = False 
    Set xlWkbkDummy = xlDummy.Workbooks.Open ("C:\test.xls")
    Set ObjWorkbook = xlApp.Workbooks.Open ("C:\test.xls")
    
    'close the first wkbk so only the ReadOnly copy is available to user
    xlDummy.quit
    Set xlDummy = Nothing
    Set xlWkbkDummy = Nothing

    Set objWorksheet = xlApp.Sheets("sheet1")
    'show ReadOnly wkbk
    xlApp.Visible = True
    
    'Set shSheet = Worksheets("Sheet1")

End Sub

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Or you could set the property of S:\Dan\folder\file.xls to read only to begin with and avoid the whole mess.

This is a simple right click on the file choose properties and check the read only box.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top