PDA

View Full Version : Open an Excel file only if it is not already open



Jeronimo323
09-13-2008, 02:36 AM
Hello

I am not very good at Word VBA, but I would like to create the following macro.

At work, we use a Word macro designed to open Word files. I would like to modify the code in order to open a specific Excel file, but only if this Excel file is not already open. (I have already tried several times, but it did not work).

Also, I would like to close this Excel file (only the file, not the Excel application) when the user close the Word file. But I do not know how to do this.

Sorry for this stupid questions, but these problems have haunted me for weeks !

Thank you very much for your answers.

fumei
09-15-2008, 11:25 AM
These are not stupid questions, but quite reasonable ones.

Obviously I can not comment on what code you have already, as you did not post any. However, in principle:

1. "I would like to modify the code in order to open a specific Excel file" Opened from where? Word? Excel?

2. "but only if this Excel file is not already open" Then you must test to see if it is open.

"I have already tried several times, but it did not work."

Post the code of your tries.

tca_VB
09-19-2008, 06:07 AM
I'm doing something similar (I believe). I want to take form fields from word (after completed) and put them in an excel file that is already open.

I'm close again. Right now my code opens a new instance of excel and I would like to have it check to see that excel and the particular file is open already. Then I need to check if my fields will go into the proper row in excel.

Here's what I have so far...
'Add Data to Pin_Index
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'this creates a new instance, need to check for file already open
For dataCount = 1 To Active.Document.FormFields.Count
Active.Document.FormFields(dataCount).Result = xlApp.Workbook("MasterIndex").Sheets("Pin_Index").Cells(dataCount, 2).Value
Next dataCount

tca_VB
09-19-2008, 12:20 PM
I solved mine - mainly. I really didn't need to check that excel was open since I know that it is. I just needed to activate the desired workbook and sheet.

Here's another thread posting that may help the original poster:
http://www.vbaexpress.com/forum/showthread.php?t=14230

Here's my VBA for those following:
Set wdApp = CreateObject("Word.Application")
tempName = ActiveDocument.Name

For dataCount = 1 To ActiveDocument.FormFields.Count
wdResult = ActiveDocument.FormFields(dataCount).Result
Set xlApp = GetObject(, "Excel.Application")
xlApp.Visible = True
xlApp.Workbooks("MasterIndex.xls").Activate
xlApp.Workbooks("MasterIndex.xls").Sheets("Pin_Index").Cells(2, dataCount).Value = wdResult
Next dataCount

lucas
09-19-2008, 02:26 PM
Check to see if an excel file is open:
Sub test()
Dim wkbook As String
wkbook = "MyWorkbook.xls"
If IsOpen(wkbook) Then
MsgBox wkbook & " is open"
Else
MsgBox wkbook & " is not open"
End If
End Sub
Function IsOpen(WkBookName As String) As Boolean
IsOpen = False
For Each wkbk In Application.Workbooks
opened = UCase(wkbk.Name) = UCase(WkBookName)
If opened Then
IsOpen = True
End If
Next wkbk
End Function