Hi there,
I pulled code added by Diane Poremsky from slipstick systems and made what I thought were the necessary changes(still learning vba) to copy an email body to excel. I get the runtime 5020 Application-defined or object-defined error. The code runs through opens the excel file and errors at 'If Reg1.Test(sText) Then'. It does seem to detect the email body text, but not sure where to go from here. If I need to add the folder object and/or what my issue is? Any help is greatly appreciated. Best, Cindy
Here is the code:
[ Sub CopyToExcel()
Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText As Variant
Dim sText As String
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Set olItem = Application.ActiveExplorer().Selection(1)
enviro = CStr(Environ("USERPROFILE"))
strPath = "P:\Operations\Votes Log\TESTSchedule.xlsm"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Testing")
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
rCount = rCount + 1
sText = olItem.Body
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Pattern = "((Vote With\s*)\w*\)\s*(\w*)\s*(\w*)\s*"
End With
If Reg1.Test(sText) Then
Set M1 = Reg1.Execute(sText)
For Each M In M1
vText = Trim(M.SubMatches(1))
Next
End If
xlSheet.Range("B" & rCount) = vText
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
Set M = Nothing
Set M1 = Nothing
Set Reg1 = Nothing
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub]
I pulled code added by Diane Poremsky from slipstick systems and made what I thought were the necessary changes(still learning vba) to copy an email body to excel. I get the runtime 5020 Application-defined or object-defined error. The code runs through opens the excel file and errors at 'If Reg1.Test(sText) Then'. It does seem to detect the email body text, but not sure where to go from here. If I need to add the folder object and/or what my issue is? Any help is greatly appreciated. Best, Cindy
Here is the code:
[ Sub CopyToExcel()
Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText As Variant
Dim sText As String
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Set olItem = Application.ActiveExplorer().Selection(1)
enviro = CStr(Environ("USERPROFILE"))
strPath = "P:\Operations\Votes Log\TESTSchedule.xlsm"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Testing")
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
rCount = rCount + 1
sText = olItem.Body
Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
.Pattern = "((Vote With\s*)\w*\)\s*(\w*)\s*(\w*)\s*"
End With
If Reg1.Test(sText) Then
Set M1 = Reg1.Execute(sText)
For Each M In M1
vText = Trim(M.SubMatches(1))
Next
End If
xlSheet.Range("B" & rCount) = vText
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
Set M = Nothing
Set M1 = Nothing
Set Reg1 = Nothing
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub]