简体   繁体   中英

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

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

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

词对象库

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM