Exactly I would like to check if my Excel Workbook (Only one) is open. IF it is open THEN use some code to get some data from it ELSE open my Excel file first and get some data from it. I would like to keep my Excel file always open as long as I am working on my word document.
Here is some code in a word document to try to achieve this:
Dim appExcel As Excel.Application
Dim xlWBook As Excel.Workbook
On Error Resume Next
Set appExcel = GetObject(, "Excel.Application")
If appExcel Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
Set xlWBook = appExcel.Workbooks.Open(ActiveDocument.Path & strFile)
EndIf
' Some code
xlWBook.Sheets(3).Cells(4,2).value = strData1
' or the other way
strData2 = xlWBook.Sheet(4).Cells(3,5).Value
' ...etc...
xlWBook.Close SaveChanges:=True
appExcel.Quit
Set xlWBook = Nothing
Set appExcel = Nothing
Do I need to close it (as above), knowing I will use this kind of code for another module or userform? Am I on the right track? Or is it a better practice to open it each time and then close it when you finished?
My goal is to avoid opening and closing my Excel file each time I need something from it. Note that I open my Excel file in the beginning via a click on a button in the ribbon to enter data directly on it, therefore the user is likely to leave it open, but he may close it.
A last thought: should I found out if the excel file is open? If yes close it, then open and close it each time I need it.
Thanks.