locked
Throwing an Error from Power Point VBA Code in and catching in C# Console application RRS feed

  • Question

  • Hi,

    I could not find any suitable forum to ask my question. Please help me solve this.. I have goggled for long time by nothing useful came out.

    Here is my problem: I have a C# Console Application which opens a power point slide and then run some VBA Function / Macro defined in one of the Power Point plug in and then save it. Everything is running fine. Now I want to enable Error handling. So, I'll have a HANDLER block in my VBA Function and then throw the Error from there and catch it in the main C# application which is running it and then it will deal with the error accordingly. But I could not find a suitable way to interact with the C# application and inform it about the error. Can somebody please help?

    Example:

    'VBA Code In powerpoint.
    
    Sub TestSub(Optional control As IRibbonControl)
      
      Dim link As String
      Dim Text As String
      
      On Error GoTo HANDLER
    
      '.... Then code...
    
      Exit Sub
    
    HANDLER:
    
    	'Here I want to throw the error
    
    End Sub
    

     

    C# Code to call the Power Point:

     

    PowerPoint.Application oPP = new PowerPoint.Application();          
    oPP.Visible = MsoTriState.msoTrue;          
    PowerPoint.Presentations oPresSet = oPP.Presentations; 
    
    PowerPoint._Presentation _activePres = oPresSet.Open("PPT File Path",
                MsoTriState.msoFalse, MsoTriState.msoFalse,
                MsoTriState.msoTrue); 
    
    //Now run the 'TestSub' marco in the PPT
    
    object _retobj = RunMacro(oPP, new Object[] { "TestSub" });
    
    //Now, C# Code for RunMacro function:
    
     
    
    private object RunMacro(object oApp, object[] oRunArgs)
        {
          object _ret = null;
    
          try
          {
            _ret = oApp.GetType().InvokeMember("Run",
              System.Reflection.BindingFlags.Default |
              System.Reflection.BindingFlags.InvokeMethod,
              null, oApp, oRunArgs);
    
            if (null != _ret)
            {
              //Print Value
            }
            else
            {
              //Return NULL
            }
          }
          catch (Exception _exp)
          {
            //Here I want to catch the Exception
    		throw _exp;
          }
    
          return _ret;
        }  
    

     

    Please let me know if there is any other details I need provide..

    Thanks in advance..

    Saturday, February 19, 2011 4:10 AM

All replies

  • Hi ubk1004,

    Thanks for posting in the MSDN Forum.

    According to your description, you want to catch an exception which thrown by VBA via C#, and handle the exception by C#.

    If I have misunderstood anything, please feel free to let me know.

    I found a resource related to this issue, please try it. http://stackoverflow.com/questions/120579/expose-vsto-functionality-to-vba-w-o-local-admin

    I hope it helps.

    Have a great day,

    Tom Xu


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 21, 2011 7:50 AM
  • According to your description, you want to catch an exception which thrown by VBA via C#, and handle the exception by C#.

    Hi Tom,

     

    Thanks for the reply. There is a small problem. I want to throw the Exception from VBA code. Its a PowerPoint add in developed in Classic VBA code. So, from that add in I want to throw an exception and I want that to be caught in my C# code which has opened the PowerPoint using Office Automation technique.

    I have a code to open a powerpoint slide and call a function from the VBA add in which is loaded in the PowerPoint. Upto this point everything is fine. Now I want the function call inside a try catch block, so that if there is any exception thrown by the VBA Code then it can get caught. 

     

    I hope I am able to explain my need. Thanks again for helping.

    Wednesday, February 23, 2011 2:23 PM
  • Hi ubk1004,

    As far as I know, VBA use “On Error GoTo” to handle exception, so you can return the exception information as a string result of you VBA fuction. If you want to handle it ,you can return the exception as a string result of you macro, add-in can receive it.

    If I misunderstood anything, please feel free to let me know.

    Have a great day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 28, 2011 7:20 AM
  • Hi Tom,

     

    Thanks for looking into this. Thanks for the suggestion as well. I already thought about it but the problem was all the functions I wrote does not return only string, it can return Boolean and other data type too. So this solution will not work for me.

    Saturday, March 5, 2011 8:36 PM