none
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.
    https://blogs.msmvps.com/wordmeister/2013/01/25/content-from-autotext-open-xml-sdk/

    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()
            CreateHeaderFromAutoText()
        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 = _
                                      sourcePkg.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() = _
                        "HeaderTestWithGraphic").FirstOrDefault()
                    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(). _
                        StyleDefinitionsPart
                    Dim hStyle As Style = _
                        glossStyle.Styles.Descendants(Of Style).Where( _
                        Function(s As Style) s.StyleId.Value.ToString() = _
                        "Header").FirstOrDefault()
                    Dim paraProps As StyleParagraphProperties = _
                        hStyle.StyleParagraphProperties
                    Dim runProps As StyleRunProperties = _
                        hStyle.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 = _
                            targetPkg.MainDocumentPart
                        Dim docStyle As StyleDefinitionsPart = _
                             mainTargetPart.StyleDefinitionsPart
                        Dim hTargetStyle As Style = _
                            docStyle.Styles.Descendants(Of Style).Where( _
                            Function(s As Style) s.StyleId.Value.ToString() = _
                            "Header").FirstOrDefault()
                        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"}
                            docStyle.Styles.Append(hTargetStyle)
                        End If
                        Dim paraTargetProps As StyleParagraphProperties = _
                            hTargetStyle.StyleParagraphProperties
                        Dim runTargetProps As StyleRunProperties = _
                            hTargetStyle.StyleRunProperties
                        paraTargetProps.InnerXml = paraProps.InnerXml
                        runTargetProps.InnerXml = runProps.InnerXml
    
                        Dim hPart As HeaderPart = _
                            mainTargetPart.HeaderParts.FirstOrDefault()
                        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( _
                                             sourceBlip.Embed)
                                Dim imgTargetPart As ImagePart = _
                                    hPart.AddImagePart(imgPart.ContentType)
                                imgTargetPart.FeedData(imgPart.GetStream())
                                Dim targetBlip As Blip = h.Descendants( _
                                    Of Blip).Where( _
                                    Function(b As Blip) b.Embed.Value = _
                                    sourceBlip.Embed.Value).FirstOrDefault()
                                targetBlip.Embed = hPart.GetIdOfPart(imgTargetPart)
                            Next
                        End If
                    End Using
                Else
                    Console.WriteLine("No Glossary part found.")
                    Console.ReadLine()
                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 = _
                wdPkgTarget.MainDocumentPart.GetIdOfPart(newHeaderPart)
            newHeaderPart.Header = New Header()
            Dim hr As HeaderReference = New HeaderReference With _
                {.Type = HeaderFooterValues.Default, .Id = hrID}
            Dim docTarget As Document = _
                wdPkgTarget.MainDocumentPart.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