Workbooks.open

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

[405 byte] By [NoelRietman] at [2007-12-24]
# 1

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

PaulDomag at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

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")

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

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")

TomHallmark at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

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

PaulDomag at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

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

TomHallmark at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

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")

TomHallmark at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Tom Hallmark wrote:

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

What version of VB are you using?

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

What version of VB are you using?

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

Thanks
Tom

TomHallmark at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

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"%>

Spamcop at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

<%@ 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

TomHallmark at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...