Opening Excel Workbook from Word using VBA - Always Read Only

A

Alan

Hi All,

I wasn't sure whether this should be a Word or Excel query so I have
cross-posted. Apologies if anyone objects!

I am using the following code in a module in Word to open an existing
Excel document:


Sub Open_Plan()

Dim xls As Object

Dim wrk As Object

Dim xlfilename As String

xlfilename = "F:\Alan\Plan.xls"

Set xls = CreateObject("Excel.Application")

xls.Visible = True

Set wrk = xls.workbooks.Open(FileName:=xlfilename, Updatelinks:=0,
ReadOnly:=False, Password:="password")

End Sub



This works fine, except that it seems to skip or ignore the 'ReadOnly
= FALSE' argument, and the file still opens as Read Only every time.

I am stumped as to how to get around this.

Thanks in advance for any help you can provide.

Alan.
 
J

Jezebel

Have you checked in Windows Explorer to see if the file itself is read only
(came off a CD perhaps)?
 
A

Alan

Jezebel said:
Have you checked in Windows Explorer to see if the file itself is
read only (came off a CD perhaps)?

Hi Jezebel,

I should have stated up front that I had in fact checked that, but
good point to ask nevertheless.

Unfortunately, it is not that simple an answer for me!

Any other ideas?

Thanks,

Alan.
 
J

JGM

Hi Alan,

Try changing
Set wrk = xls.workbooks.Open(FileName:=xlfilename, Updatelinks:=0, _
ReadOnly:=False, Password:="password")
for
Set wrk = xls.workbooks.Open(FileName:=xlfilename, Updatelinks:=0, _
WriteResPassword:="password")

If your original code opens the workbook all it means that it is not
protected against opening it, but against modification, which is a different
password. If you have two passwords, you will have to include both in the
arguments, even if it is the same password for opening and for modification.

Something like:
Set wrk = xls.workbooks.Open(FileName:=xlfilename, Updatelinks:=0, _
Password:="password", WriteResPassword:="password")

If you decalre both password like this, You do not need the ReadOnly
argument. Actually, the ReadOnly argument is to give you the option to open
a document through code and make it "Read Only" even if it has not been
protected by a password... In fact, without the password argument, the
ReadOnly argument is useless, and if you supply a password, why bother with
a ReadOnly argument... see what I mean?

Anyway, just my 2 cents!

HTH
Cheers!
 
A

Alan

JGM said:
Try changing


for

Set wrk = xls.workbooks.Open(FileName:=xlfilename, Updatelinks:=0, _
WriteResPassword:="password")

If your original code opens the workbook all it means that it is not
protected against opening it, but against modification, which is a
different password. If you have two passwords, you will have to
include both in the arguments, even if it is the same password for
opening and for modification.

_______________________________________
Jean-Guy Marcil
(e-mail address removed)

Hi Jean-Guy,

Thanks for your input.

I looked into this, but the password is definately only to open the
workbook, not just for modification.

I have got around this for the time being by having Word run a small
executable that I wrote that then opens excel as wanted.

Thank you for your input though - I may return to this when I have a
little more time, and I'll investigate your suggestion in detail.

Cheers,

Alan.
 

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