Open Excel File with VBA

Posted on 2006-04-24
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
    LVL 13

    Accepted Solution

    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

    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") pathname,,true
    myexcel.visible = true

    set my excel = nothing

    Author Comment

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

    Expert Comment

    No problem ;-)

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now