Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect if a Certain Excel Spreadsheet is open before exporting data 1

Status
Not open for further replies.

Jayz

ISP
Feb 17, 2002
59
Hi,
Using VBA, On a button click, How can I detect if a Certain Excel Spreadsheet is open, before exporting data to this particular file.

I'd like it to bring up a msgbox to "close the spreadsheet" before exporting.

Any help would be much appreciated,

Jay
 
Jay,

I am having a hard time following what you are asking. Let my try to review. You are need to make sure that in your Excel workbook you need a certain sheet activated/visible.

I am not really sure on the next part about the msgbox.

-Uncle Cake
 
I've created a code which exports queried data to a specified excel spreadsheet:

'*******************************************************
Private Sub Export_Click()

DoCmd.TransferSpreadsheet acExport,_ 8,"OM_Search_query1","C:\Genie_Export\OM_Search1.xls", True

Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
xlApp.Workbooks.open "c:\GENIE_Export\OM_SEArch1.xls"
Set xlApp = Nothing ' the application, then release the reference.

End Sub
'*****************************

if I requery the data and try to export it to the spreadhsheet that is currently opened, the export procedure hangs, which is expected.

I want to avoid this happening by prompting the user that "the spreadsheet is currently opened", please close it first.

So I'm thinking that I need to detect first whether this spreadsheet is opened. If so, prompt the user. If not continue with the export of data.

Hope this helps.

Thanks in advance,
Jay

 
UncleCake are you there?

Can someone please help.

Regards,
Jay
 
You may want to try using GetObject to attach to the active instance of Excel. En error will occur if Excel is not running.
Code:
On Error Resume Next
Set xlApp = GetObject(,"Excel.Application")
If Err.Number <> 0
   'Excel is not open - file can not be open
   Err.Clear
   'Now, Open Excel
   Set xlApp = CreateObject(&quot;Excel.Application&quot;)
Else
   'Excel is Open
   'Traverse through active workbooks and check 
   '  to see if the file is already opened
   'If Open - tell user and quit
End If
 
Thanks dsi for the response.

My apologies as my VBA skills are very limited.
My understanding is that the above code will run when there is an error.
The problem is that because the data is trying to export to a currently opened spreadsheet, an error will not be detected by Access untill I close the spreadsheet. In the meantime it will just hang.

Any other ideas?
 
Here is a little function that will check to see if the workbook is open. Just call the IsWorkbookOpen function, passing the name of the file you are checking, and it will return True or False accordingly. I put some code in so that the case does not matter.
Code:
Option Explicit

Private Sub Command1_Click()
    'Check if Workbook is Open
    If IsWorkbookOpen(&quot;OM_Search1.xls&quot;) = False Then
        MsgBox &quot;workbook is not open&quot;
    Else
        MsgBox &quot;workbook is open&quot;
    End If
    Unload Me
    End
End Sub
Public Function IsWorkbookOpen(sName As String) As Boolean
    Dim xlApp As Object
    Dim wb As Object
    IsWorkbookOpen = False
    'See if Excel is Running
    On Error Resume Next
    Set xlApp = GetObject(, &quot;Excel.Application&quot;)
    If Err.Number <> 0 Then 'Excel not running
        Err.Clear
        IsWorkbookOpen = False
    Else    'Excel is running - check for WB
        For Each wb In xlApp.Workbooks
            If UCase(wb.Name) Like UCase(sName) Then
                IsWorkbookOpen = True
                Exit Function
            End If
        Next wb
    End If
    Set wb = Nothing
    Set xlApp = Nothing
End Function
Hope this helps...
 
Brilliant job dsi!!. A few tweaks to your code gave me exactly what I wanted. Thanks for all you help.

Regards,
Jay

Here is the modified code for those whom may benefit from it:

Dim xlApp As Object

'Check if Workbook is Open
If IsWorkbookOpen(&quot;OM_Search2.xls&quot;) = False Then

DoCmd.TransferSpreadsheet acExport, 8, &quot;OM_Search_query2&quot;, &quot;C:\Genie_Export\OM_Search2.xls&quot;, True

Set xlApp = CreateObject(&quot;excel.application&quot;)
xlApp.Visible = True
xlApp.Workbooks.open &quot;c:\GENIE_Export\OM_Search2.xls&quot; 'Path to your file

Else
MsgBox &quot;OM_Search2.xls is currently open. Please close the spreadsheet before exporting_ data. Note: Existing data will be overwritten&quot;, vbExclamation
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top