C# .NET&;Word-如何将纯文本转换为文档中的合并字段
我正在使用Visual Studio.NET(VB) 所以,如果你有一个C#中的解决方案,就先到这里() 我有一个包含文本的文档和一组要替换的单词: 我需要替换纯文本并替换为实际的合并字段C# .NET&;Word-如何将纯文本转换为文档中的合并字段,c#,.net,vb.net,ms-word,mergefield,C#,.net,Vb.net,Ms Word,Mergefield,我正在使用Visual Studio.NET(VB) 所以,如果你有一个C#中的解决方案,就先到这里() 我有一个包含文本的文档和一组要替换的单词: 我需要替换纯文本并替换为实际的合并字段 Dim replacements(,) As String = New String(,) {{"[firstname]", "$Field.FName"}, {"[lastname]", "$Field.LName"}, {"[ad
Dim replacements(,) As String =
New String(,) {{"[firstname]", "$Field.FName"},
{"[lastname]", "$Field.LName"},
{"[addr]", "$Field.Addr.St"},
{"[city]", "$Field.City"}}
Dim dotXLoc "c:/test/result.dotx"
Dim Fileformat As Microsoft.Office.Interop.Word.WdSaveFormat = Word.WdSaveFormat.wdFormatXMLTemplate 'SAVE AS DOT
Dim wordApp As Object = New Microsoft.Office.Interop.Word.Application()
Dim currentDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open(dotXLoc)
' Get bounds of the array.
Dim bound0 As Integer = replacements.GetUpperBound(0)
' Loop over all elements.
For i As Integer = 0 To bound0
' Get element.
Dim FieldFind As String = replacements(i, 0)
Dim FieldReplace As String = replacements(i, 1)
'<<< CODE HERE TO REPLACE TEXT WITH MERGEFIELD >>>
Next
currentDoc.SaveAs(dotXLoc & " v2.dotx", Fileformat)
currentDoc.Close()
wordApp.Quit()
Dim替换(,)作为字符串=
新字符串(,){{“[firstname]”,“$Field.FName”},
{“[lastname]”,“$Field.LName”},
{“[addr]”,“$Field.addr.St”},
{“[城市],“$Field.city”}
Dim dotXLoc“c:/test/result.dotx”
Dim文件格式为Microsoft.Office.Interop.Word.WdSaveFormat=Word.WdSaveFormat.wdFormatXMLTemplate“另存为点”
Dim wordApp As Object=新的Microsoft.Office.Interop.Word.Application()
将currentDoc设置为Microsoft.Office.Interop.Word.Document=wordApp.Documents.Open(dotXLoc)
'获取数组的边界。
Dim bound0作为整数=替换。GetUpperBound(0)
'在所有元素上循环。
对于i,作为整数=0到边界0
'获取元素。
Dim FieldFind As String=替换项(i,0)
尺寸字段替换为字符串=替换(i,1)
'>
下一个
currentDoc.SaveAs(dotXLoc和“v2.dotx”,文件格式)
currentDoc.Close()
wordApp.Quit()
以下是最终结果
Public sub main()
Dim rtfLoc = "c:/temp.rtf"
Dim dotXLoc = "c:/temp.dotx"
Dim Fileformat As Microsoft.Office.Interop.Word.WdSaveFormat = Word.WdSaveFormat.wdFormatXMLTemplate
Dim wordApp As Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim currentDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open(rtfLoc)
TextToMergeField(currentDoc)
currentDoc.SaveAs(dotXLoc, Fileformat)
currentDoc.Close()
wordApp.Quit()
End Sub
我们从main函数调用TextToMergeField(currentDoc)
通过这种方式,我们可以循环浏览多个文档并替换所有实例
Private Sub TextToMergeField(ByRef currentdoc As Word.Document)
Dim rng As Word.Range
Dim replacements(,) As String =
New String(,) {{"[firstname]", "$Field.FName"},
{"[lastname]", "$Field.LName"},
{"[addr]", "$Field.Addr.St"},
{"[city]", "$Field.City"}}
Dim bound0 As Integer = replacements.GetUpperBound(0)
currentdoc.Activate()
For i As Integer = 0 To bound0
rng = currentdoc.Range
With rng.Find
.Text = replacements(i, 0)
Do While .Execute(Replace:=WdReplace.wdReplaceOne)
currentdoc.Fields.Add(rng, WdFieldType.wdFieldMergeField, replacements(i, 1))
Loop
End With
Next
End Sub
希望这对别人有帮助