Consulting

Results 1 to 6 of 6

Thread: Solved: Last line is 1st in a range

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location

    Solved: Last line is 1st in a range

    The following bit of code really annoys me. Let me explain a bit. This is from XL. A Word doc search has made its' find and all you want to do is find the paragraph location of the find and make a range of it. The following codes are only needed when the found paragraph is on a last page line and you want to expand the range to the next page. Word seemed to get confused in 2001 vs my 2003 upgrade (see code... both are the same with commented out code(2003) and numeric values) Is this an upgrade or do I have a Word option messing with me again? Dave

    mS2001premium
    [VBA]
    'adjust for pages
    If Wapp.Selection.Information(wdActiveEndPageNumber) > 1 Then
    If Wapp.Selection.Information(wdFirstCharacterLineNumber) <> 46 Then
    Adjust = Wapp.Selection.Information(wdActiveEndPageNumber) * 46 - 46
    Else
    Adjust = (Wapp.Selection.Information(wdActiveEndPageNumber) - 1) * 46 - 46
    End If
    End If
    FirstParaloc = Wapp.Selection.Information(wdFirstCharacterLineNumber) + Adjust
    Set Myrange = Wapp.ActiveDocument.Paragraphs(FirstParaloc).Range
    [/VBA]


    ms2003
    [VBA]
    If Wapp.Selection.Information(3) > 1 Then
    'If Wapp.Selection.Information(10) <> 46 Then
    Adjust = Wapp.Selection.Information(3) * 46 - 46
    'Else
    'Adjust = (Wapp.Selection.Information(3) - 1) * 46 - 46
    'End If
    End If
    FirstParaloc = Wapp.Selection.Information(10) + Adjust
    Set Myrange = Wapp.ActiveDocument.Paragraphs(FirstParaloc).Range
    [/VBA]

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Dave,

    Without studying it in detail, the range of the paragraph containing the selection is[vba]Set MyRange = Wapp.Selection.Paragraphs(1).Range[/vba]You would, of course, be better not using the Selection in the first place.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Tony.. Much thanks for your help again. Many apologies for the delayed reply. It's a busy time of year for me. I'm not sure if I follow how you would use a find and then set a range without using a selection? I trialed a variation of your code suggestion and it worked for 2003 but again my 01 version didn't want to play. I'm attaching a zipped "summary.doc" to place on your "C" drive (it's just an example doc). Place the following code in an XL module. I would like to determine if this is setting, MS version or my stupid computer issue. If you, or anyone else, has some time to trial this on different MS versions that would be greatly appreciated. Dave

    [vba]
    Sub FixWord()
    ' displays line/paragraph# & found range(s)
    'paragraphs are 1 line only

    Dim Wapp As Object, Bigstring As String, myData As String
    Dim TemP As String, PagFlag As Boolean
    Dim Myrange As Variant, Myrange2 As Variant, Adjust As Integer
    Dim ThisParaLoc As Integer, LastParaLoc As Integer, FirstParaloc As Integer

    TotParas = 5 'paragraph range
    ThisParaLoc = 0 'found paragraph line#
    Bigstring = vbNullString 'combines paragraphs

    On Error GoTo RetErr
    'Open file and search
    Set Wapp = CreateObject("Word.Application")
    TemP = "c:\summary.doc"
    Wapp.documents.Open Filename:=TemP, ReadOnly:=True

    'turn on pagination
    PagFlag = False
    If Wapp.Options.Pagination = False Then
    Wapp.Options.Pagination = True
    PagFlag = True
    End If

    'find last paragraph
    LastParaLoc = Wapp.ActiveDocument.Paragraphs.Count
    'Wapp.ActiveDocument.Select
    'LastParaLoc = Wapp.Selection.Paragraphs.Count 'last paragraph(line#)

    'Find keyword("seed") in the source WORD document
    'loop to find all keywords in doc.
    Do While ThisParaLoc < LastParaLoc
    Set Myrange2 = Wapp.ActiveDocument.Paragraphs(ThisParaLoc + 1).Range
    Myrange2.SetRange Start:=Myrange2.Start, _
    End:=Wapp.ActiveDocument.Paragraphs(LastParaLoc).Range.End
    Myrange2.Select
    With Wapp.Selection.Find
    .Text = "seed"
    .Forward = True
    .Execute

    'expand keyword selection to whole paragraph(line) selection
    'add next 4 paragaphs(lines) to selection
    'adjust for pages and selection on more than 1 page
    If .Found = True Then
    .Parent.Expand Unit:=4

    'if > 1 page
    If Wapp.Selection.Information(3) > 1 Then
    '************************
    'all of the code below is needed for MS2001 premium to
    'correct last line error ie. remove comments for MS2001
    ' If Wapp.Selection.Information(10) <> 46 Then
    Adjust = Wapp.Selection.Information(3) * 46 - 46
    ' Else
    ' Adjust = (Wapp.Selection.Information(3) - 1) * 46 - 46
    ' End If
    End If

    FirstParaloc = Wapp.Selection.Information(10) + Adjust
    MsgBox "Found word on Line/Paragraph#: " & FirstParaloc

    Set Myrange = Wapp.ActiveDocument.Paragraphs(FirstParaloc).Range
    Myrange.SetRange Start:=Myrange.Start, _
    End:=Wapp.ActiveDocument.Paragraphs(FirstParaloc + (TotParas - 1)).Range.End
    ThisParaLoc = FirstParaloc + (TotParas - 1)
    Myrange.Select
    myData = Wapp.Selection.Text
    Else
    Exit Do
    End If

    End With

    'store in bigstring
    Bigstring = Bigstring + myData
    MsgBox Bigstring
    Loop
    If PagFlag Then
    Wapp.Options.Pagination = False
    End If
    Wapp.Quit
    Set Wapp = Nothing
    Exit Sub
    'handle errors
    RetErr:
    On Error GoTo 0
    MsgBox "Error"
    Wapp.Quit
    Set Wapp = Nothing
    If PagFlag Then
    Wapp.Options.Pagination = False
    End If
    End Sub
    [/vba]

    edit: code update & 2000 to 2001 in comments
    re-edit: fixed format & dim
    Last edited by Dave; 06-23-2006 at 10:29 PM.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am finding it difficult to figure out what you are achieving here. Or what you want to. Other comments:
    [vba] 'find last paragraph
    Wapp.ActiveDocument.Select
    LastParaLoc = Wapp.Selection.Paragraphs.Count 'last paragraph(line#)[/vba]
    This code selects the entire document - apparently in order to get the paragraph count. This use of Selection is totally not needed.
    LastParaLoc = Wapp.ActiveDocument.Paragraphs.Count will get the paragraph count. No need to select the document. Plus, the name of the variable - LastParaLoc is at odds with what is put into that variable. "Loc" seems to me to imply some Location number. However what it gets is simply the paragraph count.

    Then, with the whole document still selected, you code:[vba] Set Myrange2 = Wapp.ActiveDocument.Paragraphs(ThisParaLoc + 1).Range
    Myrange2.SetRange Start:=Myrange2.Start, _
    End:=Wapp.ActiveDocument.Paragraphs(LastParaLoc).Range.End
    Myrange2.Select [/vba]Since ThisParaLoc (another misnomer I think) = 0 at the start of the loop, essentially you make the Selection everything again. The Range is ThisParaLoc(0) + 1, so Paragraph(1) - the first paragraph, then SetRange to LastParaLoc.Range. What you have done is:

    1. Selecting the entire document (not needed for what you are doing);

    2. getting the paragraph count - again not needed by selecting;

    3. then Selecting the entire document again - or rather make a Range of the entire document, then selecting that Range.

    If I had a grasp of what exactly you are trying to do, I am pretty sure this code could be condensed greatly.

  5. #5
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Thanks Gerry for your time. You are correct, this code does replace the offensive 2 lines of code from your initial quote...
    [VBA]
    LastParaLoc = Wapp.ActiveDocument.Paragraphs.Count
    [/VBA]
    As the variable name suggests, it is the location of the last paragraph/line number (paragraphs are 1 line only) in the Doc. "FirstParaloc" is the first line number of each found range. "ThisParaLoc" is the last line number of each found range. This routine searches a document for an occurences of the word "seed" , expands the found word "seed" range to include the entire line/paragraph and the next 4 lines/paragraphs. It then converts this expanded range to a string variable ("mydata"...which I see I forgot to Dim). These single strings are then collected into a single string ("Bigstring") as the final result. So the process is... set the entire Doc to a range for searching, after a found word convert 5 lines to a string, combine this string with all found strings into a single output string, then reset the search range to the remaining Doc and continue the process until the last paragraph. The attachment Doc provides an example of my real concern. In my MS 2001 premium version, when a found word is on the last line of a page, without this seemingly insane line of code this routine produces erroneous results and crashes.
    [VBA]
    Adjust = (Wapp.Selection.Information(3) - 1) * 46 - 46
    [/VBA]
    As indicated, this is not needed in my MS2003 version. I hope that if you trial the routine with the attached Doc, you will have a better understanding of the problem. I only put this routine together to display and hopefully fix my problem. Again, thanks for your help. Dave

  6. #6
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    I've finally arrived at a solution. This works for my different MS versions. The coding change determines the page adjustment before expanding the found word into a paragraph. A simple fix for a confusing problem. Again, thanks Tony and Gerry for your efforts. Dave
    [VBA]
    Sub FixWord()
    ' displays line/paragraph# & found range(s)
    'paragraphs are 1 line only

    Dim Wapp As Object, Bigstring As String, myData As String
    Dim TemP As String, PagFlag As Boolean
    Dim Myrange As Variant, Myrange2 As Variant, Adjust As Integer
    Dim ThisParaLoc As Integer, LastParaLoc As Integer, FirstParaloc As Integer

    TotParas = 5 'paragraph range
    ThisParaLoc = 0 'found paragraph line#
    Bigstring = vbNullString 'combines paragraphs

    On Error GoTo RetErr
    'Open file and search
    Set Wapp = CreateObject("Word.Application")
    TemP = "c:\summary.doc" 'name of search source doc.
    Wapp.documents.Open Filename:=TemP, ReadOnly:=True

    'turn on pagination
    PagFlag = False
    If Wapp.Options.Pagination = False Then
    Wapp.Options.Pagination = True
    PagFlag = True
    End If

    'find last paragraph
    LastParaLoc = Wapp.ActiveDocument.Paragraphs.Count

    'Find keyword("seed") in the source WORD document
    'loop to find all keywords in doc.
    Do While ThisParaLoc < LastParaLoc
    Set Myrange2 = Wapp.ActiveDocument.Paragraphs(ThisParaLoc + 1).Range
    Myrange2.SetRange Start:=Myrange2.Start, _
    End:=Wapp.ActiveDocument.Paragraphs(LastParaLoc).Range.End
    Myrange2.Select
    With Wapp.Selection.Find
    .Text = "seed" 'keyword
    .Forward = True
    .Execute

    If .Found = True Then

    'if > 1 page
    If Wapp.Selection.Information(3) > 1 Then
    Adjust = Wapp.Selection.Information(3) * 46 - 46
    End If

    'expand keyword selection to whole paragraph(line) selection
    .Parent.Expand Unit:=4

    'adjust FirstParaloc for pages
    FirstParaloc = Wapp.Selection.Information(10) + Adjust
    MsgBox "Found word on Line/Paragraph#: " & FirstParaloc

    'add next 4 paragaphs(lines) to selection
    Set Myrange = Wapp.ActiveDocument.Paragraphs(FirstParaloc).Range
    Myrange.SetRange Start:=Myrange.Start, _
    End:=Wapp.ActiveDocument.Paragraphs(FirstParaloc + (TotParas - 1)).Range.End
    ThisParaLoc = FirstParaloc + (TotParas - 1)
    Myrange.Select
    myData = Wapp.Selection.Text
    Else
    Exit Do
    End If

    End With

    'store in bigstring
    Bigstring = Bigstring + myData
    MsgBox Bigstring
    Loop

    If PagFlag Then
    Wapp.Options.Pagination = False
    End If
    Wapp.Quit
    Set Wapp = Nothing
    Exit Sub

    'handle errors
    RetErr:
    On Error GoTo 0
    MsgBox "Error"
    If PagFlag Then
    Wapp.Options.Pagination = False
    End If
    Wapp.Quit
    Set Wapp = Nothing
    End Sub
    [/VBA]

Posting Permissions

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