JeroenDV
JeroenDV

Reputation: 135

Insert a line break from Excel VBA in a Word document

From my Excel Sheet I open up a Word document. Excel generates some text within the document. Between the text I want a LineBreak. The code results in an error message stating: parameter value out of acceptable range

Using the following documentation this should be possible using InsertBreak.

Sub InsertLineBreak()
    Set wrd = CreateObject("Word.Application") 'Open Word
    Set objDoc = wrd.Documents.Add 'Add new document
    Set objSelection = wrd.Selection 'Select this document

    objSelection.InsertBreak Type:=wdLineBreak 'Insert Break
End Sub

Upvotes: 3

Views: 7808

Answers (2)

JeroenDV
JeroenDV

Reputation: 135

For future readers:

In this case the Microsoft Word 16.0 Object Library was missing. It can be added Tools-> References and selecting the corresponding Library

Word Object Lib

Upvotes: 1

Daniel
Daniel

Reputation: 954

It works perfectly fine for me, are you sure you have added correct reference?

Try objSelection.InsertBreak Type:=6 or add Microsoft Word 16.0 Object Library reference, or declare a globar variable wdLineBreak

Upvotes: 3

Related Questions