PDA

View Full Version : Solved: ID the selection in Word 03



Dave
04-12-2006, 06:35 AM
I recently was forced to "upgrade" from MS office 2000 premium to MS office 2003 basic... a bit of a long story of stupidity starting with an unplugged keyboard and ending with a $400 outlay to replace my "misplaced" 2000 disk. Anyways, previously in Word 2000, the line number of a selection could be identified using the following code. Now with my 2003 "upgrade" , this code only returns -1. Anyone know what's up with that or how to identify the line (or paragraph) number of a selection using Word 2003? Dave
Wapp.Selection.Information(wdFirstCharacterLineNumber)

TonyJollans
04-12-2006, 11:53 AM
It does normally work but something is nagging at the back of my mind about this if only I could recall what it was. I expect a value of -1 to mean the information is not available - do you get it all the time or only in certain circumstances?

Dave
04-12-2006, 01:27 PM
Tony thanks for your assistance. The following example represents my routine. The second message box returns -1? Dave
ps. requires reference to Word object library

Sub test()
Dim myrange2 As Variant, Wapp As Object, Mytopic1 As String
Mytopic1 = "seed" 'search word change as needed
On Error GoTo ErFix
Set Wapp = CreateObject("Word.Application")
'change filename below as needed
Wapp.documents.Open Filename:="C:/Records/Summary.doc", ReadOnly:=True
Wapp.activedocument.Select
With Wapp.Selection.Find
.Execute FindText:=Mytopic1, Format:=False, Forward:=True
If .Found = True Then
.Parent.Expand Unit:=wdParagraph '4
myData = Wapp.Selection.Text
MsgBox myData
MsgBox Wapp.Selection.Information(wdFirstCharacterLineNumber)
End If
End With
Wapp.Quit
Set Wapp = Nothing
Exit Sub
ErFix:: On Error GoTo 0
MsgBox "File error"
Wapp.Quit
Set Wapp = Nothing
End Sub

Edited 13-Apr-06 by GeekGirlau. Reason: insert vba tags

Dave
04-12-2006, 01:29 PM
Tony thanks for your assistance. The following example represents my routine. The second message box returns -1? Dave
ps. requires reference to Word object library

Sub test()
Dim myrange2 As Variant, Wapp As Object, Mytopic1 As String
Mytopic1 = "seed" 'search word change as needed
On Error GoTo ErFix
Set Wapp = CreateObject("Word.Application")
'change filename below as needed
Wapp.documents.Open Filename:="C:/Records/Summary.doc", ReadOnly:=True
Wapp.activedocument.Select
With Wapp.Selection.Find
.Execute FindText:=Mytopic1, Format:=False, Forward:=True
If .Found = True Then
.Parent.Expand Unit:=wdParagraph '4
myData = Wapp.Selection.Text
MsgBox myData
MsgBox Wapp.Selection.Information(wdFirstCharacterLineNumber)
End If
End With
Wapp.Quit
Set Wapp = Nothing
Exit Sub
ErFix:: On Error GoTo 0
MsgBox "File error"
Wapp.Quit
Set Wapp = Nothing
End Sub

fumei
04-12-2006, 11:17 PM
The Expand instruction can, and probably does, move both the start and end of the selection. You may want to try collapsing it. Here is a different version.Sub FindLineNumber()
Selection.HomeKey unit:=wdStory

With Selection.Find
.ClearFormatting
Do While (.Execute(findtext:="lazy dog", Forward:=True) = True) = True
Selection.Expand unit:=wdParagraph
MsgBox Selection.Text
MsgBox Selection.Information(wdFirstCharacterLineNumber)
Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
End SubAdjust accordingly for your seed, and that you are using Wapp as your instance of Word.

TonyJollans
04-13-2006, 12:38 AM
Where in the document is your second occurrence found? The Line Number will be -1 if you're outside the main body of the document - headers, footers, etc.

Dave
04-13-2006, 04:05 PM
Maybe I've got a "glitch" ... this code continues to produce -1 but does find and show all the entries. There are no headers and footers (perhaps I don't understand?)... it's just imputted records ... docs with no format other than page seperation (other MS generated stuff ). Tony.. again thanks for your help as I seem to have made my initial molehill problem into a mountian of trouble which is really wearing thin on my good words for MS. Dave

Sub FindLineNumber()
Dim myrange2 As Variant, Wapp As Object, Mytopic1 As String
Mytopic1 = "seed" 'search word change as needed
On Error GoTo ErFix
Set Wapp = CreateObject("Word.Application")
'change filename below as needed
Wapp.documents.Open Filename:="C:/Records/Summary.doc", ReadOnly:=True
Wapp.activedocument.Select
Wapp.Selection.HomeKey unit:=wdStory

With Wapp.Selection.Find
.ClearFormatting
Do While (.Execute(findtext:=Mytopic1, Forward:=True) = True) = True
Wapp.Selection.Expand unit:=wdParagraph
MsgBox Wapp.Selection.Text
MsgBox Wapp.Selection.Information(wdFirstCharacterLineNumber)
Wapp.Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
Wapp.Quit
Set Wapp = Nothing
Exit Sub
ErFix:: On Error GoTo 0
MsgBox "File error"
Wapp.Quit
Set Wapp = Nothing
End Sub

fumei
04-15-2006, 03:06 AM
Try getting rid of this line. Wapp.activedocument.Select

TonyJollans
04-16-2006, 02:14 AM
I don't really have time to check this out but what I think the problem is, is ..

The Line Number can only be returned as Information if Word can calculate it. As it is dependent upon Page size it requires that the document be properly paginated. So, for example, it won't work (i.e. it will return -1) if you are in Normal View and Background Repagination is switched off.

As you are doing things in a hidden Word instance I suspect that, even if the options are set 'correctly', Word will not have repaginated the document on opening (which it always has to do as printer information is not saved with the documet) and will not, therefore, be able to determine line numbers.

fumei
04-16-2006, 06:19 AM
D'oh! Man, you are brilliant.

Dave
04-16-2006, 09:11 PM
Tony that was brilliant! My new install didn't have the general option "background repagination" turned on and this was the problem. Now both the previously posted codes work fine. My sincere gratitute for your help. Gerry you were correct that line of code is not required. Thanks. I also came up with a bit slower approach to ID and store found paragraphs which I thought I might as well post. Dave
ps. no Word reference required

Option Base 1
Sub FindParagraphNo()
'display paragraph number of found para(s)
Dim Mytopic1 As String, Wapp As Object, Parcount As Variant
Dim ParaArray() As Variant, Cnt As Integer, Cnt2 As Integer
Dim Msgstrng As String, Cnt3 As Integer
Mytopic1 = "seed" 'search word change as needed
On Error GoTo ErFix
Set Wapp = CreateObject("Word.Application")
'change filename below as needed
Wapp.documents.Open Filename:="C:/Records/Summary.doc", ReadOnly:=True
For Each Parcount In Wapp.ActiveDocument.Paragraphs
Cnt = Cnt + 1
Parcount.Range.Select
With Wapp.Selection.Find
.Execute FindText:=Mytopic1, Format:=False, Forward:=True
If .Found = True Then
Cnt2 = Cnt2 + 1
ReDim Preserve ParaArray(Cnt2)
ParaArray(Cnt2) = Cnt
End If
End With

Next Parcount
Wapp.Quit
Set Wapp = Nothing
'make msg
Msgstrng = ParaArray(UBound(ParaArray))
For Cnt3 = UBound(ParaArray) - 1 To LBound(ParaArray) Step -1
Msgstrng = Msgstrng & " , " & ParaArray(Cnt3)
Next Cnt3
MsgBox "Paragraph numbers: " & Msgstrng
Exit Sub
ErFix:: On Error GoTo 0
MsgBox "File error"
Wapp.Quit
Set Wapp = Nothing
End Sub

Dave
04-19-2006, 05:28 AM
Just to wrap this thread up, this following line of code placed after the file open fixes/avoids the problems I encountered. Again thanks for the help. I'll mark this thread solved if it will let me. Dave

Wapp.Options.Pagination = True