none
VSTO Ribbon runtime error HResult=0x80131534 RRS feed

  • Question

  • Hi,

    Visual Studio Community 2017, Windows 10, Windows and Office (CTR) fully updated.

    Writing an Add-in for MS Project. Have simply created a new project, added Ribbon designer and .xml. Test time and after clicking the button in Project I get:

    System.TypeInitializationException
      HResult=0x80131534
      Message=The type initializer for 'TaskUpdate.TaskUpdateMain' threw an exception.
      Source=TaskUpdate
      StackTrace:
       at TaskUpdate.TaskUpdateMain.UpdateTasks() in C:\Visual Studio Projects\TaskUpdate\TaskUpdate\TaskUpdateMainCode.vb:line 6
       at TaskUpdate.PR_Ribbon_XML.UpdateTasksButton_Click(IRibbonControl control) in C:\Visual Studio Projects\TaskUpdate\TaskUpdate\PR-Ribbon-XML.vb:line 38
    Inner Exception 1:
    InvalidCastException: Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'.

    This is Project 2016 and Office 2016 from MSDN.

    The TaskUpdate code gets called fine:

    <Runtime.InteropServices.ComVisible(True)> _
    Public Class PR_Ribbon_XML
        Implements Office.IRibbonExtensibility
        Private ribbon As Office.IRibbonUI
        Public Sub New()
        End Sub
        Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
            Return GetResourceText("TaskUpdate.PR-Ribbon-XML.xml")
        End Function
    #Region "Ribbon Callbacks"
        'Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226
        Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
            Me.ribbon = ribbonUI
        End Sub

        Public Sub UpdateTasksButton_Click(ByVal control As Office.IRibbonControl)  ', ByVal isPressed As Boolean)
            UpdateTasks()    'Error happens here
        End Sub
    #End Region

    Module TaskUpdateMain
        Public ProjectApp As New MSProject.Application

        Public Sub UpdateTasks()
            MsgBox("Hello World")
        End Sub
    End Module

    Once this runs (no error messages in build) I can convert my VBA code and I'm away.

    Can anyone help please?

    Many thanks,

    Rod


    Rod Gill
    Author of the one and only Project VBA Book and VBA developer.
    www.project-systems.co.nz

    Thursday, May 24, 2018 3:09 AM

Answers

  • Hello Rod Gil,

    I would suggest you get current application instance instead of creating a new application instance.

    Please try

    Public ProjectApp As MSProject.Application=Globals.ThisAddIn.Application

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rod Gill Friday, May 25, 2018 2:56 AM
    Friday, May 25, 2018 1:30 AM

All replies

  • Hello Rod Gill,

    What't the result if you delete "Public ProjectApp As New MSProject.Application" from TaskUpdateMain Module?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 24, 2018 9:09 AM
  • Hi Terry,

    Good catch, that works. However if I move that line to a new Module and use ProjectApp in the original module on clicking the button I get:

    System.TypeInitializationException
      HResult=0x80131534
      Message=The type initializer for 'TaskUpdate.GlobalVariables' threw an exception.

    Inner Exception 1:
    InvalidCastException: Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'.

    My previous add-ins have all used Public ProjectApp As New MSProject.Application in my main module. Can't access Project objects without it.

    Tried adding the ProjectApp line in the Sub and runtime error is:

    System.InvalidCastException
      HResult=0x80004002
      Message=Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'.
      Source=mscorlib
      StackTrace:
       at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
       at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
       at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at TaskUpdate.TaskUpdateMain.UpdateTasks() in C:\@ProjectSystems\Visual Studio Projects\TaskUpdate\TaskUpdate\TaskUpdateMainCode.vb:line 3
       at TaskUpdate.PR_Ribbon_XML.UpdateTasksButton_Click(IRibbonControl control) in C:\@ProjectSystems\Visual Studio Projects\TaskUpdate\TaskUpdate\PR-Ribbon-XML.vb:line 38

    Whats confusing is the 

    Message=Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.MSProject.ApplicationClass'

    ApplicationClass reference. The Reference for an MSProject Add-in is Microsoft.Office.Interop.MSProject.Application

    Dim New MSProject.ApplicationClass isn't allowed as assembly is configured to embed interop types

    Thanks for your help.


    Rod Gill
    Author of the one and only Project VBA Book and VBA developer.
    www.project-systems.co.nz

    Thursday, May 24, 2018 9:33 PM
  • Hello Rod Gil,

    I would suggest you get current application instance instead of creating a new application instance.

    Please try

    Public ProjectApp As MSProject.Application=Globals.ThisAddIn.Application

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rod Gill Friday, May 25, 2018 2:56 AM
    Friday, May 25, 2018 1:30 AM
  • That did it. Must have copied the wrong code from somewhere.

    Many thanks,


    Rod Gill
    Author of the one and only Project VBA Book and VBA developer.
    www.project-systems.co.nz

    Friday, May 25, 2018 2:56 AM