VB and VBA Users Source Code: Opening an Excel workbook from VB
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Opening an Excel workbook from VB
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, July 06, 2001
Hits:
1175
Category:
Office
Article:
The following code demonstrates how to open an existing Excel workbook from VB. Option Explicit 'Purpose : Opens a workbook in a new instance of Excel 'Inputs : sPath The path of the workbook to open 'Outputs : Returns True if succeeded in opening the workbook. 'Author : Andrew Baker 'Date : 25/05/2001 'Notes : Function WorkbookOpen(sPath As String) As Boolean Dim oExcel As Object If Len(sPath) > 0 And Len(Dir$(sPath)) Then On Error GoTo ErrFailed Set oExcel = CreateObject("Excel.Application") oExcel.Workbooks.Open sPath oExcel.WindowState = 2 oExcel.Visible = True Set oExcel = Nothing WorkbookOpen = True Else Debug.Print "Can't find workbook " & sPath WorkbookOpen = False End If Exit Function ErrFailed: Debug.Print "Failed: " & Err.Description WorkbookOpen = False On Error GoTo 0 End Function 'Demonstration routine Sub Test() If WorkbookOpen("C:\Test.xls") Then MsgBox "Opened workbook", vbSystemModal Else MsgBox "Failed to open workbook" End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder