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









share|improve this question






















  • 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 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















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









share|improve this question






















  • 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 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













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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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










  • @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










  • 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 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff