Is there a way to run VBA from Outlook that will edit an Excel spreadsheet while it is open and visible in another window

I have written some vba that runs from MS Outlook, reads the currently selected email, does some processing and updates information in a spreadsheet. I cannot convert the spreadsheet to macro enabled because it is used by another system that can't handle macro enabled spreadsheets.

Following are the key elements of my code (I have removed some of the steps that don't relate to my problem). All of this works well but if I have the sheet open in Excel it crashes. So my question is: Is there any way to modify my code so it will perform the updates while I still have the spreadsheet open and visible in Excel? That would make it possible for me to check the entry, and possibly make minor adjustments, without having to open and close it in Excel after every update.

Set xlApp = CreateObject("Excel.application")
Set xlbook = Workbooks.Open(FileName:=WorkbookName, Notify:=bReadOnly)       'bReadOnly is set to true if I don't need to update
With xlbook.Sheets(MASTERSHEETNAME)
    ' various steps follow to find and update the relevant data
End With
xlbook.Save
xlApp.Quit
Set xlbook = Nothing
Set xlApp = Nothing
Rob4077Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
Please try this:
set xlApp = GetObject(, "Excel.Application")
With xlApp .Sheets(MASTERSHEETNAME)
    ' various steps follow to find and update the relevant data
End With
xlApp .Save
xlApp.Quit
Set xlApp = Nothing  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob4077Author Commented:
Thanks. Looks like it may fix the problem but I need to change a few other things as well to work with it and I need to go to the office. I will fully implement and test when I return and review question then. Thanks for the response.
0
Rob4077Author Commented:
Hi MacroShadow.
It starts working if the sheet is open. If it's not it says "ActiveX component can't create object". I presume I can get around that by trying to CreateObject then using in If Err GetObject

The part of the code that is failing is xlApp.save which is coming back with a "Application-defined or object-defined error" message OR "Method 'Save' of object '_Application' failed"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MacroShadowCommented:
It starts working if the sheet is open. If it's not it says "ActiveX component can't create object". I presume I can get around that by trying to CreateObject then using in If Err GetObject
Correct. Please note that if you have more than one open workbook, the code will fail.

The part of the code that is failing is xlApp.save which is coming back with a "Application-defined or object-defined error" message OR "Method 'Save' of object '_Application' failed"
Hmmm, makes sense, it's the worksheet we want to save not the Excel.Application. Try omitting the save line.
0
Rob4077Author Commented:
Thanks. I think I have worked out how to work well with it. If I leave the Save and quit it just inserts the lines and I can see them. Then if I want I can save or delete when I've finished working with it so I think that will be an even better solution than I had hoped for. Thanks again
0
NorieVBA ExpertCommented:
You might want to try GetObject first and if that fails CreateObject, something like this.
Dim xlApp As Object

    On Error Resume Next
    Set xlApp  = GetObject(, "Excel.Application")
    On Error Goto 0

    If xlApp Is Nothing Then
        Set xlApp = CreateObject("Excel.Application")
        Set xlbook = Workbooks.Open(FileName:=WorkbookName, Notify:=bReadOnly)       'bReadOnly is set to true if I don't need to update
    Else
        Set xlbook = xlApp.Workbooks(WorkbookName)
   End If

   With xlbook.Sheets(MASTERSHEETNAME)
    ' various steps follow to find and update the relevant data
   End With

   xlbook.Save

   xlApp.Quit

   Set xlbook = Nothing
   Set xlApp = Nothing

Open in new window

0
Rob4077Author Commented:
Fantastic, Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.