VBA如何将格式化的文本从Excel传递到MS Word

我已经要求在哪里需要比较Excel中使用VBA的两个文本样本。 由于每个文本样本在LEN中超过3000个字符,因此我使用UserForm.TextBox字段获取,将它们存储到stringvariables中。

接下来,我使用一个VBA代码,我在这里find' mikerickson ': VBA例程comp

比较文本。 问题是这个代码的输出被input到一个Cell:A1中。 由于文本样本每个包含3000个以上的字符,所以结果不会显示在A1中,因为excel限制了每个单元的字符数。

我想知道如何获得结果(所有格式如下划线和删除线)到一个MS Word文档。

目前,这是用于输出到单元格A1的代码。

strResult = ComparedText(strOne, strTwo, olStart, olLength, nwStart, nwLength) With outCell.Cells(1, 1) .Clear .Value = strResult For i = LBound(olStart) To UBound(olStart) If olStart(i) <> 0 Then With .Characters(olStart(i), olLength(i)).Font .ColorIndex = 3 .StrikeThrough = True End With End If Next i For i = LBound(nwStart) To UBound(nwStart) If nwStart(i) <> 0 Then With .Characters(nwStart(i), nwLength(i)).Font .ColorIndex = 4 .Underline = True End With End If Next i End With 

这里需要一个replace代码,它将strResultinput到一个与上面代码中的错误格式完全相同的格式的word文档中。

希望有人帮助。 提前致谢。

这将从单元格A1到Word。 如果单元格A1中的数据被截断,那么我们将不得不深入挖掘。

 Set wordy = CreateObject("Word.Application") wordy.Documents.Add Sheets(1).Range("A1").Copy wordy.Documents(1).ActiveWindow.Selection.PasteSpecial DataType:=wdPasteRTF wordy.Visible = True 'wordy.Quit Set wordy = Nothing 

如果我们可以在Word中插入strResult并执行格式化,那么这可能适用于你…

 Set wordy = CreateObject("Word.Application") wordy.Documents.Add wordy.Documents(1).ActiveWindow.Selection.InsertAfter Text:=strResult wordy.Documents(1).Range.Select wordy.Visible = True With wordy.Selection For i = LBound(olStart) To UBound(olStart) If olStart(i) <> 0 Then For j = olStart(i) To (olStart(i) + olLength(i) - 1) With .Characters(j).Font .ColorIndex = 3 .Strikethrough = True End With Next j End If Next i For i = LBound(nwStart) To UBound(nwStart) If nwStart(i) <> 0 Then For j = nwStart(i) To (nwStart(i) + nwLength(i) - 1) With .Characters(j).Font .ColorIndex = 4 .Underline = True End With Next j End If Next i End With Set wordy = Nothing 

这可能不是最有效的代码,但希望能让你朝着正确的方向前进。 我必须创buildj循环,因为它看起来像在Word中一样, Characters对象一次只能访问一个字符。