使用VB.NET在Excel Addin中检索CustomXMLParts

时间:2014-05-23 19:44:46

标签: vb.net excel visual-studio-2012 add-in

我需要一点帮助,使用VB.NET检索存储在Excel Addin中的CustomXMLPart中的值。我已经搜索过,并且没有发现很多细节。根据我的发现,我所拥有的代码应该有效。起初,我认为我的xml部分没有添加,所以要坚持跨会话,但我在(3)之前和之后(4)添加我的自定义xml部分显示了CustomXMLParts集合的计数,并且计数确实增加了一。我在打开保存的Excel工作簿时也显示了计数,并且在将我的CustomXMLPart添加到集合后它是相同的数字(4)。这是以下相关代码。任何帮助将不胜感激。需要更多信息,请告诉我。

在Excel插件中,我有一个弹出窗口,我要求用户输入,这是我需要保留的信息。在后面的代码中,我创建了xml并添加到集合中。

代码:

    Dim workbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
    Dim xml As String

    xml = "<?xml version=""1.0"" encoding=""utf-8"" ?>" _
        & "<refreshViewPointData xmlns=""http://refreshviewpointdata.com"">" _
        & "<dataReference>" _
        & "<system>" & cboSystem.Text & "</system>" _
        & "<library>" & cboLibraries.Text & "</library>" _
        & "<view>" & txtObject.Text & "</view>" _
        & "<headers>" & chkInclColumnHdrs.Checked.ToString() & "</headers>" _
        & "<numOfRecords>" & txtRowCount.Text & "</numOfRecords>" _
        & "<reference>" & txtReference.Text & "</reference>" _
        & "</dataReference>" _
        & "</refreshViewPointData>"

    workbook.CustomXMLParts.Add(xml, System.Type.Missing)

在ThisAddIn.vb文件的ThisAddIn_Startup()方法中,我尝试检索CustomXMLPart。我从ThisAddIn_Startup()调用RetrieveCustomXMLPart()方法。

RetrieveCustomXMLParts()的代码:

    Dim parts As Microsoft.Office.Core.CustomXMLParts

    parts = Application.ActiveWorkbook.CustomXMLParts.SelectByNamespace("http://refreshviewpointdata.com")

    If parts.Count > 0 Then
        RefreshData(parts.ToString())
    End If

RefreshData()的代码:

    Dim r As New RibbonViewPoint
    Dim viewXMLPart As New XmlDocument

    Dim system, library, sObject, reference As String
    Dim headers As Boolean
    Dim numRecords As Integer

    'Load the xml from the string.
    viewXMLPart.LoadXml(part)

    'Retrieve the values from the xml document.
    system = viewXMLPart.SelectSingleNode("/dataReference/system").Value
    library = viewXMLPart.SelectSingleNode("/dataReference/library").Value
    sObject = viewXMLPart.SelectSingleNode("/dataReference/view").Value
    headers = CType(viewXMLPart.SelectSingleNode("/dataReference/headers").Value, Boolean)
    numRecords = CType(viewXMLPart.SelectSingleNode("/dataReference/numOfRecords").Value, Integer)
    reference = viewXMLPart.SelectSingleNode("/dataReference/reference").Value

    'Call method to run the object to refresh the data.
    r.RunSelectedObject(system, library, sObject, headers, reference, numRecords)

在RetrieveCustomXMLPart()方法中,SelectByNamespace()方法没有返回我的CustomXMLPart,它明显具有与我传入的名称相同的名称空间。任何人都知道错误是什么?

此外,如果有人对其他事情有任何见解,我也不会理解这也会很棒。在RefreshData()方法中,我将viewXMLPart变量作为XMLDocument来加载数据并从那里获取值。之前,我将它定义为&#34; Dim viewXMLPart As New Microsoft.Office.Core.CustomXMLPart&#34;,它一直给我一个语法错误,说明&#34;&#39; Microsoft.Office.Core.CustomXMLPartClass .Friend Sub New()&#39;在这种情况下无法访问,因为它是朋友。&#34;

感谢!!!

1 个答案:

答案 0 :(得分:1)

找到解决方案。以下是有用的。

    Dim workbook = Application.ActiveWorkbook
    Dim customXMLParts = workbook.CustomXMLParts.SelectByNamespace("urn:viewpoint-refresh")
    Dim customXMLPart = customXMLParts.Cast(Of CustomXMLPart)().FirstOrDefault()