我有一个使用Microsoft Word 2016 / Interop界面调用replace
函数的应用程序。这样,应用程序会将“ Customername”之类的文本替换为客户的实际姓名。当然,这不是最好的方法,但这就是过去构建应用程序的方式。现在,我不得不对命名函数遇到的格式进行一些调整。
被替换的文本之一是“ [ TELNR ]”,被所有已知的客户编号替换,每行一个编号。您可以在屏幕截图的顶部,将空白行作为vbCr
插入在空白页面上效果很好。相同的调用函数不会在表中添加新行(左侧第二页),甚至不会触摸文本框(右侧第二页)。
我尝试将换行符传递为^p
,\n\r
,\n
,vbClRf
/ vbNewLine
,vbCr
。这些都不起作用。
我在Google或SO上找不到任何有用的东西。您是否有可能导致甚至解决问题的想法或原因?
#Region "Sample Strings"
Dim sample1 As String = "t:022220000000" & vbNewLine & "h:022221111111" & vbNewLine & ":0222222222222" & vbNewLine & "f:022223333333"
Dim sample2 As String = "t:022220000000" & vbCr & "h:022221111111" & vbCr & ":0222222222222" & vbCr & "f:022223333333"
Dim sample3 As String = "t:022220000000" & "^p" & "h:022221111111" & "^p" & ":0222222222222" & "^p" & "f:022223333333"
Dim sample4 As String = "t:022220000000" & "\n" & "h:022221111111" & "\n" & ":0222222222222" & "\n" & "f:022223333333"
Dim sample5 As String = "t:022220000000" & "\n\r" & "h:022221111111" & "\n\r" & ":0222222222222" & "\n\r" & "f:022223333333"
Dim sample6 As String = "t:022220000000" & vbCrLf & "h:022221111111" & vbCrLf & ":0222222222222" & vbCrLf & "f:022223333333"
#End Region
Private Sub replaceText(file As Microsoft.Office.Interop.Word.Document, find As String, replaceWith As String)
// First line was commented out for the ^p try
If replaceWith.Contains("^") Then replaceWith = replaceWith.Replace("^", "")
file.Content.Find.Execute(FindText:=find, ReplaceWith:=replaceWith, Replace:=Word.WdReplace.wdReplaceAll)
End Sub