I want to create a Word Macro that can identify a line that starts

J

John

I have a very large Word document

I would love to be able to create a macro to carry out a very specific
task.

Here are the instructions in plain English:

- Find the next line (i.e. search 'down' in document) to look for a
line beginning with 'Diagnosis: '
- I then want to copy that entire line EXCEPT the word
'Diagnosis' [and send it to the clipboard]

This is something that's easy for a human to do, I just don't know if
there's any code that can achieve the same result. (I don't even know
if there's actually any code that tell Word to send something to the
clipboard)

(The reason I want to write a macro for this is that it will be done
hundreds of times)


Any help would be greatly appreciated :)
John
 
D

Doug Robbins - Word MVP

The following code will copy the balance of the line that contains
"Diagnosis:" to the clipboard:

Dim rng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Diagnosis:", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) =
True
Set rng = Selection.Range
rng.End = ActiveDocument.Bookmarks("\Line").Range.End
rng.Start = Selection.Range.End
rng.Copy
Loop
End With
 
V

vik ram

The following code will copy the balance of the line that contains
"Diagnosis:" to the clipboard:

    Dim rng As Range
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(FindText:="Diagnosis:", Forward:=True, _
            MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) =
True
            Set rng = Selection.Range
            rng.End = ActiveDocument.Bookmarks("\Line").Range.End
            rng.Start = Selection.Range.End
            rng.Copy
        Loop
    End With

--
Hope this helps,

Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org




I have a very large Word document
I would love to be able to create a macro to carry out a very specific
task.
Here are the instructions in plain English:
- Find the next line (i.e. search 'down' in document) to look for a
line beginning with 'Diagnosis: '
- I then want to copy that entire line EXCEPT the word
'Diagnosis' [and send it to the clipboard]
This is something that's easy for a human to do, I just don't know if
there's any code that can achieve the same result. (I don't even know
if there's actually any code that tell Word to send something to the
clipboard)
(The reason I want to write a macro for this is that it will be done
hundreds of times)
Any help would be greatly appreciated :)
John- Hide quoted text -

- Show quoted text -

Thanks for the response Doug, but when I tried it, I think it went
through the entire document, possibly sending stuff to the clipboard
along the way. I just want to to look for the next 'Diagnosis: ' it
finds, and send it to the clipboard. (I will come back to the document
to look at the other diangoses later)

:)


[Below is what I entered]

Sub sendDIAGNOSIStoCLIPBOARD()
Dim rng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Diagnosis:", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False)
= True
Set rng = Selection.Range
rng.End = ActiveDocument.Bookmarks("\Line").Range.End
rng.Start = Selection.Range.End
rng.Copy
Loop
End With


End Sub
 
D

Doug Robbins - Word MVP

If you start with the selection at the beginning of the document, the first
time the following code is run, it will handle the first Diagnosis: in the
document. If it is run a second time (without moving the selection before
the first Diagnosis:, it will then proceed to handle the next Diagnosis:

Dim rng As Range
' Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="Diagnosis:", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False
Set rng = Selection.Range
rng.End = ActiveDocument.Bookmarks("\Line").Range.End
rng.Start = Selection.Range.End
MsgBox rng.Text
rng.Copy
End With


--
Hope this helps,

Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org

vik ram said:
The following code will copy the balance of the line that contains
"Diagnosis:" to the clipboard:

Dim rng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Diagnosis:", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) =
True
Set rng = Selection.Range
rng.End = ActiveDocument.Bookmarks("\Line").Range.End
rng.Start = Selection.Range.End
rng.Copy
Loop
End With

--
Hope this helps,

Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org




I have a very large Word document
I would love to be able to create a macro to carry out a very specific
task.
Here are the instructions in plain English:
- Find the next line (i.e. search 'down' in document) to look for a
line beginning with 'Diagnosis: '
- I then want to copy that entire line EXCEPT the word
'Diagnosis' [and send it to the clipboard]
This is something that's easy for a human to do, I just don't know if
there's any code that can achieve the same result. (I don't even know
if there's actually any code that tell Word to send something to the
clipboard)
(The reason I want to write a macro for this is that it will be done
hundreds of times)
Any help would be greatly appreciated :)
John- Hide quoted text -

- Show quoted text -

Thanks for the response Doug, but when I tried it, I think it went
through the entire document, possibly sending stuff to the clipboard
along the way. I just want to to look for the next 'Diagnosis: ' it
finds, and send it to the clipboard. (I will come back to the document
to look at the other diangoses later)

:)


[Below is what I entered]

Sub sendDIAGNOSIStoCLIPBOARD()
Dim rng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="Diagnosis:", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False)
= True
Set rng = Selection.Range
rng.End = ActiveDocument.Bookmarks("\Line").Range.End
rng.Start = Selection.Range.End
rng.Copy
Loop
End With


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top