PDA

View Full Version : Find the End of the Line with the Cursor



kingraw
04-20-2011, 05:22 PM
BEGINNING OF DOC----

TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
TEXTTEXTTEXTTEXT TXT

TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
TEXTTEXTTEXTTEXT TXT

TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
TEXTTEXTTEXTTEXT TXT

END OF DOC------

RESULT DESIRED BY CODE—
BEGINNING OF DOC------
TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
INSERTED TEXT
TEXTTEXTTEXTTEXT TXT

TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
INSERTED TEXT
TEXTTEXTTEXTTEXT TXT

TEXTTEXTTEXTTEXT TEXT
HGL;TEXTTEXTTEXT TEXT
INSERTED TEXT
TEXTTEXTTEXTTEXT TXT

END OF DOC------

This is what I have so far.

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("U:\whereverthe.doc")
Set objSelection = objWord.Selection

objSelection.Find.Forward = True
objSelection.Find.Text = "HGL;"
objSelection.Find.Execute
Do While True
objSelection.Find.Execute
If objSelection.Find.Found Then
objSelection.TypeText(Chr(11))
objSelection.TypeText "INSERTED TEXT "
Else
Exit Do
End If
Loop

Problem is I get it to find the Line where an HGL; is but I can get it to the cursor to the end of the line and I cant use Selection.EndKey Unit:=wdLine cause it keeps giving me an error.
So can someone give me an alternative method to moving the cursor to the end of the line without selecting or highlight the rest of the text so I can do a lean shift+enter to make a new line and insert text.

Sorry for the Length of my post. I just wanted to be clear also to clarify.
The program needs to Find every line that has a HGL; on it and create a new line under it(without disturbing the text on the line the HGL; is on or that underneath it) and a insert new text message.

I'm doing this inside a word doc. Code being ran inside a notepad with the ext. .vbs simple and plain.

Thanks in Advance

gmaxey
04-20-2011, 05:56 PM
Try something along these lines:

Sub ScratchMacro()
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim oRngDuplicate As Word.Range
Set oDoc = ActiveDocument
Set oRng = oDoc.Range
With oRng.Find
.Text = "HGL;"
Do While .Execute
oRng.Collapse wdCollapseEnd
Set oRngDuplicate = oRng.Duplicate
oRngDuplicate.MoveStartUntil Chr(11)
oRngDuplicate.MoveStart wdCharacter, 1
oRngDuplicate.InsertAfter "INSERTED TEXT" & vbLf
Loop
End With
End Sub