0
votes

I'm having trouble with Selection.EndKey Extend:=wdExtend.

Windows 7 64bit, Office 2013 with the following References checked in my Project References dialog:

• Visual basic For applications • Microsoft Word 15.0 Object Library • OLE Automation • Normal • Microsoft Office 15.0 Object Library

I open a Word document that consist of one sentence paragraphs (no periods at the ends). My desire is to select each paragraph and replace it with an entry from a predefined table.

Here is the original code:

For i = intFirstItemNo To intLastItemNo
  Selection.EndKey Extend:=wdExtend
  Selection.TypeText Text:=arlAdItem01(i)
  Selection.MoveRight unit:=wdCharacter, Count:=1
Next i

I then discovered that occasionally there will be a paragraph that is longer than the page width and wraps around to the next physical line. At this point the code above truncates the paragraph at the end of the line (unit:=wdLine is the default).

After much experimentation I've discovered that only the enumerations 5=wdLine & 6=wdStory work. 1 through 4 (1=wdCharacter, 2=wdWord, 3=wdSentence, 4=wdParagraph) produce the old "Run time error '4120'" message.

This is the line I want to use:

Selection.EndKey Unit:=wdParagraph, Extend:=wdExtend

My question is, does anyone know how to make the code work the way it's described to work in the Selection.EndKey Method (Word) entry of the Office Dev Center?

1

1 Answers

1
votes

Seems to be a bug in Word VBA. Has been al least since 2013 and in 2016.

I solved it with

Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1

Doesn't work if the paragraph is the last one.