Code to extract subject line

A

acampbell012

I found this code from an '02 posting which is supposed to extract the
subject line from a designated Outlook folder. The code crashes on the
line "AscName = Asc(CharName)"; Error 5, "Invalid procedure call or
argument".

I am using Outlook 2003 and would like to utilize this code to export
the subject line.

Thanks.

Alan


Sub ProcessTheInbox()


Dim fName As String
Dim i As Long
Dim oApp As Outlook.Application
Dim oFoldMail As Object
Dim oItem As Outlook.MailItem
Dim oNameSp As Outlook.NameSpace
Dim x As Long
Dim strSubject As String
Dim sName As String
Dim z As Long


'Open or attach to Outlook
Set oApp = New Outlook.Application
Set oNameSp = oApp.GetNamespace("MAPI")


'Attach to the appropriate folders
Set oFoldMail = oNameSp.GetDefaultFolder(6) '6=Inbox


'Process mail items
z = 0
x = oFoldMail.Items.Count
MsgBox "The number of emails in " & CStr(oFoldMail) & " is " &
CStr(x)
Open "c:\output.txt" For Output As #1


For i = x To 1 Step -1
Set oItem = oFoldMail.Items.Item(i)
strSubject = oItem.Subject
If Left(strSubject, 4) = "test" Or Left(strSubject, 4) = "TEST"
Then


z = z + 1
sName = TrimUserName(strSubject)
MsgBox "The name returned is " & sName
Write #1, sName
End If


Next i
MsgBox "The number of emails processed is " & CStr(z)


Close #1
Set oApp = Nothing
Set oFoldMail = Nothing
Set oItem = Nothing
Set oNameSp = Nothing


End Sub


Function TrimUserName(ByVal Subjectline)


Dim AscName
Dim RightName
Dim i As Integer
Dim iFoundPos As Integer
Dim iLen As Integer


iLen = Len(Subjectline)
MsgBox Subjectline & " = Length of Subject field is " & Str(iLen)
AscName = 0
iFoundPos = 0
For i = 0 To iLen
Do While Not (AscName = 35) '# delimiter


iFoundPos = iFoundPos + 1
CharName = Mid(UCase(Subjectline), i + 1, 1) 'trawl through
the
Subject
AscName = Asc(CharName)
i = i + 1


Loop
Next
RightName = Right(Subjectline, (iLen - iFoundPos))
TrimUserName = RightName
End Function
 
M

Michael Bauer [MVP - Outlook]

Am 24 Aug 2006 11:45:06 -0700 schrieb (e-mail address removed):

Please ensure that the CharName variable isn´t empty.
 
A

acampbell012

It is not empty but, it is blank (i.e. "")

Just to test the results, I changed CharName = Mid(UCase(Subjectline),
i + 1, 1) to

CharName = Mid(UCase(Subjectline), i - 1, 1) and it shows empty.

I do some VBA programming in Excel but, Outlook is new to me. I am not
sure what this function is attempting to do within the sub in order to
diagnose why it crashes. I have tried several mods to the search
string, testing known subject line in my inbox and have had no success.
I read help on Asc function, but not sure what purpose it is serving in
this code/function.

Appreciate any additonal feedback you can provide.
 
M

Michael Bauer [MVP - Outlook]

Am 25 Aug 2006 07:59:58 -0700 schrieb (e-mail address removed):

With "empty" I mean also blank. If the string is blank ("") then an error
occurs.
 

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