替换Word文件中的突出显示的文本

时间:2019-02-10 22:10:14

标签: vba templates ms-word blueprism

因此,此子项应将输入文档中突出显示的文本替换为输入集合中给定的另一个文本(我用蓝色棱柱表示),但由于结果是相同的突出显示文本,因此它不会做任何事情。 我不知道可能是什么问题,有什么想法吗?

Sub Main()
            Dim d As Object = GetDocument(handle, documentName)
            Dim r As Object = d.Range
            Dim i As Integer
            Dim current_text As String = Nothing
            Dim new_text As String = Nothing

            r.Find.Highlight = True
            r.Find.Forward = True

            For repeat As Integer = 0 To fields.Columns.count() - 1
                For index As Integer = 0 To fields.Rows.count() - 1
                    current_text = fields.Rows(index).Item(0)
                    new_text = collection.Rows(index).Item(repeat)

                    Do While r.Find.Execute(current_text)
                        If r.HighlightColorIndex = currentHighlight Then
                            r.Text = new_text
                            r.HighlightColorIndex = CInt(newHighlight)
                            r.Bold = newBold
                        End If
                        i = r.End


    r.Start = i

                Loop

            Next
            Next

End Sub

2 个答案:

答案 0 :(得分:0)

我已经使用Macro解决/确定了相同的问题。您可以为此使用以下代码,并根据需要进行修改,如此处所示,替换为word文档中的文本/标签。

Set myDoc = WordApp.Documents.Add(Template:=TemplateFilePath, NewTemplate:=False, DocumentType:=0)
With myDoc.Range.Find
                                    .text = [Text to be find]                                        
                                    .Replacement.text = [Replacement Text]                                        
                                    .Replacement.ClearFormatting
                                    .Replacement.Font.ColorIndex = wdRed
                                    .Forward = True
                                    .Wrap = wdFindContinue
                                    .Format = True
                                    .MatchCase = False
                                    .MatchWholeWord = False
                                    .MatchWildcards = False
                                    .MatchSoundsLike = False
                                    .MatchAllWordForms = False
                                    .Execute Replace:=wdReplaceAll
                            End With

答案 1 :(得分:0)

使用“实用程序字符串>以CSV格式获取集合”将Collection阶段的内容投射为文本。然后移除昏迷并以空格代替,例如(在c#中):

string correctString = collectionCSV.Replace(",", " ");  
amendedCSV = correctString;

这可以输出文本类型的数据项(即,以逗号分隔的文本类型中的集合的内容),然后您可以使用“ MS Word VBO>键入文本”进行引用,以将其写入Word文档中,无论您身在何处需要放置它们。您可能需要在对象的初始化页面上引用System.IO.dll和System.IO命名空间。