MS Word function to add a range to a document

Suppose I want to add text at the end of a document and immediately access it as a Range object, so I can set some of its properties without affecting the previous text. Ideally, the Range.InsertAfter method will return a Range object that would be ideal for this, but it is not.

It seems to me that Word should know perfectly well which range determines the result of calling InsertAfter , but at first glance I need to calculate it “after the fact” from the length of the inserted text or in some other way.

So, I developed a simple approach. In the pseudo code (actually it is Delphi but I hope it will not interfere with VBA answers) what I do is

 ARange := Document.Range ARange.Text := 'AAA' AEnd := ARange.End - 1 // AEnd is an integer ARange.SetRange(AEnd, AEnd) ARange.Text := 'XXX' ARange.Bold := True 

and it seems that I can endlessly add blocks of text to the end of the document by repeating the second block of code.

Line

 ARange.SetRange(AEnd, AEnd) 

as I understand it, it seems that I built a new Range at the end of the existing one (as opposed to calling Collapse on the existing range), and works fine for the simple tests that I tried. But I'm not interested, I miss a trick somewhere. Is there a more direct way to add a range to a document and get a link to it?

PS: I should have made it clearer that I was trying to do this without using the Selection object (for a number of reasons, including the fact that you can only have one of them at a time).

+5
source share
4 answers

There are various ways to get the range at the end of a document. You found it, but, as you say, it's a little cool. My preferences:

 Word.Range rngEndOfDoc = Document.Content; //Content returns a Range object and is a property, not a method like Range() rngEndOfDoc.Collapse(Word.WdCollapseDirection.wdCollapseEnd); rngEndOfDoc.Text = "Text at the end of the document"; 

Resetting a range conceptually resembles pressing the right (or left) arrow key when you have a choice. Thus, rngEndOfDoc becomes the "point", and not the contents of the entire contents of the main document.

FWIW I never think about the situation when I will use EndKey for this (emulate user actions), and I would only change Selection when I want to leave the user at the end of the document so that he can start typing in this place.

+3
source

Thanking you for three wonderful responses from others, I thought I would add my own. Below are two versions of the feature that I started with the wishes of Word. initially returning the added range.

The first version uses MS Word objects to import the MS Word text file library that traditionally comes with Delphi (for example, Word2000.Pas) and uses "early binding" automation, while the second version does the same using late binding.

 function AppendRange(InputRange : Range) : Range; var Direction : OleVariant; begin Result := InputRange; Direction := wdCollapseEnd; Result.Collapse(Direction); end; function AppendRangeLB(InputRange : OleVariant) : OleVariant; begin Result := InputRange; Result.Collapse(wdCollapseEnd); end; 

Use for example

 AppendedRange := AppendRange(ExistingRange); AppendedRange.Text := 'YYY'; 
+3
source

Think of Document.Range as the union of all possible ranges in the main document (that is, except for headers, footers, floating material, etc.). It always starts before the first character and ends after the last character (which, in my experience, is always a hidden paragraph mark). It is impossible to define a different range, which after the current Document.Range or, conceptually, "add" something after Document.Range , because there is no "after" Document.Range (it always comes to the end).

Thus, in order to expand the document with new text or other built-in objects, you must insert them at the end of Document.Range - actually to the end, since the last, hidden, paragraph mark should be the final character of the document. This is exactly what you are doing in your pseudo code. In VBA, the empty range at the end of the document can be referenced in

 Document.Range(Document.Range.End-1, Document.Range.End-1) 

Trying to define it with Document.Range.End actually causes an error, because it points to this last (hidden) character - and this is outside the document - that’s why we need -1.

After receiving this range, we can fill it with things - expanding it and, logically, Document.Range . For example, to add text to the end of the active document, write

 ActiveDocument.Range(ActiveDocument.Range.End-1, ActiveDocument.Range.End-1).Text = "New Text" 

Note that with the same “final” empty range, you can achieve:

 Document.Bookmarks("\EndOfDoc").Range 
+2
source

In Word 15 (Office 365) it works with the next version using a library like Word 2010 (I use early binding, but it should work with the latest binding). Word is TWordApplication , and aRange is WordRange .

 Word.Selection.EndKey(wdStory, wdMove); aRange := Word.Selection.Range; aRange.Text := 'This is new text added at the end.'; 
0
source

Source: https://habr.com/ru/post/1241267/


All Articles