?
Solved

Getting accurate Word line number with VBA

Posted on 2003-03-20
4
Medium Priority
?
552 Views
Last Modified: 2008-03-03
Hi,

How do I get the absolute line number of a given cursor position in the word document. Absolute, and not the relative line number you see at the Word 2000 status bar(bottom) which is relative number to the current page.

When I get the absolute line number of a position, I must be able to go back to the same position using "CTRL+G > Line > enter value"


Thank you

0
Comment
Question by:harrylmh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:dmang
ID: 8182172
Hi harrylmh ... this was interesting...here's what I have to offer ...

first the bad news....this does not use the ctrl + g for go to, but rather a dropdown menu item when you right click in the document area.

next... a routine which will determine the page and linecounts for the document, and then determine where to go

Put this in the document open sub of your document. This will update word's dropdown menus.

Private Sub Document_Open()

    Dim stritem As String, strCaption As String
    Dim cbar As CommandBar
    Dim cbc As CommandBarControl
    Dim i As Integer, iCTlCount As Integer
 
    For Each cbar In Application.CommandBars
   
        iCTlCount = cbar.Controls.Count
        For i = 1 To iCTlCount
            If cbar.Controls(i).Caption Like "Bullets and*" Then
                For Each cbc In cbar.Controls
                    If cbc.Caption = "Go To Absolute Line Number" Then
                        Exit Sub
                    End If
                Next cbc
               
                With cbar.Controls.Add(msoControlButton)
                     .Caption = "Go To Absolute Line Number"
                     .OnAction = "GoToAbsoluteLine"
                     .Style = msoButtonIconAndCaption
                     .BeginGroup = True
                     .FaceId = 361
                     .TooltipText = "Go to absolute line number in the current document"
                     .Tag = .Caption
                     .Visible = True
                End With
            End If
       
        Next i
   
    Next cbar


End Sub


Next....

Using the VB editor,Add a module to the document, and add this in.

Sub GotoAbsoluteLine()

    Dim iPage As Integer
    Dim iLines As Integer
    Dim iAbsLine As Integer
    Dim iPageLines() As Integer
    Dim iPageCount As Integer
    Dim strPageNum As String
    Dim iGoToLine As Integer
   
    strline = InputBox("Enter aboslute line number", "Go To Document Line Number", 1)
       
    If Len(strline) > 0 Then
        If IsNumeric(strline) Then
            iAbsLine = Val(strline)
        Else
            MsgBox "Please enter numeric line value"
            Exit Sub
        End If
       
    Else
        MsgBox "Go To Absolute Line was cancelled"
        Exit Sub
    End If
   
    Application.ScreenUpdating = False
    ReDim iPageLines(Selection.Information(wdNumberOfPagesInDocument))
    strPageNum = Trim(Str(Selection.Information(wdNumberOfPagesInDocument)))
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
   
    'get last line of last page
   
    Selection.EndKey Unit:=wdStory
    iPageLines(Selection.Information(wdNumberOfPagesInDocument) - 1) = Selection.Information(wdFirstCharacterLineNumber)
    p = Selection.Information(wdNumberOfPagesInDocument)
    strPageNum = Trim(Str(p))
   
    For p = Selection.Information(wdNumberOfPagesInDocument) To 1 Step -1
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1, Name:=strPageNum
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=1, Name:=""
        iPageLines(p - 1) = Selection.Information(wdFirstCharacterLineNumber)
        strPageNum = Trim(Str(p))
    Next p
   
    For p = 0 To UBound(iPageLines) - 1
        iLines = iLines + iPageLines(p)
        If iLines > iAbsLine Then
            If p = 0 Then
                strPageNum = Trim(Str(p + 1))
                Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
                iGoToLine = iAbsLine - 1
                Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, _
                        Count:=iGoToLine, Name:=""
            Else
                strPageNum = Trim(Str(p + 1))
                Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
                iGoToLine = iAbsLine - (iLines - iPageLines(p)) - 1
                Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, _
                        Count:=iGoToLine, Name:=""
            End If
            Exit For
        End If
       
    Next p
   
    Application.ScreenUpdating = True


End Sub




0
 
LVL 9

Accepted Solution

by:
dmang earned 375 total points
ID: 8182628
HI again harrylmh ...

I found a couple of errors when trying to handle single pages, and the first line on a page...

Use the following in the document module instead..


Sub GotoAbsoluteLine()

    Dim iPage As Integer
    Dim iLines As Integer
    Dim iAbsLine As Integer
    Dim iPageLines() As Integer
    Dim iPageCount As Integer
    Dim strPageNum As String
    Dim iGoToLine As Integer
    Dim DocRange As Range
    Dim p As Integer
   
    strline = InputBox("Enter aboslute line number", "Go To Document Line Number", 1)
       
    If Len(strline) > 0 Then
        If IsNumeric(strline) Then
            iAbsLine = Val(strline)
        Else
            MsgBox "Please enter numeric line value"
            Exit Sub
        End If
       
    Else
        MsgBox "Go To Absolute Line was cancelled"
        Exit Sub
    End If
   
    Application.ScreenUpdating = False
    Set DocRange = ActiveDocument.Range
    iPageCount = DocRange.ComputeStatistics(Statistic:=wdStatisticPages)
    ReDim iPageLines(iPageCount)
    strPageNum = Trim(Str(iPageCount))
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
   
    'get last line of last page
   
    Selection.EndKey Unit:=wdStory
    iPageLines(iPageCount - 1) = Selection.Information(wdFirstCharacterLineNumber)
    'p = Selection.Information(wdActiveEndAdjustedPageNumber)
    strPageNum = Trim(Str(iPageCount))
   
    If iPageCount > 1 Then
        For p = iPageCount To 2 Step -1
            Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1, Name:=strPageNum
            Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=1, Name:=""
            iPageLines(p - 2) = Selection.Information(wdFirstCharacterLineNumber)
            strPageNum = Trim(Str(p - 1))
        Next p
    End If
    For p = 0 To UBound(iPageLines) - 1
        iLines = iLines + iPageLines(p)
    Next p
   
    If iAbsLine > iLines Then
        MsgBox "Requested line number is out of document boundary (Max = " & Trim(Str(iLines)) & ")"
        Exit Sub
    End If
   
    For p = 0 To UBound(iPageLines) - 1
        iLines = iLines + iPageLines(p)
        If iLines > iAbsLine Then
            If p = 0 Then
                strPageNum = Trim(Str(p + 1))
                Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
                iGoToLine = iAbsLine - 1
                Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, _
                        Count:=iGoToLine, Name:=""
            Else
                strPageNum = Trim(Str(p + 1))
                Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPageNum
                If iAbsLine - iLines - iPageLines(p) - 1 > 0 Then 'first line of page
                    iGoToLine = iAbsLine - (iLines - iPageLines(p)) - 1
                    Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, _
                        Count:=iGoToLine, Name:=""
                End If
            End If
            Exit For
        End If
       
    Next p
   
    Application.ScreenUpdating = True


End Sub
0
 

Expert Comment

by:CleanupPing
ID: 8531907
Hi harrylmh,
This old question (QID 20558290) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8982590
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in a week or two.  I would appreciate any comments by the experts that would help me in making a recommendation.
It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp

GPrentice00
Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question