Word 2016 VSTO Com加载项错误:对象引用未设置为对象的实例

时间:2016-11-14 16:19:36

标签: vb.net ms-word vsto

我有一个Visual Studio 2013 VSTO Word加载项目,在Visual Studio之外看起来可以正常工作。但是当我安装它并运行特定功能时,会产生错误。我添加了try catch,因为Word似乎是如此压制错误并且静静地看起来很好(尽管它没有)。

我的智慧以此结束。我不明白为什么它会从Visual Studio中正确运行而不是安装。其他以前编写的功能似乎工作正常。

对象引用未设置为对象的实例。    at FrRibbon.Support.AppStats.WriteRibbonUsage(String subApplication,String featureInvoked,String initials)

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Namespace Support

Public Module AppStats

    Public Sub WriteRibbonUsage(subApplication As String, featureInvoked As String, initials As String)

        Try
            Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("FRAppStatsConnectionString").ConnectionString)
                Using command As New SqlCommand("InsertRibbonUsage", connection)
                    command.CommandType = CommandType.StoredProcedure
                    command.Parameters.Add(New SqlParameter("subApplication", subApplication))
                    command.Parameters.Add(New SqlParameter("featureInvoked", featureInvoked))
                    command.Parameters.Add(New SqlParameter("initials", initials))
                    connection.Open()
                    command.ExecuteNonQuery()
                End Using
            End Using

        Catch ex As Exception
            Globals.ThisAddIn.Application.ActiveDocument.Range.Text = String.Concat(ex.Message, vbCrLf, ex.StackTrace, vbCrLf, ex.Source, vbCrLf, ex.InnerException)
        End Try

    End Sub

End Module
End Namespace

 Private Sub btnRemoveHyperlinks_Click(sender As Object, e As RibbonControlEventArgs) Handles btnRemoveHyperlinks.Click

    Support.AppStats.WriteRibbonUsage("Super Macro", "Remove Hyperlinks", Helpers.GetUserName())

    If Not Globals.ThisAddIn.CustomTaskPanes.Any(Function(c) c.Title = "Remove Hyperlinks") Then
        RemoveHyperlinksTaskPane = New CustomPane(RemoveHyperlinksControl, "Remove Hyperlinks", "removeHyperlinks", RemoveHyperlinksWindowList)
        RemoveHyperlinksTaskPane.Visibility(True, "right", 425)
        RemoveHyperlinksControl.PopulateHyperLinkListBox()
    Else
        RemoveHyperlinksTaskPane.Visibility(True, "right", 425)
        RemoveHyperlinksControl.PopulateHyperLinkListBox()
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

直到我将连接字符串添加到应用程序设置,并将其引用为:My.Settings.MyConnectionStringName,它才有效。我之前刚刚在app.config的ConnectionStrings部分手动添加了它。

我怀疑这与编译为VSTOAddinName.dll.config的app.config有关,这可能解释了为什么它在Visual Studio中工作,但在安装时没有。