×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Open an existing Read Only Excel document

Open an existing Read Only Excel document

Open an existing Read Only Excel document

(OP)
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?

RE: Open an existing Read Only Excel document

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

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

RE: Open an existing Read Only Excel document

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

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

RE: Open an existing Read Only Excel document

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.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close