none
Problem with Set and AddNode in CustomXMLPart RRS feed

  • Question

  • I'm getting a compile error "Expected function or variable" on the second .AddNode method in the following code and I don't understand why:

    Sub PracticeAdd_And_SetNode()
    Dim oDoc As Word.Document
    Dim oCustXMLPart As Office.CustomXMLPart
    Dim oNode 'As CustomXMLNode
      Set oDoc = ActiveDocument
      'Kill any existing XMLPart.
      On Error Resume Next
      oDoc.CustomXMLParts.SelectByNamespace("http://nsPractice.com").Item(1).Delete
      On Error GoTo 0
      'Create a basic XML Part
      Set oCustXMLPart = oDoc.CustomXMLParts.Add("<?xml version='1.0' encoding='utf-8'?><Root_Node xmlns='http://nsPractice.com'></Root_Node>")
        'I can add a node:
        oCustXMLPart.AddNode oCustXMLPart.SelectSingleNode("/ns0:Root_Node"), "Practice_Node1", , , , "Practice text"
        '... and then use set.
        Set oNode = oCustXMLPart.SelectSingleNode("/ns0:Root_Node[1]/strNodeName[1]")
        'But not this. When unsetted, it returns a compile error "Expected function or variable" and AddNode is highlighted. Why?
        'Set oNode = oCustXMLPart.AddNode(oCustXMLPart.SelectSingleNode("/ns0:Root_Node"), "Practice_Node2", , , , "Practice text")
    lbl_Exit:
      Exit Sub
    End Sub
    I can add a node using .AddNode and then I can set my oNode variable to the added node, but I can't seem to combine the two into one line of code. Am I missing something or is this just not possible? Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, November 21, 2012 1:14 PM

Answers

  • Hi Greg,

    Thanks for posting in the MSDN Forum.

    AddNode is a sub procedures  instead of a function. It has no return value. So

    Set oNode = oCustXMLPart.AddNode(oCustXMLPart.SelectSingleNode("/ns0:Root_Node"), "Practice_Node2", , , , "Practice text")

    Is a incorrect statement.

    I hope it can help you.

    Have a good day,

    Tom

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Greg Maxey Thursday, November 22, 2012 3:51 AM
    Thursday, November 22, 2012 2:53 AM
    Moderator

All replies

  • Hi Greg,

    Thanks for posting in the MSDN Forum.

    AddNode is a sub procedures  instead of a function. It has no return value. So

    Set oNode = oCustXMLPart.AddNode(oCustXMLPart.SelectSingleNode("/ns0:Root_Node"), "Practice_Node2", , , , "Practice text")

    Is a incorrect statement.

    I hope it can help you.

    Have a good day,

    Tom

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Greg Maxey Thursday, November 22, 2012 3:51 AM
    Thursday, November 22, 2012 2:53 AM
    Moderator
  • Tom,

    Just not possible.  Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Thursday, November 22, 2012 3:51 AM