Opening an Excel workbook using Outlook rule and Outlook VBA - Outlook script runs twice
up vote
-1
down vote
favorite
I'm trying to automate a report, the code for which is in an excel workbook with a Workbook_Open
macro. The Outlook macro is supposed to trigger upon receiving an email, download an attachment, and subsequently open an Excel file (which should then run a macro and close itself).
The code currently triggers twice whenever an email arrives in my inbox that satisfies the rule criteria (containing some attachment). I can't figure out why. My full code is below. Thank you in advance.
Sub saveAttach(item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim wbOpen1 As Excel.Workbook
Dim wbOpen2 As Excel.Workbook
Dim object_attachment As Outlook.Attachment
Set xlApp = New Excel.Application
With xlApp
.Visible = True
.EnableEvents = True
.UserControl = False
.DisplayAlerts = False
.AskToUpdateLinks = False
End With
Dim saveFolder As String
Dim sTemp As String
saveFolder = "C:UsersmystuffDownloadssavedest"
For Each object_attachment In item.Attachments
If InStr(object_attachment.DisplayName, ".csv") Then
sTemp = object_attachment.DisplayName
sTemp = Left(sTemp, Len(sTemp) - 4)
On Error Resume Next
If IsFile(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".xlsm") = False Then
object_attachment.SaveAsFile saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"
Set wbOpen1 = xlApp.Workbooks.Open(FileName:=(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"))
End If
End If
Next
Set wbOpen2 = xlApp.Workbooks.Open(FileName:="C:UsersmystuffDownloadssavedestmyfile.xlsm")
Set wbOpen1 = Nothing
Set wbOpen2 = Nothing
Set xlApp = Nothing
End Sub
Function IsFile(ByVal fName As String) As Boolean
On Error Resume Next
IsFile = (GetAttr(fName))
End Function
excel vba excel-vba outlook outlook-vba
|
show 3 more comments
up vote
-1
down vote
favorite
I'm trying to automate a report, the code for which is in an excel workbook with a Workbook_Open
macro. The Outlook macro is supposed to trigger upon receiving an email, download an attachment, and subsequently open an Excel file (which should then run a macro and close itself).
The code currently triggers twice whenever an email arrives in my inbox that satisfies the rule criteria (containing some attachment). I can't figure out why. My full code is below. Thank you in advance.
Sub saveAttach(item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim wbOpen1 As Excel.Workbook
Dim wbOpen2 As Excel.Workbook
Dim object_attachment As Outlook.Attachment
Set xlApp = New Excel.Application
With xlApp
.Visible = True
.EnableEvents = True
.UserControl = False
.DisplayAlerts = False
.AskToUpdateLinks = False
End With
Dim saveFolder As String
Dim sTemp As String
saveFolder = "C:UsersmystuffDownloadssavedest"
For Each object_attachment In item.Attachments
If InStr(object_attachment.DisplayName, ".csv") Then
sTemp = object_attachment.DisplayName
sTemp = Left(sTemp, Len(sTemp) - 4)
On Error Resume Next
If IsFile(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".xlsm") = False Then
object_attachment.SaveAsFile saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"
Set wbOpen1 = xlApp.Workbooks.Open(FileName:=(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"))
End If
End If
Next
Set wbOpen2 = xlApp.Workbooks.Open(FileName:="C:UsersmystuffDownloadssavedestmyfile.xlsm")
Set wbOpen1 = Nothing
Set wbOpen2 = Nothing
Set xlApp = Nothing
End Sub
Function IsFile(ByVal fName As String) As Boolean
On Error Resume Next
IsFile = (GetAttr(fName))
End Function
excel vba excel-vba outlook outlook-vba
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
You have usedOn Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.
– Tony Dallimore
Nov 11 at 0:56
What isIsFile
supposed to do? It appears to me that no file named fName will exist soGetAttr
will give an error which you have ignored. I have not tried your code but my guess isIsFile
always returnsFalse
.
– Tony Dallimore
Nov 11 at 0:59
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11
|
show 3 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to automate a report, the code for which is in an excel workbook with a Workbook_Open
macro. The Outlook macro is supposed to trigger upon receiving an email, download an attachment, and subsequently open an Excel file (which should then run a macro and close itself).
The code currently triggers twice whenever an email arrives in my inbox that satisfies the rule criteria (containing some attachment). I can't figure out why. My full code is below. Thank you in advance.
Sub saveAttach(item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim wbOpen1 As Excel.Workbook
Dim wbOpen2 As Excel.Workbook
Dim object_attachment As Outlook.Attachment
Set xlApp = New Excel.Application
With xlApp
.Visible = True
.EnableEvents = True
.UserControl = False
.DisplayAlerts = False
.AskToUpdateLinks = False
End With
Dim saveFolder As String
Dim sTemp As String
saveFolder = "C:UsersmystuffDownloadssavedest"
For Each object_attachment In item.Attachments
If InStr(object_attachment.DisplayName, ".csv") Then
sTemp = object_attachment.DisplayName
sTemp = Left(sTemp, Len(sTemp) - 4)
On Error Resume Next
If IsFile(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".xlsm") = False Then
object_attachment.SaveAsFile saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"
Set wbOpen1 = xlApp.Workbooks.Open(FileName:=(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"))
End If
End If
Next
Set wbOpen2 = xlApp.Workbooks.Open(FileName:="C:UsersmystuffDownloadssavedestmyfile.xlsm")
Set wbOpen1 = Nothing
Set wbOpen2 = Nothing
Set xlApp = Nothing
End Sub
Function IsFile(ByVal fName As String) As Boolean
On Error Resume Next
IsFile = (GetAttr(fName))
End Function
excel vba excel-vba outlook outlook-vba
I'm trying to automate a report, the code for which is in an excel workbook with a Workbook_Open
macro. The Outlook macro is supposed to trigger upon receiving an email, download an attachment, and subsequently open an Excel file (which should then run a macro and close itself).
The code currently triggers twice whenever an email arrives in my inbox that satisfies the rule criteria (containing some attachment). I can't figure out why. My full code is below. Thank you in advance.
Sub saveAttach(item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim wbOpen1 As Excel.Workbook
Dim wbOpen2 As Excel.Workbook
Dim object_attachment As Outlook.Attachment
Set xlApp = New Excel.Application
With xlApp
.Visible = True
.EnableEvents = True
.UserControl = False
.DisplayAlerts = False
.AskToUpdateLinks = False
End With
Dim saveFolder As String
Dim sTemp As String
saveFolder = "C:UsersmystuffDownloadssavedest"
For Each object_attachment In item.Attachments
If InStr(object_attachment.DisplayName, ".csv") Then
sTemp = object_attachment.DisplayName
sTemp = Left(sTemp, Len(sTemp) - 4)
On Error Resume Next
If IsFile(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".xlsm") = False Then
object_attachment.SaveAsFile saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"
Set wbOpen1 = xlApp.Workbooks.Open(FileName:=(saveFolder & "" & sTemp & "_" & Format(Now(), "yyyymmdd") & ".csv"))
End If
End If
Next
Set wbOpen2 = xlApp.Workbooks.Open(FileName:="C:UsersmystuffDownloadssavedestmyfile.xlsm")
Set wbOpen1 = Nothing
Set wbOpen2 = Nothing
Set xlApp = Nothing
End Sub
Function IsFile(ByVal fName As String) As Boolean
On Error Resume Next
IsFile = (GetAttr(fName))
End Function
excel vba excel-vba outlook outlook-vba
excel vba excel-vba outlook outlook-vba
asked Nov 9 at 8:15
doubleknavery
2618
2618
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
You have usedOn Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.
– Tony Dallimore
Nov 11 at 0:56
What isIsFile
supposed to do? It appears to me that no file named fName will exist soGetAttr
will give an error which you have ignored. I have not tried your code but my guess isIsFile
always returnsFalse
.
– Tony Dallimore
Nov 11 at 0:59
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11
|
show 3 more comments
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
You have usedOn Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.
– Tony Dallimore
Nov 11 at 0:56
What isIsFile
supposed to do? It appears to me that no file named fName will exist soGetAttr
will give an error which you have ignored. I have not tried your code but my guess isIsFile
always returnsFalse
.
– Tony Dallimore
Nov 11 at 0:59
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
You have used
On Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.– Tony Dallimore
Nov 11 at 0:56
You have used
On Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.– Tony Dallimore
Nov 11 at 0:56
What is
IsFile
supposed to do? It appears to me that no file named fName will exist so GetAttr
will give an error which you have ignored. I have not tried your code but my guess is IsFile
always returns False
.– Tony Dallimore
Nov 11 at 0:59
What is
IsFile
supposed to do? It appears to me that no file named fName will exist so GetAttr
will give an error which you have ignored. I have not tried your code but my guess is IsFile
always returns False
.– Tony Dallimore
Nov 11 at 0:59
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222012%2fopening-an-excel-workbook-using-outlook-rule-and-outlook-vba-outlook-script-ru%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Set a breakpoint see if this code runs twice. Change the name see if other code runs. Turn off the rule see if other code runs.
– niton
Nov 9 at 11:01
Why open an Excel workbook to run a macro which you could have run in Outlook?
– Tony Dallimore
Nov 10 at 1:41
You have used
On Error Resume Next
in a way that means "Don't tell me about any errors in my code because I like strange failures" Please delete this statement which is not helping.– Tony Dallimore
Nov 11 at 0:56
What is
IsFile
supposed to do? It appears to me that no file named fName will exist soGetAttr
will give an error which you have ignored. I have not tried your code but my guess isIsFile
always returnsFalse
.– Tony Dallimore
Nov 11 at 0:59
@niton I have done this, and yes it does.
– doubleknavery
Nov 12 at 1:11