julien1h
julien1h

Reputation: 85

How can I keep line breaks from Excel to Word?

I have this code

Dim i As Integer
Dim word_fichier As Document
Dim MaFeuille As Worksheet
Dim nb_lines As Integer

    For i = 14 To nb_lines
        With word_fichier.Range.Find
            .Text = MaFeuille.Cells(i, 1)
            .Replacement.Text = MaFeuille.Cells(i, 2)
            .Execute Replace:=2
        End With
    Next i

This code is good but if I have a cell like that

hello
i am the
annoying string

The .replacement.text dont keep line breaks.

Do you have any ideas?

Upvotes: 1

Views: 516

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57743

Excel uses just a line feed vbLf in its cells.

If you want a real line break in Word you need to convert that into vbCrLf for a paragraph break Enter or vbVerticalTab for a manual line break Shift+Enter:

.Replacement.Text = Replace$(MaFeuille.Cells(i, 2), vbLf, vbCrLf)

Upvotes: 2

Related Questions