none
Warning Variable 'addIn' is used before it has been assigned a value. A null reference exception could result at runtime._ RRS feed

  • Question

  • Hello my code displays a warning

    Warning Variable 'addIn' is used before it has been assigned a value. A null reference exception could result at runtime.           

    This is my code

    Public Function NewWorkbook(ByVal templatePath As String, Optional ByVal DM As Boolean = True) As ExcelWorkbook Dim pathObject As Object = Convert.ChangeType(templatePath, TypeCode.Object, Globalization.CultureInfo.CurrentCulture) Dim newBook As ExcelWorkbook = New ExcelWorkbook(m_application.Workbooks.Add(pathObject), Me) Dim addIn As Microsoft.Office.Core.COMAddIn = addIn.Application("NetDocuments.Client.ExcelAddIn") Dim automationObject As NetDocuments.Client.Common.Infrastructure.NdOfficeAddInUtilities automationObject = addIn.Object Dim profile As NetDocuments.Client.Common.Models.COM.NdProfileAttributesCollection profile = New NetDocuments.Client.Common.Models.COM.NdProfileAttributesCollection Dim strFileName As String strFileName = "" Try newBook = OpenWorkbook(newBook) If InStr(newBook.Workbook.Application.ActiveWorkbook.Name, "Blank", CompareMethod.Text) <> 1 Then RaiseEvent WorkbookAdded(newBook.Workbook.Name) End If

    ' TFS-2290 Excel not in Automation mode If m_application.Visible = True Then m_application.ActiveWindow.Activate() ' ensure focus is set back to application from DM If DM Then '' default to nd Home ' automationObject.ShowSaveDialog(, profile, strFileName, "", "", "") automationObject.ShowSaveDialog(, profile, strFileName, "", "", "", True) If NetDocuments.Client.Presentation.SaveResult.Saved Then 'save ElseIf NetDocuments.Client.Presentation.SaveResult.Canceled Then 'cancel End If End If End If Return newBook Catch ex As Exception Logger.Log(Me.GetType(), ex) Throw End Try End Function



    • Edited by dd_ Friday, June 5, 2015 3:57 AM update
    Friday, June 5, 2015 3:56 AM

Answers

  • Hi dd,

    >> Warning Variable 'addIn' is used before it has been assigned a value. A null reference exception could result at runtime

    It was caused that you did not set the value of addIn before you used it. Based on your code, did you want to get the COMAddIn of the excel application. If so, I suggest you try the code below:

        Private Sub Addin_Click(sender As Object, e As EventArgs) Handles Addin.Click
            'Dim addIn As Microsoft.Office.Core.COMAddIn
            Dim oXL As Microsoft.Office.Interop.Excel.Application
            oXL = New Excel.Application
            oXL.Visible = True
            oXL.Workbooks.Add(Excel.XlSheetType.xlWorksheet)
            Dim addin As Microsoft.Office.Core.COMAddIn = oXL.COMAddIns.Item("Edward") 'the name of addin
        End Sub

    You could refer the link below for more information.

    # VSTO Add-ins, COMAddIns and RequestComAddInAutomationService
    http://blogs.msdn.com/b/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx

    Best Regards,

    Edward


    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.


    Monday, June 8, 2015 9:27 AM

All replies

  • Hi dd,

    >> Warning Variable 'addIn' is used before it has been assigned a value. A null reference exception could result at runtime

    It was caused that you did not set the value of addIn before you used it. Based on your code, did you want to get the COMAddIn of the excel application. If so, I suggest you try the code below:

        Private Sub Addin_Click(sender As Object, e As EventArgs) Handles Addin.Click
            'Dim addIn As Microsoft.Office.Core.COMAddIn
            Dim oXL As Microsoft.Office.Interop.Excel.Application
            oXL = New Excel.Application
            oXL.Visible = True
            oXL.Workbooks.Add(Excel.XlSheetType.xlWorksheet)
            Dim addin As Microsoft.Office.Core.COMAddIn = oXL.COMAddIns.Item("Edward") 'the name of addin
        End Sub

    You could refer the link below for more information.

    # VSTO Add-ins, COMAddIns and RequestComAddInAutomationService
    http://blogs.msdn.com/b/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx

    Best Regards,

    Edward


    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.


    Monday, June 8, 2015 9:27 AM
  • Many thanks Edward. that worked!

    best regards

    --dd

    Monday, June 8, 2015 10:35 PM