use vba to search for subject in document

C

Co

Hi All,

I'm trying to search in a doc for a subject line or lines.

sSubject = Selection
While InStr(sSubject, Chr(13)) = 0
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
sSubject = Selection
Wend
sSubject = Left(sSubject, Len(sSubject) - 1)

I tried as you can see to find a Chr(13) and then end the search but
that doesn't always work.
Sometimes a Return is given and then the sentence continues on the
next line.

I thought maybe search for a blank line but that doesn't work,
sometimes you have:

Subj: This is the subject line
which is followed by a new line
From: Me

Can anyone tell me how to solve this?

Marco
 
J

Jan Hyde (VB MVP)

Co <[email protected]>'s wild thoughts were released
Hi All,

I'm trying to search in a doc for a subject line or lines.

sSubject = Selection
While InStr(sSubject, Chr(13)) = 0
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
sSubject = Selection
Wend
sSubject = Left(sSubject, Len(sSubject) - 1)

I tried as you can see to find a Chr(13) and then end the search but
that doesn't always work.
Sometimes a Return is given and then the sentence continues on the
next line.

I thought maybe search for a blank line but that doesn't work,
sometimes you have:

Subj: This is the subject line
which is followed by a new line
From: Me

Can anyone tell me how to solve this?

You need to decide what the rule is.

For example, you could choose to take all the text between
'Subj: and 'From:'
 
C

Co

Co <[email protected]>'s wild thoughts were released








You need to decide what the rule is.

For example, you could choose to take all the text between
'Subj: and 'From:'

Yes,

problem is I made this for a number of docs which all have different
layouts.
 
D

Doug Robbins - Word MVP

Dim myrange as Range.

Set myrange = Selection.Range
myrange.End = Selection.Paragraphs(1).Range.End

myrange should then contain the text that you want.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

Co

Dim myrange as Range.

Set myrange = Selection.Range
myrange.End = Selection.Paragraphs(1).Range.End

myrange should then contain the text that you want.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP













- Tekst uit oorspronkelijk bericht weergeven -

Doug,

I tried your code but still just get one line of the multi-line
subject.
Could you show me where to put it in my code?

Marco
 
D

Doug Robbins - Word MVP

In that case, rather than being multi-line, your "subject" must be multiple
paragraphs. How many? If it is n, then use

Dim myrange As Range
Set myrange = Selection.Range
myrange.End = Selection.Paragraphs(1).Range.End
myrange.MoveEnd Unit:=wdParagraph, Count:=n

myrange will then contain your subject.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Dim myrange as Range.

Set myrange = Selection.Range
myrange.End = Selection.Paragraphs(1).Range.End

myrange should then contain the text that you want.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP













- Tekst uit oorspronkelijk bericht weergeven -

Doug,

I tried your code but still just get one line of the multi-line
subject.
Could you show me where to put it in my code?

Marco
 

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