tags:

views:

17

answers:

1

I need to manipulate the VBA environment of an application from a C# addin so I can add modules, event handlers etc to documents that are created by the addin.

I've done it before in VB.Net but can't see how to get at the IDE object in C#. Am I just missing a reference or a Using directive? I can't see anything appropriate in the references.

In VB.Net I have a reference to Microsoft.VisualBasic and I have done this:

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel


Public Module VBA
    Private m_VBEnv As VBIDE.VBE
    Private m_dlg As AddInDialog

    Private Structure EventHandler
        Public objectName As String
        Public eventName As String
        Public action As String
    End Structure
    Private m_handlers As New Collection

    Public Function InitVBA(ByVal app As PBObjLib.Application, ByVal dlg As AddInDialog, ByVal scrWidth As Integer, ByVal scrHeight As Integer) As Boolean
        InitVBA = False
        Try
            m_dlg = dlg
            m_VBEnv = DirectCast(app.VBE, VBIDE.VBE)
            m_VBEnv.MainWindow.Height = 480
            m_VBEnv.MainWindow.Width = 640
            m_VBEnv.MainWindow.Left = (scrWidth - m_VBEnv.MainWindow.Width)
            m_VBEnv.MainWindow.Top = ((scrHeight - m_VBEnv.MainWindow.Height) - 50)
            m_VBEnv.MainWindow.WindowState = VBIDE.vbext_WindowState.vbext_ws_Minimize ' .vbext_ws_Normal
            HideVBWindow()
            InitVBA = True
        Catch e As Exception
            dlg.LogMessage(AddInDialog.LogLevel.Err, "Failed to initialise VBA: " & e.Message)
        End Try

    End Function

    ...

End Module

I have added a reference to Microsoft.VisualBasic to my C# but it does not recognise VBIDE.VBE.

Also, how would I do the DirectCast bit in C#. Is it just a simple cast?

Any suggestions?

--- Alistair.

A: 

The answer was to ensure that all references were .NET references. I had a COM one hiding in there. I removed the VB related references, reloaded the project, added the references (all .NET this time) and it all came to life.