Word2003 - test for "End Of Document"

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Word2003 - test for "End Of Document"

Post by ChrisGreaves »

Code: Select all

Function blnAtEndOfDocument() As Boolean
    Dim rng As Range
    Set rng = Selection.Range ' Preserve the current/original  Selection
    Dim intOriginal As Integer
    intOriginal = rng.End ' Preserve the end point of the given range (usually a Selection.Range)
    Selection.EndKey Unit:=wdStory ' Go to the end of the document
    blnAtEndOfDocument = (intOriginal = Selection.Range.End) ' If the two end points are the same value, we were already at the end of the document
    rng.Select ' Regardless, re-select the original selection
'Sub TESTblnAtEndOfDocument()
'    MsgBox blnAtEndOfDocument()
'End Sub
End Function
This code works for me, in that it tells me whether I am right at the end of a document (and need to .TypeText a vbCrLf), or whether I am NOT at the end of the document, and so can assume that a vBCrLf is somewhere further on.
Comments welcome.
Cheers
Chris
Last edited by ChrisGreaves on 27 Oct 2020, 16:11, edited 1 time in total.
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word2003 - test for "End Of Document"

Post by HansV »

A paragraph end in Word is vbCr, not vbCrLf.
Best wishes,
Hans

User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Word2003 - test for "End Of Document"

Post by Jay Freedman »

It isn't necessary to move the Selection (and thus the cursor, possibly causing the screen to scroll). This will do:

Code: Select all

Function blnAtEndOfDocument()
    blnAtEndOfDocument = (Selection.End = ActiveDocument.Range.End - 1)
End Function
The "- 1" is necessary here because every document ends with a paragraph mark, and the farthest possible position of the cursor is to the left of that paragraph mark, or 1 character before the end of the document's range.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

HansV wrote:A paragraph end in Word is vbCr, not vbCrLf.
Hans, you are quite right :sulks:

I know that using the vbCrLf works for me, so perhaps I should have stressed "a paragraph mark" rather than 'the end of a paragraph"?
Cheers
Chris
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

Jay Freedman wrote:It isn't necessary to move the Selection ...
Thanks Jay. You are quite right. I already used :sulks: with Hans, so :pouts:

This morning I came across two other functions, boith of them in my utility library, to test end of document.
One of the definitions uses your construct.

I rather suspect that you have had an impact on my coding for longer than I care to admit.
Although, obviously, the impact doesn't sear my brain anyway near enough :laugh:

Cheers
Chris
There's nothing heavier than an empty water bottle

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Word2003 - test for "End Of Document"

Post by LisaGreen »

Hello,

I believe that VbCrLf can be replaced with VbNewline.... I think this is a hangover from early versions of Basic.

One of the companies I worked for... er... some time ago... was UNIVAC... er Sperry... and so on... tiiiillll.... UNISYS.... They had ... and I believe they still have... a policy of never deleting "old commands" in software to maintain "backwards compatibility".

Lisa

User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Word2003 - test for "End Of Document"

Post by Jay Freedman »

LisaGreen wrote:One of the companies I worked for... er... some time ago... was UNIVAC... er Sperry... and so on... tiiiillll.... UNISYS.... They had ... and I believe they still have... a policy of never deleting "old commands" in software to maintain "backwards compatibility".
Small world! I joined Burroughs six months before it became the other half of Unisys. I stayed there for 26 years. Yes, there is an almost pathological reluctance to let anything go out of support, mainly because the customers are so insistent on keeping the old software running.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

LisaGreen wrote:I believe that VbCrLf can be replaced with VbNewline.... I think this is a hangover from early versions of Basic.
Thanks Lisa. I'll experiment once my laptop is rebuilt (a few days from now).

In VBA I see vbCrLf as two characters while vbCr and vbLF are single characters.
Cheers
Chris
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

Jay Freedman wrote:... I joined Burroughs six months before it became the other half of Unisys. ...
Ooooh! I was enmeshed in COBOL on a B6700 (I think it was) in West Perth back in 1982, or thereabouts.
At that stage I was thinking less and less of which hardware I was on, and focusing more on making my cCBOL as transportable as possible.
Cheers
Chris
There's nothing heavier than an empty water bottle

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Word2003 - test for "End Of Document"

Post by LisaGreen »

ChrisGreaves wrote:
LisaGreen wrote:I believe that VbCrLf can be replaced with VbNewline.... I think this is a hangover from early versions of Basic.
Thanks Lisa. I'll experiment once my laptop is rebuilt (a few days from now).

In VBA I see vbCrLf as two characters while vbCr and vbLF are single characters.
Cheers
Chris
Yep... That's correct... and the Newline is two as well.

Just in case... though you prob know already.... From teletype days... A Cr er... returned the carraige... and only that ... while a LF fed a line... and only that... Thus to get to a newline you had to send both... Hence CrLf

It was the equivalent of pushing the bar on the typewriter to get to the start of the next line!!

I wrote a "Line dump" procedure a while ago to give the ascii chrs from a line and they show up clearly. It's been very useful over the years as a debugging tool!

Lisa

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Word2003 - test for "End Of Document"

Post by LisaGreen »

Jay Freedman wrote:
LisaGreen wrote:One of the companies I worked for... er... some time ago... was UNIVAC... er Sperry... and so on... tiiiillll.... UNISYS.... They had ... and I believe they still have... a policy of never deleting "old commands" in software to maintain "backwards compatibility".
Small world! I joined Burroughs six months before it became the other half of Unisys. I stayed there for 26 years. Yes, there is an almost pathological reluctance to let anything go out of support, mainly because the customers are so insistent on keeping the old software running.

Ahhhhh.... the old days!! I still have a soft spot for er... let's call it unisys... THough there was a joke going around at the time... prob buried in the past now that it stood for univac is still your supplier!! hehehe.

I don't know how things are at the moment but I just loved the idea that they used a simple text editor coupled with a stream generator to create ALL of their code for EVERYTHING!!

Am I correct in remembering that Burroughs didn't use an assembler?

Lisa

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word2003 - test for "End Of Document"

Post by HansV »

In Word, vbCr is used as paragraph mark, and character 11 is used as line break.
Excel uses vbLf to start a new line in a cell.

Standard text files in Windows use vbCrLf as line break.
The original Macintosh operating system used vbCr as line break, but OS X uses vbLf (just like Unix).

If you have code that has to run on both Windows and Macs, vbNewLine is handy: it will insert a line break appropriate for the platform the code runs on, so vbCrLf on Windows, and vbLf on OS X.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12605
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Word2003 - test for "End Of Document"

Post by StuartR »

Old fashioned teletypes used to use CR to return the carriage to the beginning of the line and LF to scroll the carriage down one line. It was normal to use both at the end of a line to position the carriage correctly for the next line. This was based on how typewriters used to work.
StuartR


User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Word2003 - test for "End Of Document"

Post by Jay Freedman »

LisaGreen wrote:Ahhhhh.... the old days!! I still have a soft spot for er... let's call it unisys... THough there was a joke going around at the time... prob buried in the past now that it stood for univac is still your supplier!! hehehe.

I don't know how things are at the moment but I just loved the idea that they used a simple text editor coupled with a stream generator to create ALL of their code for EVERYTHING!!

Am I correct in remembering that Burroughs didn't use an assembler?

Lisa
When the name was announced after the merger, we said it sounded like "Aunt Eunice's Computer Company". :grin:

Our coding was done in what was essentially a flat-file database, where every line was stored as a record with metadata about its sequence, date of creation, and patch number. If necessary, any section of code could be backed out through some or all previous patches, all the way back to ground zero. We could also pinpoint which patch introduced a particular bug. Every time I described this to non-Burroughs programmers, I could see them turning green with envy.

Some machines in the B5000/B5500 large systems series were optimized to run Algol programs (https://en.wikipedia.org/wiki/Burroughs_large_systems). If I'm correct, the instruction set was a one-for-one match to the Algol constructs, so the compiler was essentially an assembler. Fortunately, I never had to worry about the machine-code details.

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Word2003 - test for "End Of Document"

Post by LisaGreen »

> pinpoint which patch introduced a particular bug

HAHAHAHA!!! I remember that one!! A solution introducing bugs!

Lisa

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

Jay Freedman wrote:... If I'm correct, the instruction set was a one-for-one match to the Algol constructs, so the compiler was essentially an assembler. Fortunately, I never had to worry about the machine-code details.
This sounds similar to "Plasyd" on ICL's 1900 series.
We started off in PLAN assembly language but then were told that we could continue to receive a paycheck if we switched to Plasyd. (hah hah!)

Plasyd was a hybrid language that had some structure (If-Then-Else) which got rid of Branch instructions, and allowed us to use symbolic names for registers - basically we no longer had to use registers but could think of them as variables.
I suspect that Plasyd expanded language constructs in the way that the IBM macros (1401, 360) expanded into lines of the host assembly language. It wasn't quite 3rd generation FORTRAN, Algol or COBOL, but it was a giant step towards improved productivity.

cheers
Chris
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15619
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003 - test for "End Of Document"

Post by ChrisGreaves »

LisaGreen wrote:It was the equivalent of pushing the bar on the typewriter to get to the start of the next line!!...
... but without the BEL code.

I still miss the sound of my mother (ding!) typing (ding!) stencils (ding!) for (ding!) the (ding!) Gestetner (ding!) duplicator. (ding!) (ding!)

Cheers
Chris
There's nothing heavier than an empty water bottle