Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-08-24
11
588 Views
Last Modified: 2012-05-10
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
Comment
Question by:etech0
  • 4
  • 4
  • 3
11 Comments
 
LVL 17

Assisted Solution

by:calacuccia
calacuccia earned 100 total points
ID: 33513003
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33513061
   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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33513091
heck I forgot the on error resume next, put that line before getobject call

On Error Resume Next

doh!
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Author Comment

by:etech0
ID: 33544539
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
 
LVL 17

Expert Comment

by:calacuccia
ID: 33544713
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33545137
close down excel, run task manager and ensure no excel.exe running
make sure you have .visible = true lines added
then try again
0
 
LVL 10

Author Comment

by:etech0
ID: 33546330
rockiroads: I tried that.
calacuccia: your revised code does not seem to do anything.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33546664
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
 
LVL 65

Accepted Solution

by:
rockiroads earned 400 total points
ID: 33553049
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
 
LVL 10

Author Comment

by:etech0
ID: 33553598
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
 
LVL 10

Author Closing Comment

by:etech0
ID: 33553620
calacuccia: Your code did the trick. Thanks to both of you for your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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