Open Excel File with VBA

Posted on 2006-04-24
Medium Priority
Last Modified: 2012-06-27

I am trying to open an Excel file with VBA.  My code looks like this so far :

Set appXL = New Excel.Application
Set xlWB = appXL.Workbooks.Open(strPath)
xlWB.Application.Visible = True

It opens and it even edits the way I want it to but I cant save it.  I noticed that when it opens the Excel it says read-only.  But when I open that file normally it does not say read only.  How do I change this?  Thanks

Question by:lebeau26
  • 3
LVL 13

Accepted Solution

Lucas earned 500 total points
ID: 16528508
If you already have the file open before you open it via your code, it will always be read only.

Set xlWB = appXL.Workbooks.Open(strPath),,true
'sets it to read only

Set xlWB = appXL.Workbooks.Open(strPath),,false
'not read only

LVL 13

Expert Comment

ID: 16528549
You should also look into "late binding" so that your app will work in all versions of excel.

In the above code you have to also:

set xlapp = Nothing
set xlWb = Nothing
Here is an example that does the same as above.

Dim pathname as string
dim myExcel as object

pathname = "C:\myExcelfile.xls"

set myExcel = CreateObject("Excel.Application")

myExcel.workbooks.open pathname,,true
myexcel.visible = true

set my excel = nothing

Author Comment

ID: 16529214
Ugh, I so dumb, I had it open.  Thanks
LVL 13

Expert Comment

ID: 16529252
No problem ;-)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question