none
Checking the existence of document variable throws COMException RRS feed

  • Question

  • Hi,

    In my Word Add-In solution (C#) I'm trying to check for the existence of a particular document variable. The problem is when the document variable does not exists, it throws COMException. I'm using the try-catch properly to determine the existence of the variable, so in the catch statement I return 'false' as the variable was not found.

    Here is my code:

    public static bool ReadDocVar(Document doc, string varName, out string varData)
    {
        object obj;
        Word.Variable v;
    
        obj = varName;
    
        try
        {
            v = doc.Variables.get_Item(ref obj); // <- If document variable doesn't exists, it throws COMException
            varData = v.Value; // <- 'v' is deleted
        }
        catch (System.Exception)
        {
            varData = string.Empty;
            return false;
        }
    
        return true;
    }

    The exception does not stops the application,  but in VS output window I receive:

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Source.DLL

    Is there a way to eliminate this kind of messages? Or is it bad programming and I should use different approach to check for existence of document variables.

    Thanks.

    Monday, March 10, 2014 8:49 AM

Answers

  • Hi etaiso,

    When an application is being debugged, the debugger gets notified whenever an exception is encountered.

    If you don’t want to show the exception message in the output window, you can disable it via setting the option: debug->option->Output Window->Exception message

    And article below may be helpful to understand the exception:
    What is a First Chance Exception?
    First and second chance exception handling

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 14, 2014 9:59 AM
    Moderator

All replies

  • Hello etasio,

    The Variables collection doesn't provide any method for checking whether a particular property is presented at the moment. So, your scenario can be used in real world applications.

    But also, as a workaround, you can use iterate over all variables in the collection and see whether it contains the item you are interested in. The Item method of the Variables collection accepts an index which indicates the ordinal position. Actually, the Item method corresponds to the indexer in the .Net world. So, based on the Count property value you can iterate via all items and check the Name property.

    Monday, March 10, 2014 10:57 AM
  • Ok, thanks. 

    The iteration approach will not fit to my needs as I have document with many variables.

    What about the exception messages that sent to the Visual Studio output window, is there a way to eliminate them without code change?

    Monday, March 10, 2014 12:14 PM
  • Did you try to catch the 'System.Runtime.InteropServices.COMException'  instead?

    There is no way to exclude exceptions due to the fact that Variables collection doesn't provide a special method for searching a particular variable.

    Monday, March 10, 2014 12:25 PM
  • I just tried that. No change. Both catching but the output is similar.
    Monday, March 10, 2014 2:36 PM
  • Hi etaiso,

    What version of Visual Studio and Office are you using? I change the code to below and it works well for me (Visual Studio 2013 and Office 2013):

    private void btnAddVariable_Click(object sender, EventArgs e) { Globals.ThisAddIn.Application.ActiveDocument.Variables.Add(tbVariable.Text, tbVariable.Text); } private void btnQueryVariable_Click(object sender, EventArgs e) { try {

    'pass the variable name to the get_Item function Variable v = Globals.ThisAddIn.Application.ActiveDocument.Variables.get_Item(tbVariable.Text); // <- If document variable doesn't exists, it throws COMException MessageBox.Show(v.Value); } catch (System.Exception) { MessageBox.Show("There is no such variable!"); } }


    Best regards

    Fei

    Tuesday, March 11, 2014 6:04 AM
    Moderator
  • Hi Fei,

    I'm using VS 2010 and Office 2010. The code works for me, but did you get the:

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Source.DLL

    Message in Visual Studio output window?

    My question is how to eliminate this message (I know it is just a message and the code is proper, but I got hundreds of this).

    Thanks.

    Wednesday, March 12, 2014 9:10 AM
  • Hi etaiso,

    When an application is being debugged, the debugger gets notified whenever an exception is encountered.

    If you don’t want to show the exception message in the output window, you can disable it via setting the option: debug->option->Output Window->Exception message

    And article below may be helpful to understand the exception:
    What is a First Chance Exception?
    First and second chance exception handling

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 14, 2014 9:59 AM
    Moderator