Link to home
Start Free TrialLog in
Avatar of rrhandle8
rrhandle8Flag for United States of America

asked on

VB.NET adding .xla to Excel Menu

The code below runs in VB.Net. It adds an .XLA to the Excel Add-In Menu.  It is not working on Windows 64-Bit platform.

ERROR MESSAGE
Microsoft Visual Basic for Applications
Compile error:
The code in this project must be updated for use on 64-bit systems.  Please review and update Declare statements and then mark them with the PtrSafe attribute

Open in new window



  Friend Function LoadAsaExcelMenu() As Boolean
            Const messageTitle As String = "Add ASA Navigator button to Excel"

            Dim objExcel As Object = Nothing
            Dim blnExcelAlreadyLoaded As Boolean = False
            Try
                objExcel = GetObject(, "Excel.Application")
                blnExcelAlreadyLoaded = True
            Catch ex As Exception
                Try
                    objExcel = CreateObject("Excel.Application")
                Catch ex2 As Exception
                    MessageBox.Show("Could not get or create an instance of Excel.", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Return False
                End Try
            End Try
            If objExcel IsNot Nothing Then
                Try
                    Dim objWb As Object = objExcel.Workbooks.Add()
                    Try
                        Dim objAddin As Object = objExcel.AddIns.Add(Application.StartupPath & "\Excel Add-In\ASA Navigator.xla", True)
                        objAddin.Installed = True
                        objAddin = Nothing
                    Catch ex As Exception
                        LoadAsaExcelMenu = False
                        MessageBox.Show("Could not add ASA Navigator button to Excel (1).", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Finally
                        objWb.close(False)
                        objWb = Nothing
                    End Try

                    LoadASAExcelMenu = True
                Catch ex As Exception
                    LoadASAExcelMenu = False
                    MessageBox.Show("Could not add ASA Navigator button to Excel (2).", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Finally
                    If blnExcelAlreadyLoaded = False Then
                        objExcel.Quit()
                    End If

                    Marshal.ReleaseComObject(objExcel)
                    GC.Collect()

                    objExcel = Nothing
                End Try
            End If
        End Function

        Friend Function UnloadAsaExcelMenu() As Boolean
            Const messageTitle As String = "Remove ASA Navigator button from Excel"

            Dim objExcel As Object = Nothing
            Dim blnExcelAlreadyLoaded As Boolean = False
            Try
                objExcel = GetObject(, "Excel.Application")
                blnExcelAlreadyLoaded = True
            Catch ex As Exception
                Try
                    objExcel = CreateObject("Excel.Application")
                Catch ex2 As Exception
                    MessageBox.Show("Could not get or create an instance of Excel.", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Return False
                End Try
            End Try
            If objExcel IsNot Nothing Then
                Try
                    Dim objWb As Object = objExcel.Workbooks.Add()
                    Try
                        objExcel.AddIns("ASA Navigator").Installed = False
                    Catch ex As Exception
                        UnloadAsaExcelMenu = False
                        MessageBox.Show("Could not remove ASA Navigator button from Excel (1).", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Finally
                        objWb.close(False)
                        objWb = Nothing
                    End Try

                    UnloadASAExcelMenu = True
                Catch ex As Exception
                    UnloadASAExcelMenu = False
                    MessageBox.Show("Could not remove ASA Navigator button from Excel (2).", messageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Finally
                    If blnExcelAlreadyLoaded = False Then
                        objExcel.Quit()
                    End If

                    Marshal.ReleaseComObject(objExcel)
                    GC.Collect()

                    objExcel = Nothing
                End Try
            End If
        End Function

Open in new window

Avatar of rrhandle8
rrhandle8
Flag of United States of America image

ASKER

Sorry, I got that wrong.  It works on Windows 64-bit but does not work with Excel 64-bit.
ASKER CERTIFIED SOLUTION
Avatar of Jan Karel Pieterse
Jan Karel Pieterse
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial