[O2007] -> How to search for a string in the body and get the line number?

Status
Not open for further replies.
S

Steve JORDI

Hi,

I'm trying to get a reply to a mail message and look for a specific

substring.

Then I need to know the position of the line.

For example:

search "Sincerely yours,"

get the line number

insert 3 lines BEFORE the "Sincerly Yours"

I can't find a way. I know how to use Replace to change

subtrings, but I can't get a way to just locate one and get

the line number.

How can I do this?

Then, how can I insert text at that linePosition-3?

Thanks for any help.

Sincerely,

Steve JORDI

(Remove the K_I_L_LSPAM from my email address)

----------------1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com

Switzerland WWW: www.sjordi.com

----------------Volcanoes at www.sjordi.com/volcanoes

MovieDB at www.sjmoviedb.com

------------------------------------------------
 
RE: [O2007] -> How to search for a string in the body and get the line

Steve-

There may be a better way, but here is some code that might help. This code

takes an email and splits lines into an array. You could then loop from

lbound(arrayname) to ubound(arrayname) and use Instr to find the line with

your target string.

At that point, you would know from the loop what (line) number the string

was found on, and be able to go back to the source message and replace the

line n-3 with x = y & vbcrlf & x

where x is the original line, and you are inserting your target text

followed by the line that was already there.

For the following code, you can just ignore all the Excel stuff, and just

focus on the split statement and array.

Sub ParseEmailFolderToExcel()

Set objApp = Application

Set olns = Outlook.GetNamespace("MAPI")

Set myinbox = olns.PickFolder

'Set myItems = myinbox.Items

Dim XLApp As Excel.Application

Dim wkb As Excel.Workbook

Dim wks As Excel.Worksheet

Dim ExcelWasNotRunning As Boolean

On Error Resume Next

Set XLApp = GetObject(, "Excel.Application")

If Err Then

ExcelWasNotRunning = True

Set XLApp = New Excel.Application

XLApp.Visible = True

End If

Set wkb = XLApp.Workbooks.Add

Set wks = wkb.Sheets(1)

With wks

StartCount = 0

strEmailContents = ""

For Each outlookmessage In myinbox.Items

StartCount = StartCount + 1

> Range("A" & StartCount).Value = outlookmessage.ReceivedTime

> Range("B" & StartCount).Value = outlookmessage.SenderName

> Range("C" & StartCount).Value = outlookmessage.Subject

FullMsg = outlookmessage.Body

AllLines = Split(FullMsg, vbCrLf)

On Error Resume Next

eachval = Split(AllLines(2), """,""") 'I was just grabbing the

second line

> Range("D" & StartCount).Value = Right(eachval(0),

Len(eachval(0)) - 1)

> Range("E" & StartCount).Value = eachval(1)

> Range("F" & StartCount).Value = eachval(2)

> Range("G" & StartCount).Value = eachval(3)

> Range("H" & StartCount).Value = eachval(4)

> Range("I" & StartCount).Value = Left(eachval(5),

Len(eachval(5)) - 2)

On Error GoTo 0

'you'd need to use something like:

For eVal = lbound(eachVal) to ubound(eachval)

'the ucase and all caps are to find a match without being

case sensitive

'if you eliminate the ucase and try to match "Sincerely

Yours" then

'it would not find the line "Sincerely yours" for example.

FoundString = Instr(UCASE(eachVal(eVal)),"SINCERELY YOURS,")

If(Not(IsERROR(FoundString))) then

'Do your replacement code on line eVal-3 (or would that

be eVal -2?)

End If

Next

Next

End With

Set myOlApp = Nothing

Set olns = Nothing

Set myinbox = Nothing

Set myItems = Nothing

End Sub

"Steve JORDI" wrote:


> Hi,
> I'm trying to get a reply to a mail message and look for a specific
> substring.
> Then I need to know the position of the line.

> For example:
> search "Sincerely yours,"
> get the line number
> insert 3 lines BEFORE the "Sincerly Yours"

> I can't find a way. I know how to use Replace to change
> subtrings, but I can't get a way to just locate one and get
> the line number.

> How can I do this?

> Then, how can I insert text at that linePosition-3?

> Thanks for any help.

> Sincerely,
> Steve JORDI

> (Remove the K_I_L_LSPAM from my email address)
> ----------------------------------------------> 1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
> Switzerland WWW: www.sjordi.com
> ----------------------------------------------> Volcanoes at www.sjordi.com/volcanoes
> MovieDB at www.sjmoviedb.com
> ----------------------------------------------> .
>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S ->[O2007] Any way to know the Plain Text Wrap limit? Outlook VBA and Custom Forms 2
S ->[O2007] Parsing each line of a MailItem HTMLBody? Outlook VBA and Custom Forms 2
S -> [O2007]: get the message HTML original source code? Outlook VBA and Custom Forms 6
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 7
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
A 'search people' now asks me to 'press enter' Using Outlook 11
A Relocate Search Bar in Outlook Using Outlook 2
A Search folder and move the email Outlook VBA and Custom Forms 0
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
T How to find or display the sub-folder name for an Archive Search Using Outlook 10
P Search folder: all emails sent to or from a domain Using Outlook 1
T Outlook365 search item listed as "potential matches" can't be opened Using Outlook 0
Victor_50 Outlook 2019 Jump to folder from search folder Outlook VBA and Custom Forms 0
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
J Outlook search bar in Office 2021 Professional Using Outlook 1
T Outlook 365 Search Box reverting to old location Using Outlook 2
P Outlook 2013 search no longer works Using Outlook 2
C How to search for items in Outbox with multiple accounts? Using Outlook 20
J search doesn't find anything Using Outlook 1
kkqq1122 How would I add Search for attachment name Outlook VBA and Custom Forms 3
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
C Search not accurate, but not indexing Using Outlook 9
J How do you disable address search box when typing @ in body of email? Using Outlook 0
H Outlook 2019 intermittent search results Using Outlook 0
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
D Advanced e-Mail search on from/to contact group only searches for first 20 contacts in group Using Outlook 0
mll persistently customise columns in outlook advanced search Using Outlook 3
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
H Search Email Header for Content Type Outlook VBA and Custom Forms 1
P Posts in Folder No Longer Group by Conversation Column After Search Using Outlook 0
O Advanced search - can I use booleans Using Outlook 3
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
M Reverting The Outlook Search Box Location (or other undesired additions) Using Outlook 1
M Something went wrong and your search couldn't be completed Using Outlook 1
M Disable Contact Card Results when using "Search People" in Outlook Ribbon Using Outlook 7
J outlook 2007 doesn't let me choose which .pst to search Using Outlook 2
P outlook 2008 search box criteria couldn't be saved Using Outlook 2
C Outlook with Office365 - search across account, by date rate, in multiple folders - how? Using Outlook 2
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
L New Location for the Search Bar Using Outlook 6
T How can you include Junk Email in Search Results like you can include Deleted Items? Using Outlook 3
J Outlook 2016 After a search in all mailboxes, where is each message that was found? Using Outlook 6
King Mustard Sort search groups by amount of items? Using Outlook 1
V Outlook 2016 will not move emails in search results Using Outlook 4
T Search for incoming e-mails for a specified time range Using Outlook 1
S outlook 2007 calendar search Using Outlook 6

Similar threads

Back
Top