When I create a new code module I don't have access to the Excel object model, how do I get access?

Add-in Express™ Support Service
That's what is more important than anything else

When I create a new code module I don't have access to the Excel object model, how do I get access?
 
Jakov Perica


Guest


I use VB.NET

When I create a new code module I don't have access to the Excel object model.

A new code module looks like this:

Module Module1

End Module

What do I need to do to be able to:

*Define Excel variables such as ranges
*Access Excel objects such as ActiveSheet

EDIT: In case this is not a AIX question per se, but rather an VB.NET for Excel question, I would be thankful for a pointer to a knowledge source that you think can fill my knowledge gaps. I am new to using .NET for Excel.
Posted 08 Jan, 2015 07:21:19 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Jakov,

Copy Imports statements from the module where this works to the new module.


Andrei Smolin
Add-in Express Team Leader
Posted 08 Jan, 2015 08:54:38 Top
Jakov Perica


Guest


Hi Andrei,

It works in AddinModule.vb so I have tried to copy the import statements from there. I arrive at:

Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms
Imports AddinExpress.MSO
Imports Excel = Microsoft.Office.Interop.Excel


Module Module1

    Sub MySub()
        Dim wks As Excel.Worksheet = ExcelApp.ActiveWorkbook.Worksheets.Add()

    End Sub

End Module


But Visual Studio tells me that "'ExcelApp' is not declared. It may be inaccessible due to its protection level."
Posted 08 Jan, 2015 09:21:09 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Jakov,

You need to use this approach to access ExcelApp from such a module:

CType(AddinModule.CurrentInstance, {add-in project name such as MyAddin1}.AddinModule).ExcelApp



Andrei Smolin
Add-in Express Team Leader
Posted 09 Jan, 2015 04:12:21 Top
Jakov Perica


Guest


Thank you, that works splendidly!
Posted 09 Jan, 2015 05:13:05 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 09 Jan, 2015 08:31:40 Top