Replace bookmark text in Word file using Open XML SDK

前端 未结 11 1997
眼角桃花
眼角桃花 2020-12-01 08:24

I assume v2.0 is better... they have some nice \"how to:...\" examples but bookmarks don\'t seem to act as obviously as say a Table... a bookmark is defined by two

11条回答
  •  失恋的感觉
    2020-12-01 08:55

    Here is how I do it in VB.NET:

    For Each curBookMark In contractBookMarkStarts
    
          ''# Get the "Run" immediately following the bookmark and then
          ''# get the Run's "Text" field
          runAfterBookmark = curBookMark.NextSibling(Of Wordprocessing.Run)()
          textInRun = runAfterBookmark.LastChild
    
          ''# Decode the bookmark to a contract attribute
          lines = DecodeContractDataToContractDocFields(curBookMark.Name, curContract).Split(vbCrLf)
    
          ''# If there are multiple lines returned then some work needs to be done to create
          ''# the necessary Run/Text fields to hold lines 2 thru n.  If just one line then set the
          ''# Text field to the attribute from the contract
          For ptr = 0 To lines.Count - 1
              line = lines(ptr)
              If ptr = 0 Then
                  textInRun.Text = line.Trim()
              Else
                  ''# Add a 
    run/text component then add next line newRunForLf = New Run(runAfterBookmark.OuterXml) newRunForLf.LastChild.Remove() newBreak = New Break() newRunForLf.Append(newBreak) newRunForText = New Run(runAfterBookmark.OuterXml) DirectCast(newRunForText.LastChild, Text).Text = line.Trim curBookMark.Parent.Append(newRunForLf) curBookMark.Parent.Append(newRunForText) End If Next Next

提交回复
热议问题