modify vba to run it as script rule

Status
Not open for further replies.

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


 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S VBA to modify appointment item in additional Exchange account doesn't work Using Outlook 0
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
R Outlook 2010 Modify Style "Do not check spelling or grammar" not saving Outlook VBA and Custom Forms 0
Justo Horrillo It's possible to modify the behaviour of the conversation option? Using Outlook 2
S Modify account template? BCM (Business Contact Manager) 0
R Can't modify Outlook view font with IE anymore (even though IE still affects print font) Using Outlook 5
smokiibear How to modify Today Page to Include tasks from other task folders Using Outlook 1
S Modify recurring outlook invites based on form entry Using Outlook 0
C How To Open Outlook Attachment in Excel, Modify some Data, then Re-Save It Using Outlook 3
A Possible to modify people preview window? Using Outlook 1
R How to modify Outlook Select Rooms form columns Using Outlook 1
G Modify Subject line of incoming mail where I am in Cc line Using Outlook 1
N modify New mesage SEND button Outlook VBA and Custom Forms 1
R Custom Archive code -- modify my code! Outlook VBA and Custom Forms 3
C How do I grab a field property (like .value) so I can modify it via OL code? Outlook VBA and Custom Forms 4
A Script to either modify "from" address or prevent a reply being se Outlook VBA and Custom Forms 2
B Can't modify PR_CLIENT_SUBMIT_TIME? Outlook VBA and Custom Forms 1
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 3
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1

Similar threads

Top