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
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 0
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8

Similar threads

Top