![](/img/trans.png)
[英]How do I unlock a content control using the OpenXML SDK in a Word 2010 document?
[英]Replacing content in Word 2010 Textboxes using OpenXML
使用Open XML SDK,我已經成功地以編程方式在Word文檔中查找書簽或文本字符串並插入了新內容。 我正在使用OpenXmlPowerTools.SearchAndReplacer進行文本搜索和替換,而本文的書簽答案是使用Open XML SDK替換Word文件中的書簽文本。
當書簽或我要替換的文本位於文本框內時,所有操作均將失敗。
為什么這兩種方法都無法在文本框中使用? 我試圖替換使用Texboxes中的內容進行布局的Word文檔,但我無法解決問題所在。
有人對可能出現的問題有任何建議嗎? 謝謝
我這樣做-在文本不存在多次運行的情況下,它適用於文本框(例如1字加粗)
Dim searchQuery = From tx In mainPart.Document.Body.Descendants(Of Text)()
Where tx.Text.Contains(replaceData.OldText)
Dim i As Integer
For i = 0 To searchQuery.Count - 1
searchQuery(i).Text = searchQuery(i).Text.Replace(replaceData.OldText, replaceData.NewText)
Next
這是其中包含單詞test的簡單文本框的XML:
<w:pict xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
<v:stroke joinstyle="miter" />
<v:path gradientshapeok="t" o:connecttype="rect" />
</v:shapetype>
<v:shape id="_x0000_s1027" style="position:absolute;margin-left:0;margin-top:0;width:186.35pt;height:110.6pt;z-index:251660288;mso-width-percent:400;mso-height-percent:200;mso-position-horizontal:center;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin" type="#_x0000_t202" xmlns:v="urn:schemas-microsoft-com:vml">
<v:textbox style="mso-fit-shape-to-text:t">
<w:txbxContent>
<w:p w:rsidR="00B558B5" w:rsidRDefault="00B558B5">
<w:proofErr w:type="gramStart" />
<w:r>
<w:t>test</w:t>
</w:r>
<w:proofErr w:type="gramEnd" />
</w:p>
</w:txbxContent>
</v:textbox>
</v:shape>
</w:pict>
您可以看到結構與在書簽中搜索文本時有所不同,因為文本框實際上是作為圖片存儲的。 如果您調整搜索算法以處理這種不同的結構,那么您應該能夠找到文本並將其替換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.