[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-24
11
Medium Priority
?
603 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

649 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