You're encountering an issue where using:
Try
' Attempt to access Excel or Outlook application
Catch ex As Exception
' Handle exception
End Try
...still causes an error, possibly during debugging or when the target Office application (e.g., Excel or Outlook) is not running.
This usually happens because of how COM interop works in .NET and how Visual Studio handles exceptions — especially in Debug mode. Let's break this down and provide a robust solution.
✅ Problem Summary
When you try to access Excel or Outlook COM objects like this:
Dim excelApp As Excel.Application = CType(Marshal.GetActiveObject("Excel.Application"), Excel.Application)
- If Excel is not running,
GetActiveObject throws an exception.
- In Visual Studio Debug Mode, even if you catch it with
Try/Catch, it might still break and show an unhandled exception.
- This is not a real runtime error — it’s just the debugger breaking on the thrown exception, which can be confusing.
✅ Goal
We want to safely detect whether:
- Excel is already running
- Outlook is already running
and only create a new instance if needed — without triggering unnecessary exceptions or breaking the flow.
✅ Solution: Use Late Binding + TryGetCOMObject
To avoid the debugger from stopping on these exceptions, we can use late binding via Type.GetTypeFromProgID() and Activator.CreateInstance(), and check for running applications more gracefully.
Here's a robust VB.NET implementation that safely checks for Excel and Outlook without triggering errors in the IDE.
✅ Updated Code (Safe COM Access)
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Core
Public Class RibbonButton
Inherits OfficeRibbon
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim appWord As Application = Globals.ThisAddIn.Application
Dim docCurrent As Document = appWord.ActiveDocument
If docCurrent Is Nothing Then
MessageBox.Show("No active document.")
Return
End If
Dim currentName As String = Path.GetFileNameWithoutExtension(docCurrent.FullName)
Dim docNew As Document = Nothing
Dim wbNew As Object = Nothing ' Late-bound Excel.Workbook
Dim xlApp As Object = Nothing
' Check open Word docs
For Each doc As Document In appWord.Documents
If doc IsNot docCurrent AndAlso Path.GetFileNameWithoutExtension(doc.FullName) = currentName Then
docNew = doc
Exit For
End If
Next
' Safely check for Excel
If Not TryGetExcelApplication(xlApp) Then
MessageBox.Show("Excel is not running.")
Else
For Each wb As Object In xlApp.Workbooks
If Path.GetFileNameWithoutExtension(wb.FullName) = currentName Then
wbNew = wb
Exit For
End If
Next
End If
' Output results
MessageBox.Show($"Current Doc: {docCurrent.FullName}" &
vbCrLf & If(docNew IsNot Nothing, $"Matched Word Doc: {docNew.FullName}", "") &
vbCrLf & If(wbNew IsNot Nothing, $"Matched Excel WB: {wbNew.FullName}", ""))
End Sub
''' <summary>
''' Safely tries to get the running Excel instance using late-binding.
''' </summary>
Private Function TryGetExcelApplication(ByRef excelApp As Object) As Boolean
Try
' Try to get running Excel instance
Dim typeExcel As Type = Type.GetTypeFromProgID("Excel.Application")
If typeExcel Is Nothing Then
Return False
End If
excelApp = Marshal.GetActiveObject("Excel.Application")
Return True
Catch ex As COMException
' Excel not running
Return False
Catch
Return False
End Try
End Function
''' <summary>
''' Safely tries to get the running Outlook instance using late-binding.
''' </summary>
Private Function TryGetOutlookApplication(ByRef outlookApp As Object) As Boolean
Try
Dim typeOutlook As Type = Type.GetTypeFromProgID("Outlook.Application")
If typeOutlook Is Nothing Then
Return False
End If
outlookApp = Marshal.GetActiveObject("Outlook.Application")
Return True
Catch ex As COMException
' Outlook not running
Return False
Catch
Return False
End Try
End Function
End Class