modify vba to run it as script rule

Omar

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I'm trying to modify the following code to use it as rule Script but for some reason I can not figure it out how to modify the following line

Code:
For Each Item In Application.ActiveExplorer.Selection

here is the full code

Code:
Option Explicit
Public Sub UpdateReport(Item As Outlook.MailItem)
  Dim xlApp As Excel.Application
  Dim xlWB As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
  Dim vText As Variant
  Dim sText As String
  Dim vItem As Variant
  Dim i As Long
  Dim rCount As Long
  Dim bXStarted As Boolean
  Dim FileName As String
  Dim FilePath As String '// SaveAs CSV File Path
  Dim sPath As String '// .CSV File Path
  
  '// the path of the workbook
  sPath = "C:\temp\temp.csv"
  
  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
  '// Open the workbook to input the data
  Set xlWB = xlApp.Workbooks.Open(sPath)
  Set xlSheet = xlWB.Sheets("Oracle Order")
  
  '// Process Mail Item in ?
  For Each Item In Application.ActiveExplorer.Selection
  sText = Item.Body
  vText = Split(sText, Chr(13)) ' Chr(13)) carriage return
  
  '// Find the next empty line of the worksheet
  rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
  rCount = rCount + 1
  
  '// Check each line of text in the message body
  For i = UBound(vText) To 0 Step -1
  
  '// Customer
  If InStr(1, vText(i), "Customer") > 0 Then
  vItem = Split(vText(i), Chr(9)) ' Chr(9) horizontal tab
  xlSheet.Range("A" & rCount) = Trim(vItem(1))
  End If
  
  '// Order #
  If InStr(1, vText(i), "Order #") > 0 Then
  vItem = Split(vText(i), Chr(9))
  xlSheet.Range("B" & rCount) = Trim(vItem(1))
  End If
  
  
  '// Service Level
  If InStr(1, vText(i), "Service Level") > 0 Then
  vItem = Split(vText(i), Chr(9))
  xlSheet.Range("J" & rCount) = Trim(vItem(1))
  End If
  Next i
  FilePath = Environ("USERPROFILE") & "\Documents\Temp\"
  FileName = Sheets(1).Range("B2").Value
  xlWB.SaveAs FileName:=FilePath & FileName
  Next Item
  
  '// Close & SaveChanges
  xlWB.Close SaveChanges:=True
  If bXStarted Then
  xlApp.Quit
  End If
  
  Set xlApp = Nothing
  Set xlWB = Nothing
  Set xlSheet = Nothing
  Set Item = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You need to remove that line (obviously) because run a script only processes the newly arrived message.

"Item" in Public Sub UpdateReport(Item As Outlook.MailItem) tells outlook to refer to the newly arrived message as "Item" - it looks like you only need to remove that line and the Next Item at the end and it should work.
 

Omar

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Diane thanks for taking the time to help me.

when I remove that line, and run test it gave me a
Rule in Error ( The Script "" doesn't exist or is invalid)
Code:
For Each Item In Application.ActiveExplorer.Selection

Next Item
 

Omar

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I did Michael, here is what the code looks like now.

Code:
Option Explicit
Public Sub Test(Item As Outlook.MailItem)
    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim vText As Variant
    Dim sText As String
    Dim vItem As Variant
    Dim i As Long
    Dim rCount As Long
    Dim XStarted As Boolean
    Dim FileName As String
    Dim FilePath As String '// SaveAs CSV File Path
    Dim sPath As String '// .CSV File Path

    '// the path of the workbook
    sPath = "C:\temp\temp.csv"

    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")
        XStarted = True
    End If

    On Error GoTo 0
    '// Open the workbook to input the data
    Set xlWB = xlApp.Workbooks.Open(sPath)
    Set xlSheet = xlWB.Sheets("Report")

    '// Process Mail body
    sText = Item.Body
    vText = Split(sText, Chr(13)) ' Chr(13)) carriage return

    '// Find the next empty line of the worksheet
    rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
    rCount = rCount + 1

    '// Check each line of text in the message body
    For i = UBound(vText) To 0 Step -1

        '// Customer Name
        If InStr(1, vText(i), "Customer") > 0 Then
            vItem = Split(vText(i), Chr(9)) ' Chr(9) horizontal tab
            xlSheet.Range("A" & rCount) = Trim(vItem(1))
        End If

        '// Order Number
        If InStr(1, vText(i), "Order #") > 0 Then
            vItem = Split(vText(i), Chr(9))
            xlSheet.Range("B" & rCount) = Trim(vItem(1))
        End If


        '// Service Level
        If InStr(1, vText(i), "Service Level") > 0 Then
            vItem = Split(vText(i), Chr(9))
            xlSheet.Range("J" & rCount) = Trim(vItem(1))
        End If
    Next i
   
    FilePath = Environ("USERPROFILE") & "\Documents\Temp\"
    FileName = xlWB.Sheets(1).Range("B2").Value
    xlWB.SaveAs FileName:=FilePath & FileName

    '// Close & SaveChanges
    xlWB.Close SaveChanges:=True
    If XStarted Then
        xlApp.Quit
    End If

    Set xlApp = Nothing
    Set xlWB = Nothing
    Set xlSheet = Nothing
    Set Item = Nothing
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Did you maybe move the script to another module? What happens if you edit the rule, and select the rule again?
 

Omar

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
okay it is working now, I have deleted the rule and re-created. Thank you Michael & Diane.
 

Rolando

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Mine is failing too. It only works for me manually if I name it Public Sub Test() without Option Explicit

2773
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It's not going to run in a rule because you have it working on select or open messages.

You just need the reply code at the end (and the strBody of course)
Code:
Sub ReplytoMessage(Item As Outlook.MailItem)

Set myReply = Item.replyall
strbody = "whatever"

'handle plain text or html format
If Item.BodyFormat = olFormatHTML Then
    myReply.HTMLBody = strbody  & vbCrLf & myReply.HTMLBody
 Else
     myReply.Body = strbody & vbCrLf & myReply.Body
End If

myReply.Display ' change to send after testing

End Sub

 
Top