Solved

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

Posted on 2010-08-24
11
584 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

758 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now