none
Activating or opening an existing Excel workbook with Visual Basic from within a Visual Studio Project

    Question

  • Hello all,

    I have tried numerous ways to modify the responses to "Openening an Excel file from within project" to open an existing Excel workbook and also select and activate a minimized workbook but have totally struck out,  Can anyone give me some guidance?  Also, where should I be looking for samples that answer such basic questions?

    Thanks very much.

    Noel Rietman

     

    Friday, September 01, 2006 5:47 PM

Answers

  • Imports Excel = Microsoft.Office.Interop.Excel

    Dim XL As Excel.Application

    'Set to new instance of excel

    XL = New Excel.Application

    XL.Visible = True

    XL.WindowState = Excel.XlWindowState.xlMaximized

    'Create(New WrokBook)

    Dim wkbk As Excel.Workbook = XL.Workbooks.Add

    'add(worksheet)

    wkbk.Worksheets.Add()

    'open existing workbook

    Dim w2 As Excel.Workbook = XL.Workbooks.Open("C:\MyXL.xls")

    Friday, September 01, 2006 7:56 PM

All replies

  • HI,

    To connect to an existing Excel instance then you should use the GetObject function instead of the CreateObject().

    Here's a how to on Excel API's:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wroriautomatingexcelusingexcelobjectmodel.asp

     

     

    cheers,

    Paul June A. Domag

    Friday, September 01, 2006 7:16 PM
  • Imports Excel = Microsoft.Office.Interop.Excel

    Dim XL As Excel.Application

    'Set to new instance of excel

    XL = New Excel.Application

    XL.Visible = True

    XL.WindowState = Excel.XlWindowState.xlMaximized

    'Create(New WrokBook)

    Dim wkbk As Excel.Workbook = XL.Workbooks.Add

    'add(worksheet)

    wkbk.Worksheets.Add()

    'open existing workbook

    Dim w2 As Excel.Workbook = XL.Workbooks.Open("C:\MyXL.xls")

    Friday, September 01, 2006 7:56 PM
  • How do I correct these errors? I entered them into a button1_click in a public class form1. I want to click on a button and open an existing workbook.

    Thanks for any help
    Tom Hallmark

    Imports Excel = Microsoft.Office.Interop.Excel  '''''''Imports gives me a Syntax error

    Dim XL As Excel.Application          ''''''''''''Excel.Application is not defined

    'Set to new instance of excel

    XL = New Excel.Application

    XL.Visible = True

    XL.WindowState = Excel.XlWindowState.xlMaximized     ''''''''''''Excel is not declared

    'Create(New WrokBook)

    Dim wkbk As Excel.Workbook = XL.Workbooks.Add      '''''''''''Excel.Workbook is not defined

    'add(worksheet)

    wkbk.Worksheets.Add()

    'open existing workbook

    Dim w2 As Excel.Workbook = XL.Workbooks.Open("C:\MyXL.xls")

     

     

    Tuesday, September 12, 2006 2:27 AM
  • Hi,

    I guess this error results if you haven't added the Excel PIA in your references. To do this:

    1. Right click your project in the solutions explorer
    2. Select Properties
    3. Go to the References Tab
    4. Click the Add button
    5. The Add Reference Dialog will appear, select the COM tab
    6. Search Microsoft Excel 11.0 Object Library
    7. Click Ok

     

    cheers,

    Paul June A. Domag

    Wednesday, September 13, 2006 3:28 PM
  • Thank you for your reply. I added the Excel 11.0 object Library however I get the same errors.

    Dim w2 As Excel.Workbook = XL.Workbooks.Open("C:\MyXL.xls") ---Excel.Workbook not declared

    Is there something I need to declare at the top of my code? Sorry if I am a bit new but am trying to move from VBA to .net.

    Thanks for any help
    Tom Hallmark

    Friday, September 15, 2006 2:26 AM
  • This is the code that MSDN says will open a workbook. It Does not work.

    All I get with this is     "Type Excel.Workbook  is Not Defined"   How do I define it?

    Thanks for any help.

    Tom Hallmark

    The Workbooks collection in Microsoft Office Excel 2003 makes it possible to work with all the open workbooks, create a new workbook, and import data into a new workbook.

    To open an existing workbook

    • Use the Open method of the Workbooks collection, passing in the path to the workbook:
      ' Visual Basic
      Dim wb As Excel.Workbook = _
          ThisApplication.Workbooks.Open("C:\YourPath\YourWorkbook.xls")
      

      The Workbooks collection in Microsoft Office Excel 2003 makes it possible to work with all the open workbooks, create a new workbook, and import data into a new workbook.

      To open an existing workbook

      • Use the Open method of the Workbooks collection, passing in the path to the workbook:
        ' Visual Basic
        Dim wb As Excel.Workbook = _
            ThisApplication.Workbooks.Open("C:\YourPath\YourWorkbook.xls")
        
    Friday, September 15, 2006 2:33 AM
  •  Tom Hallmark wrote:

    Imports Excel = Microsoft.Office.Interop.Excel  '''''''Imports gives me a Syntax error

     

    What version of VB are you using?

    Friday, September 15, 2006 10:37 PM
  • What version of VB are you using?

    I am using Visual Studio 2005 with tools for Office "VSTO"

    Thanks
    Tom

    Saturday, September 16, 2006 7:05 PM
  • I could only get the import declaration to work by enclosing each line in <% %> delimiters as below

     

    <%@ Import Namespace="System"%>

    <%@ Import Namespace="System.Data"%>

    <%@ Import Namespace="System.Data.sqlClient"%>

    Tuesday, September 19, 2006 9:13 AM
  •  

    <%@ Import Namespace="System"%>

    I will give this a try but I do not understand. I have not seen this in any of my books. What does the <%@ do?

     

    Thanks
    Tom Hallmark

    Monday, September 25, 2006 3:10 AM
  • I know this is an old post, but if you are using this thread for a current project and run into this same error as Tom, it's because you did not instantiated the "thisApplicatoin" object (Excel.Application).  Look at the "thisApplication" object in your code and change it from:

    dim thisApplication as Excel.Application (you may also have " = Nothing" at the end)

    to

    dim thisApplication as Excel.Application = New Excel.Application

    or

    dim thisApplication as New Excel.Application

    **The two lines do the same thing. It's really just up to you on how you like to code.

    This was detailed out in DMan1's post which has been marked as answered.  Just don't forget to add the reference and import the correct namespaces.

    Saturday, May 26, 2012 3:40 PM