[RESOLVED] Word: End of Document
In Word, how to check whether the current cursor position is at the end of the document.
Basically, using a loop, I am traversing all the characters of the document with .Selection.MoveRight method. I want to exit the loop when the .Selection is at the end of the document.
Pls guide
Re: Word: End of Document
A new, blank Word document contains a single empty paragraph, which is to say, a single character that happens to be a paragraph mark. Initially, the paragraph mark character has a Start property of 0, because zero items are situated to the left of it, and an End property of 1, because one item is situated to the left of it, counting itself.
When an item in a Word document is selected, Word's Selection object takes on the Start and End properties of that Item. If multiple items are selected, the Selection object takes on the Start property of the first item and the End property of the last item. For example, if the entire contents of a Word document are selected, the Selection object takes on the Start property of the first character in the document and the End property of the final paragraph mark.
The following code takes advantage of this to determine whether the selection includes the document's final paragraph mark.
VB Code:
' Determine whether selection includes final paragraph mark
Dim SelectionIncludesFinalParagraphMark As Boolean
If Selection.Type = wdSelectionNormal _
And Selection.End = ActiveDocument.Content.End _
Then
SelectionIncludesFinalParagraphMark = True
When Word's Insertion Point is flashing in a document, the Selection object's Start and End properties are equal to each other and represent how many items are to the left of the Insertion Point. Since the Insertion Point can travel up to but never past the final paragraph mark in a document, the Start and End properties of the Selection object will never get higher than the Start value of the final paragraph mark, which is to say, one less than the End property of the final paragraph mark.
The following code takes advantage of this to determine whether the insertion point is flashing in front of the document's final paragraph mark.
VB Code:
' Determine whether insertion point is flashing at end of document
Dim InsertionPointFlashingAtEndOfDoc As Boolean
If Selection.Type = wdSelectionIP _
And Selection.End = ActiveDocument.Content.End - 1 _
Then
InsertionPointFlashingAtEndOfDoc = True
Hope this helps...
Re: Word: End of Document
The below coding is help you and it is very easy method
Dim cursor_pos As Long
Dim doc_end As Long
cursor_pos = Selection.Start
doc_end = ActiveDocument.Range.End
doc_end = doc_end - 1
If cursor_pos = doc_end Then
MsgBox "Cursor position is at end of the document"
End If
Thanks,
nit22
Re: Word: End of Document
thx sid....that was very useful....can u tell me how to loop thru all the paragraphs in the document???
basically i want to delete all paragraphs, which starts with specific keywords
thx in advance
Re: Word: End of Document
Does this help?
VB Code:
Private Sub CommandButton1_Click()
Dim para As Paragraph
Dim myword As Words
For Each para In ActiveDocument.Paragraphs
'1 for the first word
' 9 because pvbangera has 9 characters
If Left(para.Range.Words(1), 9) = "pvbangera" Then
'Delete Para
para.Range.Delete
End If
Next para
End Sub
Re: Word: End of Document
thx again dude...it works...i will bother u once again if i need ur help.
btw, can u tell me how to remove page breaks frm the active document???
regards
Re: Word: End of Document
Quote:
i will bother u once again if i need ur help.
Anytime :D If I am not here then there are plenty of members who will...
Quote:
btw, can u tell me how to remove page breaks frm the active document???
Hi Does this help?
VB Code:
Private Sub CommandButton1_Click()
Dim PVB_Brk As Long ' kind of break
Dim PVBangera As Range ' the documents range
Set PVBangera = ActiveDocument.Range
With PVBangera.Find
.Text = Chr(12)
While .Execute
PVBangera.Select ' Selectin the range
PVB_Brk = Type_Of_Break(PVBangera)
Select Case PVB_Brk
Case -1 '-1 is for page break
'Deleting page Break
Selection.Delete
End Select
PVBangera.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub
Put this in a module...
VB Code:
Public Function Type_Of_Break(ByVal PVB_Range As Range) As Long
Dim PVB1 As Long ' flag for sections
Dim PVB2 As Long ' flag for sections
PVB_Range.Start = ActiveDocument.Range.Start
PVB1 = PVB_Range.Sections.Count ' count sections
PVB_Range.End = PVB_Range.End + 1 ' extend range
PVB2 = PVB_Range.Sections.Count ' counting sections again
If PVB1 = PVB2 Then ' next character is in the same section
Type_Of_Break = -1 ' ordinary pagebreak
Else
Type_Of_Break = ActiveDocument.Sections(PVB2).PageSetup.SectionStart
End If
End Function
Re: Word: End of Document
Also this is the value for different 'selection breaks type' ... (in case you require)
Continuous => 0
NewPage => 2
EvenPage => 3
OddPage => 4
Hope this helps...
Re: Word: End of Document
The below code helps to remove the page beaks in the activedocument
Dim myrange As Range
Set myrange = ActiveDocument.Range
myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
myrange.Find.Execute findtext:="^m", replacewith:="", Replace:=wdReplaceAll, Forward:=True, MatchWildcards:=False
Thanks,
nit22
Re: Word: End of Document
awesome...u ppl are really awesome
vbforum rocks
thx to all
Re: [RESOLVED] Word: End of Document
Glad to be of help :D
It really is an amazin forum.....
Re: [RESOLVED] Word: End of Document
Re: [RESOLVED] Word: End of Document
@:Nit22: It is absolutely at the Thread owner's discretion. We cannot just ask anyone to rate the post. It seems pretty rude... ;)
Re: [RESOLVED] Word: End of Document
Ok koolsid. I am new one, sorry I don't know about this.