OpenXML equivalent of Word VBA AutoTextEntry.Insert RRS feed

  • Question

  • I want to insert the content of an autotext entry into a document. I am working on a test project that is attempting to reproduce the Word VBA equivalent of:


    ActiveDocument.AttachedTemplate.AutoTextEntries.Item(strName).Insert Where:=Selection.Range, RichText:=True

    What I have so far is:


    void Create(string path)
    // open document template (*.odt)
    WordprocessingDocument doc = WordprocessingDocument.Open(path, true);
    // create a GlossaryDocument/AutoText object

    GlossaryDocumentPart glossary = doc.MainDocumentPart.GlossaryDocumentPart;


    However, from reading the API documentation, I cannot see a clear path to the collection of Glossary entries, nor can I see how to differentiate between AutoText and other GlossaryDocuments.


    Any thoughts?

    - john. 

    Tuesday, June 24, 2008 6:58 PM

All replies

  • Hi
    This is an old question, but I'll think this info could be usefull for someone anyway.

    This link provides an excellent example of how this could be done.

    If this link is broken, this is the code for a module in VB.NET with the provided functions:

    Imports DocumentFormat.OpenXml.Drawing
    Module Module1
        Sub Main()
        End Sub
        Private Sub CreateHeaderFromAutoText()
            Dim targetPath As String = "C:\Test\AddHeaderFormAT_NoHeader.docx" 
            Dim sourcePath As String = "C:\Test\TestOXML_BB.dotx"
            Using sourcePkg As WordprocessingDocument = _
                  WordprocessingDocument.Open(sourcePath, False)
                Dim mainSourcePart As MainDocumentPart = _
                If (mainSourcePart.GetPartsCountOfType( _
                                   Of GlossaryDocumentPart)() > 0) Then
                    'Read the header information from the AutoText
                    Dim glossDoc As GlossaryDocument = _
                        mainSourcePart.GetPartsOfType( _
                        Of GlossaryDocumentPart).FirstOrDefault().GlossaryDocument
                    Dim glossPart As DocPart = glossDoc.Descendants( _
                        Of DocPart).Where(Function(g As DocPart)  _
                        g.DocPartProperties.DocPartName.Val.ToString() = _
                    Dim headerXML As String = glossPart.DocPartBody.InnerXml
                    'Read the style information for the 'Header' style 
                    'stored in the AutoText
                    'In this example we're assuming that's the only 
                    'style used to format the entry
                    Dim glossStyle As StyleDefinitionsPart = _
                        mainSourcePart.GetPartsOfType( _
                        Of GlossaryDocumentPart).FirstOrDefault(). _
                    Dim hStyle As Style = _
                        glossStyle.Styles.Descendants(Of Style).Where( _
                        Function(s As Style) s.StyleId.Value.ToString() = _
                    Dim paraProps As StyleParagraphProperties = _
                    Dim runProps As StyleRunProperties = _
                    'Check for any linked graphics in the header
                    Dim sourceBlips As IEnumerable( _
                        Of DocumentFormat.OpenXml.Drawing.Blip) = _
                        glossPart.DocPartBody.Descendants( _
                        Of DocumentFormat.OpenXml.Drawing.Blip)()
                    Using targetPkg As WordprocessingDocument = _
                          WordprocessingDocument.Open(targetPath, True)
                        Dim mainTargetPart As MainDocumentPart = _
                        Dim docStyle As StyleDefinitionsPart = _
                        Dim hTargetStyle As Style = _
                            docStyle.Styles.Descendants(Of Style).Where( _
                            Function(s As Style) s.StyleId.Value.ToString() = _
                        If hTargetStyle Is Nothing Then
                            hTargetStyle = New Style With { _
                                          .StyleId = "Header", _
                                          .StyleParagraphProperties = _
                                            New StyleParagraphProperties, _
                                          .StyleRunProperties = _
                                             New StyleRunProperties, _
                                          .Default = 1, _
                                          .Type = StyleValues.Paragraph}
                            hTargetStyle.StyleName = _
                                New StyleName With {.Val = "header"}
                        End If
                        Dim paraTargetProps As StyleParagraphProperties = _
                        Dim runTargetProps As StyleRunProperties = _
                        paraTargetProps.InnerXml = paraProps.InnerXml
                        runTargetProps.InnerXml = runProps.InnerXml
                        Dim hPart As HeaderPart = _
                        If (hPart Is Nothing) Then
                            hPart = AddHeaderPart(targetPkg)
                        End If
                        Dim h As Header = hPart.Header
                        h.InnerXml = headerXML
                        If sourceBlips.Count > 0 Then
                            Dim sourceBlip As _
                                DocumentFormat.OpenXml.Drawing.Blip = Nothing
                            For Each sourceBlip In sourceBlips
                                Dim imgPart As ImagePart = _
                                    glossDoc.GlossaryDocumentPart.GetPartById( _
                                Dim imgTargetPart As ImagePart = _
                                Dim targetBlip As Blip = h.Descendants( _
                                    Of Blip).Where( _
                                    Function(b As Blip) b.Embed.Value = _
                                targetBlip.Embed = hPart.GetIdOfPart(imgTargetPart)
                        End If
                    End Using
                    Console.WriteLine("No Glossary part found.")
                End If
            End Using
        End Sub
        'Create a new default header for the document without content
        'Content will be added in the calling procedure
        Private Function AddHeaderPart(ByVal wdPkgTarget As _
                         WordprocessingDocument) As HeaderPart
            Dim newHeaderPart As HeaderPart = _
                wdPkgTarget.MainDocumentPart.AddNewPart( _
                Of HeaderPart)()
            Dim hrID As String = _
            newHeaderPart.Header = New Header()
            Dim hr As HeaderReference = New HeaderReference With _
                {.Type = HeaderFooterValues.Default, .Id = hrID}
            Dim docTarget As Document = _
            docTarget.Body.Elements( _
                Of SectionProperties).LastOrDefault().InsertAt(hr, 0)
            Return newHeaderPart
        End Function
    End Module

    Best Regards Peter Karlström Midrange AB, Sweden

    Thursday, December 14, 2017 9:24 AM