vbaexcelms-wordformatted-text

How to copy formatted text from excel to word using vba faster


Problem: I want to copy formated text from excel to word using a excelvba-script. The script copies the information dutifully but too slowly.

Can you give me a hint how to speed things up, please?

My approaches so far a documented in this dummy-document. The script assumes, that cells C1:C100 contain the formated text.

General information. I am writing a excelvba makro that copies formated text blocks to a word document. For each textblock there a two versions. The macro tracks the changes word-style.(Deletion: textcolor red and strikethrough etc.) and copies the result to a third colum. This part works like a charm. Then the third column is copied to a word document. This part works on my machine (i7-3770, ssd, 8 Gb Ram) but not on the poor souls machine who has to work with the script (amd Athlon 220) the production size is 700-1000 textblocks, with 100-1000 characters each.

option explicit
Sub start()
Dim wapp As Word.Application
Dim wdoc As Word.Document
Set wapp = CreateObject("word.application")

wapp.Visible = False
Application.ScreenUpdating = False

Set wdoc = wapp.Documents.Add
'Call copyFormattedCellsToWord(wdoc)
'Call copyFormattedCellsToWordForEach(wdoc)
'Call copyWholeRange(wdoc)
Call concatenateEverythingInAStringAndCopy(wdoc)
wapp.Visible = True
End Sub

'desired output-result (every cell in a new line and formatting preserved) meets the specs, but to slow

Sub copyFormattedCellsToWord(wdoc As Word.Document)

Dim counter As Long

Worksheets(1).Select
For counter = 1 To 100
        Worksheets(1).Range("C" & counter).Copy
        wdoc.Range(wdoc.Range.End - 1, wdoc.Range.End).PasteSpecial Placement:=wdInLine, DataType:=wdPasteHTML
Next counter

End Sub

'desired output-result, a tiny bit faster (might be only superstition), but still not fast enough

Sub copyFormattedCellsToWordForEach(wdoc As Word.Document)

Dim cell As Range

Worksheets(1).Select
For Each cell In Worksheets(1).Range("C1:C100")
        cell.Copy
        wdoc.Range(wdoc.Range.End - 1, wdoc.Range.End).PasteSpecial Placement:=wdInLine, DataType:=wdPasteHTML
Next cell

End Sub

'fast enough, but introduces a table in the word document and therefore
'doesn't meet the specs

Sub copyWholeRange(wdoc As Word.Document)

Worksheets(1).Range("C1:C100").Copy
wdoc.Range(wdoc.Range.End - 1, wdoc.Range.End).PasteSpecial Placement:=wdInLine, DataType:=wdPasteHTML

End Sub

'fast enought, looses the formatting


Sub concatenateEverythingInAStringAndCopy(wdoc As Word.Document)

Dim wastebin As String
Dim cell As Range

wastebin = ""
Worksheets(1).Select
For Each cell In Worksheets(1).Range("C1:C100")
        wastebin = wastebin & cell.Value
Next cell
Range("D1") = wastebin
Range("D1").Copy
wdoc.Range(wdoc.Range.End - 1, wdoc.Range.End).PasteSpecial Placement:=wdInLine, DataType:=wdPasteHTML

End Sub

Solution

  • Modify you copyWholeRange method in this way:

    Sub copyWholeRange(wdoc As Word.Document)
    
        Worksheets(1).Range("C1:C10").Copy
        wdoc.Range(wdoc.Range.End - 1, wdoc.Range.End).PasteSpecial Placement:=wdInLine, DataType:=wdPasteHTML
    
        wdoc.Tables(1).ConvertToText Separator:=wdSeparateByParagraphs
    End Sub