?
Solved

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

Posted on 2010-08-24
11
Medium Priority
?
600 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
[X]
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
  • 4
  • 4
  • 3
11 Comments
 
LVL 17

Assisted Solution

by:calacuccia
calacuccia earned 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1600 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

777 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