• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

Word VBA code to open Excel file - check if the file is open already

I have the following VBA code to open an Excel file from Word. I would like the code to check if the file is open already, and if so then just set the focus to that file.
Thanks.
Dim xlApp As Object
Dim xlWB As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open("C:\file.xlsm")

Open in new window

0
etech0
Asked:
etech0
  • 4
  • 4
  • 3
2 Solutions
 
calacucciaCommented:
Try this
Dim xlApp As Object
Dim xlWB As Object
On Error Resume Next
Set xlWB = GetObject("C:\file.xlsm")
If Err.Number <> 0 Then
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open("C:\file.xlsm")
End If
On Error GoTo 0

Open in new window

0
 
rockiroadsCommented:
   Dim xlApp As Object
    Dim xlWB As Object
    Dim xlWBs As Object
    Dim sFile As String
    Dim bError As Boolean

    sFile = "c:\file.xlsm"

    Set xlApp = GetObject(, "Excel.Application")
    If err.Number = 0 Then
   
        For Each xlWBs In xlApp.Workbooks
            If xlWBs.fullName = sFile Then
                Set xlWB = xlWBs
                Exit For
            End If
        Next
   
        If xlWBs Is Nothing Then
            Set xlWB = xlApp.Workbooks.Open(sFile)
            xlWB.Parent.Windows(1).Visible = True
        End If
    Else
        Set xlApp = CreateObject("Excel.Application")
        Set xlWB = xlApp.Workbooks.Open(sFile)
    End If
    xlApp.Visible = True
        On Error GoTo 0
0
 
rockiroadsCommented:
heck I forgot the on error resume next, put that line before getobject call

On Error Resume Next

doh!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
etech0Author Commented:
The code does not seem to be working. It opens excel, but the file seems to be opened hidden and I can't access it or open it any other way.
0
 
calacucciaCommented:
Add one line, as shown by rockiroads.

Dim xlApp As Object
Dim xlWB As Object
On Error Resume Next
Set xlWB = GetObject("C:\file.xlsm")
If Err.Number <> 0 Then
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open("C:\file.xlsm")
    xlApp.Visible = True
End If
On Error GoTo 0
End Sub
0
 
rockiroadsCommented:
close down excel, run task manager and ensure no excel.exe running
make sure you have .visible = true lines added
then try again
0
 
etech0Author Commented:
rockiroads: I tried that.
calacuccia: your revised code does not seem to do anything.
0
 
calacucciaCommented:
Try setting your XLWB and XlApp as public variables.

If you go through your procedure like it is now, at the end the objects are released and close.

Public xlApp As Object
Public xlWB As Object


Sub GFGFG()
On Error Resume Next
Set xlWB = GetObject("C:\file.xlsm")
If Err.Number <> 0 Then
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open("C:\file.xlsm")
    xlApp.Visible = True
End If
On Error GoTo 0 '

End Sub
0
 
rockiroadsCommented:
etech, I have been unable to reproduce your problem. what version office r u using ?
here is my full code I used in word vba. can u try this without anything else?

Public Sub OpenExcelFileTest()

    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlWBs As Object
    Dim sFile As String


    On Error Resume Next
   
    sFile = "c:\test.xlsm"

    Set xlApp = GetObject(, "Excel.Application")
    If Err.Number = 0 Then
   
        For Each xlWBs In xlApp.Workbooks
            If xlWBs.FullName = sFile Then
                Set xlWB = xlWBs
                Exit For
            End If
        Next
   
        On Error GoTo 0
        If xlWBs Is Nothing Then
            Set xlWB = xlApp.Workbooks.Open(sFile)
            xlWB.Parent.Windows(1).Visible = True
        End If
    Else
        Set xlApp = CreateObject("Excel.Application")
        Set xlWB = xlApp.Workbooks.Open(sFile)
    End If
    xlApp.Visible = True

End Sub
0
 
etech0Author Commented:
calacuccia: Your code opens Excel but not the file
rockiroads: 2007. Your code seems to be doing the trick. I would like to test it some more with my code to make sure it's okay.
0
 
etech0Author Commented:
calacuccia: Your code did the trick. Thanks to both of you for your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now