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.
Thread starter Similar threads Forum Replies Date
S VBA to modify appointment item in additional Exchange account doesn't work Using Outlook 0
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
B Outlook 2007, must modify incomming e-mail addresses to reply Using Outlook 3
A Auto modify an email used in 'Server reply using'. Using Outlook 1
C modify user password programmatically Exchange Server Administration 9
H Use group policy to block access to modify account settings outlook/exchange 2010. Using Outlook 1
P Will 'ldap modify' command work on some Active Directory fields? Using Outlook 2
U How modify attachment in an email Using Outlook 5
S Unable to create or modify mailboxes in Exchange 2010 after modifying AD with ADSIEDIT Exchange Server Administration 4
W How can I modify an email address when I reply to it? [Outlook 2003] Using Outlook 3
L not able to access signatures to modify, add, or edit. Using Outlook 1
C unable to modify tags for categories in outlook 2010 Using Outlook 1
T Is there a way to bulk modify the alias field in a Exchange 2010 enviornment? Exchange Server Administration 2
K Adminstrator unable to modify any of the settings in Exchange 2010 - insufficient access rights Exchange Server Administration 4
J Rights to modify all distribution lists Exchange Server Administration 3
Z Unable to configure an IPv4 address because IPv4 is not available.Attempt to modify the IP addresses Exchange Server Administration 4
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
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
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
Similar threads


















































Top