Results 1 to 14 of 14

Thread: [RESOLVED] Word: End of Document

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Resolved [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
    Microsoft Techie

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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:
    1. ' Determine whether selection includes final paragraph mark
    2.  
    3. Dim SelectionIncludesFinalParagraphMark As Boolean
    4. If Selection.Type = wdSelectionNormal _
    5.    And Selection.End = ActiveDocument.Content.End _
    6.    Then
    7.  
    8. 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:
    1. ' Determine whether insertion point is flashing at end of document
    2.  
    3. Dim InsertionPointFlashingAtEndOfDoc As Boolean
    4.  
    5.     If Selection.Type = wdSelectionIP _
    6.         And Selection.End = ActiveDocument.Content.End - 1 _
    7.         Then
    8.         InsertionPointFlashingAtEndOfDoc = True

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Junior Member
    Join Date
    Jan 2007
    Location
    Chennai
    Posts
    26

    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

  4. #4

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    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
    Last edited by pvbangera; Feb 5th, 2007 at 10:51 AM.
    Microsoft Techie

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Word: End of Document

    Does this help?

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim para As Paragraph
    4. Dim myword As Words
    5.  
    6. For Each para In ActiveDocument.Paragraphs
    7.        
    8.         '1 for the first word
    9.         ' 9 because pvbangera has 9 characters
    10.         If Left(para.Range.Words(1), 9) = "pvbangera" Then
    11.             'Delete Para
    12.             para.Range.Delete
    13.            
    14.         End If
    15.  
    16. Next para
    17.  
    18. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    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
    Microsoft Techie

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Word: End of Document

    i will bother u once again if i need ur help.
    Anytime If I am not here then there are plenty of members who will...
    btw, can u tell me how to remove page breaks frm the active document???
    Hi Does this help?

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim PVB_Brk As Long ' kind of break
    3.     Dim PVBangera As Range ' the documents range
    4.     Set PVBangera = ActiveDocument.Range
    5.     With PVBangera.Find
    6.         .Text = Chr(12)
    7.         While .Execute
    8.             PVBangera.Select ' Selectin the range
    9.             PVB_Brk = Type_Of_Break(PVBangera)
    10.             Select Case PVB_Brk
    11.             Case -1 '-1 is for page break
    12.                 'Deleting page Break
    13.                 Selection.Delete
    14.             End Select
    15.             PVBangera.Collapse Direction:=wdCollapseEnd
    16.         Wend
    17.     End With
    18. End Sub

    Put this in a module...

    VB Code:
    1. Public Function Type_Of_Break(ByVal PVB_Range As Range) As Long
    2.     Dim PVB1 As Long ' flag for sections
    3.     Dim PVB2 As Long ' flag for sections
    4.     PVB_Range.Start = ActiveDocument.Range.Start
    5.     PVB1 = PVB_Range.Sections.Count ' count sections
    6.     PVB_Range.End = PVB_Range.End + 1 ' extend range
    7.     PVB2 = PVB_Range.Sections.Count ' counting sections again
    8.     If PVB1 = PVB2 Then ' next character is in the same section
    9.         Type_Of_Break = -1 ' ordinary pagebreak
    10.     Else
    11.         Type_Of_Break = ActiveDocument.Sections(PVB2).PageSetup.SectionStart
    12.     End If
    13. End Function
    Last edited by Siddharth Rout; Feb 6th, 2007 at 03:40 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    Junior Member
    Join Date
    Jan 2007
    Location
    Chennai
    Posts
    26

    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

  10. #10

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: Word: End of Document

    awesome...u ppl are really awesome

    vbforum rocks


    thx to all
    Microsoft Techie

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Word: End of Document

    Glad to be of help

    It really is an amazin forum.....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12
    Junior Member
    Join Date
    Jan 2007
    Location
    Chennai
    Posts
    26

    Thumbs up Re: [RESOLVED] Word: End of Document

    Welcome............
    Last edited by nit22; Feb 7th, 2007 at 05:49 AM.

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14
    Junior Member
    Join Date
    Jan 2007
    Location
    Chennai
    Posts
    26

    Re: [RESOLVED] Word: End of Document

    Ok koolsid. I am new one, sorry I don't know about this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width