Results 1 to 8 of 8

Thread: Finding the line number in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Finding the line number in Word

    Quick question - how do I find the line number a particular word is on?

    Code:

    For iCount = 1 To ThisDocument.Content.Words.Count
    sWord = Trim(ThisDocument.Content.Words.Item(iCount))
    if sWord = <criteria> then
    iLineNumber = ?????
    end if
    Next iCount

    Thanks in advance

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Finding the line number in Word

    AFAIK, there is no line number as word wrapping and margins will throw off the count when opened under different systems.

    What are you trying to do?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Junior Member
    Join Date
    Mar 2007
    Posts
    18

    Re: Finding the line number in Word

    Here's a simple subroutine to report both the page number and the line number. You need both to identify your position in the document because line numbers are numbered relative to the current page.
    Code:
    Sub j()
    Dim getPage As Integer, lnNum As Integer
    
        getPage = Selection.Information(wdActiveEndPageNumber)
        lnNum = Selection.Information(wdFirstCharacterLineNumber)
        Debug.Print "Reported Page: " & getPage
        Debug.Print "Reported Line: " & lnNum
    End Sub
    Now it's your turn: -- show me how to add rows to an existing form in wordBasic. -- I'd be VERY grateful -- no response to my thread so far
    Bob.

    P.S. Of course, as your previous respondent pointed out, any time you change fonts or margins prior to the current position these values will change.
    B.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Finding the line number in Word

    Nice I must have missed the Information function.
    Learned something new today
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Finding the line number in Word

    Thanks for that, it's a very handy function but I'm not sure I can use it for my current situation.

    What I'm trying to do is this; I have a form, on which is a listview filled with a number of words from the document that meet a certain criteria, along with their word number.
    When I click on the word in the listview, I want the document to go to and highlight the word I clicked on the listview. I hope that explains it ok.

    James

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Finding the line number in Word

    Ok, then you will want to use the .Find method which you can have highlight its matches.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Junior Member
    Join Date
    Mar 2007
    Posts
    18

    Re: Finding the line number in Word

    You may be able to get the word count for a selection by highlighting the document up to the word of interest, then using the wordCount function -- I'm not at work today and don't have word 2003 at home, so I can't give the specifics -- Anybody know how to add a label and textbox to a MsWord 2003 form in VBA?

    Bob.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Finding the line number in Word

    Record a macro of you doing the task. Then view the module generated code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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