none
How to get document variable from .docx? RRS feed

  • Question

  • Hi

    Please allow me that this is already posted in openxmldeveloper.org discussion forum. However I got no responce for my question. So I will post this forum again.

    Currently I'm making a program that reads paragraphs from Word2010 .docx document using VB.

    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing

    Dim docPath as String="..."
     
    Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(docPath, False)
     Dim body As Body = wordDoc.MainDocumentPart.Document.Body
     Dim child As DocumentFormat.OpenXml.OpenXmlElement
     For Each child In body.ChildElements
       If TypeOf child Is Paragraph Then
         Call p(child)
       End If
     Next
    End Using

    Sub p(ByVal p As Paragraph)
     ...
    End Sub

    This works fine. So I want to add a code that reads the document variable from wordDoc. The document variable is stored like this:

    <w:docVars>
     <w:docVar w:val="urn:oasis:names:tc:dita:xsd:basetopic.xsd" w:name="pid"/>
    </w:docVars>

    As a result one responded to me that wordDoc.MainDocumentPart.RootElement.Descendants<DocumentVariables>().ToList() should return the DocVars. However when I debugging the following code in the VS2010 the docVar seems to have no members while there is actually w:docVars.

    Dim docVar = wordDoc.MainDocumentPart.RootElement.Descendants(Of DocumentVariables).ToList()

    Are there any solutions?

    Thanks,

    Wednesday, October 5, 2011 9:16 AM

Answers

  • Hi tmakita,

     

    Thanks for posting in the MSDN Forum.

     

    You will not retrieve the tag “w:docVar” due to the worDoc.MainDocumentPart.RootElement will point to the document.xml in the docx. And the w:docVar is contained at Setting.xml. The following snippet will iterate all of the DocumentVariables in your document setting.

     

    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing
    
    Module Module1
    
        Sub Main()
            Dim wdWordProcess As WordprocessingDocument = WordprocessingDocument.Open("C:\*******\test.docx", True)
            Dim wdMainDocument As MainDocumentPart = wdWordProcess.MainDocumentPart
            Dim wdDocumentVariables As DocumentVariables = New DocumentVariables()
            Dim wdDocumentVariable As DocumentVariable = New DocumentVariable()
            Dim wdSettingPart As DocumentSettingsPart = wdMainDocument.DocumentSettingsPart
    
            For Each wdDocumentVariables In wdSettingPart.Settings.Descendants(Of DocumentVariables).ToList()
                For Each wdDocumentVariable In wdDocumentVariables.Descendants(Of DocumentVariable).ToList()
                    Console.WriteLine(wdDocumentVariable.Name.Value & " => " & wdDocumentVariable.Val.Value)
                Next
            Next
            Console.ReadKey()
        End Sub
    
    End Module
    

     

    I hope my reply will solve your issue.

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by tmakita Friday, October 7, 2011 1:16 PM
    Friday, October 7, 2011 4:00 AM
    Moderator

All replies

  • Hi tmakita,

     

    Thanks for posting in the MSDN Forum.

     

    You will not retrieve the tag “w:docVar” due to the worDoc.MainDocumentPart.RootElement will point to the document.xml in the docx. And the w:docVar is contained at Setting.xml. The following snippet will iterate all of the DocumentVariables in your document setting.

     

    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing
    
    Module Module1
    
        Sub Main()
            Dim wdWordProcess As WordprocessingDocument = WordprocessingDocument.Open("C:\*******\test.docx", True)
            Dim wdMainDocument As MainDocumentPart = wdWordProcess.MainDocumentPart
            Dim wdDocumentVariables As DocumentVariables = New DocumentVariables()
            Dim wdDocumentVariable As DocumentVariable = New DocumentVariable()
            Dim wdSettingPart As DocumentSettingsPart = wdMainDocument.DocumentSettingsPart
    
            For Each wdDocumentVariables In wdSettingPart.Settings.Descendants(Of DocumentVariables).ToList()
                For Each wdDocumentVariable In wdDocumentVariables.Descendants(Of DocumentVariable).ToList()
                    Console.WriteLine(wdDocumentVariable.Name.Value & " => " & wdDocumentVariable.Val.Value)
                Next
            Next
            Console.ReadKey()
        End Sub
    
    End Module
    

     

    I hope my reply will solve your issue.

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by tmakita Friday, October 7, 2011 1:16 PM
    Friday, October 7, 2011 4:00 AM
    Moderator
  • Hi Tom,

    Thank you for your reply.

    Your code worked fine. I could get the document variable successfully.

    I appreciate your help!

    Thanks,

    Friday, October 7, 2011 1:15 PM