Reply (by .Send) e-mail Outlook “Run Script” rule not triggering VBA script for incoming messagesVBA Outlook is triggering the same rule twice, with the same subjectChange reply address in Outlook mailOutlook VBA Script on incoming messagesVBA Outlook rule to Run Script is not completingOutlook VBA script will not runIntegrate Outlook “Run as Script” rule into Excel VBA code that sends emailOutlook “Run Script” rule not triggering VBA script for incoming messagesVBA/.NET: Creating an Outlook rule for Reply-toExecuting VBA Script without access to the “Run a Script” rule in Outlook 2016What VBA method could Outlook trigger as mail is composed or replied?

Why do planes need a roll motion?

How did the Axis intend to hold the Caucasus?

How to check what is edible on an alien world?

Why is 'n' preferred over "n" for output streams?

Did the IBM PC use the 8088's NMI line?

How can I rectify up to 85 kV

How to store my pliers and wire cutters on my desk?

Why is it considered Acid Rain with pH <5.6

Sci-fi change: Too much or Not enough

Trapped in an ocean Temple in Minecraft?

How to tar a list of directories only if they exist

How many oliphaunts died in all of the Lord of the Rings battles?

The Sword in the Stone

Is this photo showing a woman standing in the nude before teenagers real?

How do I stop my characters falling in love?

Is it legal to use cash pulled from a credit card to pay the monthly payment on that credit card?

How did the SysRq key get onto modern keyboards if it's rarely used?

If my pay period is split between 2 calendar years, which tax year do I file them in?

Old French song lyrics with the word "baiser."

How to judge a Ph.D. applicant that arrives "out of thin air"

Is there a wealth gap in Boston where the median net worth of white households is $247,500 while the median net worth for black families was $8?

Why isn't there any 9.5 digit multimeter or higher?

Defining a Function programmatically

Decreasing star count



Reply (by .Send) e-mail Outlook “Run Script” rule not triggering VBA script for incoming messages


VBA Outlook is triggering the same rule twice, with the same subjectChange reply address in Outlook mailOutlook VBA Script on incoming messagesVBA Outlook rule to Run Script is not completingOutlook VBA script will not runIntegrate Outlook “Run as Script” rule into Excel VBA code that sends emailOutlook “Run Script” rule not triggering VBA script for incoming messagesVBA/.NET: Creating an Outlook rule for Reply-toExecuting VBA Script without access to the “Run a Script” rule in Outlook 2016What VBA method could Outlook trigger as mail is composed or replied?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















(Since I'm from Brazil, there is some text in Portuguese, so if you need some help, just let me know).



I've got 2 macros in my Outlook "This Outlook Session" in 1 master macro that calls the others 2 that I mentioned before.



  • The master macro do:

    Macro name: "Salvar_CNAB_Registro"

Discovers the subject of the e-mail and give the path I want depending what it's writing.
After discover the path, save all the attachments from e-mail on the path discovered.



Sub Salvar_CNAB_Registro(Email As MailItem) 
'Dim strSubject As String
Dim objMsg As Outlook.MailItem
Dim objSubject As String

objSubject = Email.Subject

'Defino qual caminho salvará os registros dos arquivos CNAB dependendo do produto da Funcesp ou da forma de liquidação
If InStr(1, objSubject, "Registro de Boletos de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
'DiretorioAnexos = "K:Divisao_Administrativa_FinanceiraTesourariaContas_ReceberCOBRANÇASSAÚDE20193 MARÇO 201925.03.2019TESTE"
ElseIf InStr(1, objSubject, "Registro de Boletos de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
ElseIf InStr(1, objSubject, "Registro de Boletos de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Boletos de Empréstimo") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
End If

Debug.Print "Diretório Macro Salvar_CNAB_Registro:"
Debug.Print DiretorioAnexos

Dim MailID As String
Dim Mail As Outlook.MailItem

MailID = Email.EntryID
Set Mail = Application.Session.GetItemFromID(MailID)

'Verifico se o anexo no e-mail é arquivo unixo TXT e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "txt" Then
Anexo.SaveAsFile DiretorioAnexos & "" & Anexo.FileName
End If
Next

'Verifico se o anexo no e-mail é arquivo unixo zip e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "zip" Then
Anexo.SaveAsFile DiretorioAnexos & Anexo.FileName
Call Unzipar_Arquivos
End If

Next

DoEvents
Call Reply_Email

Set Mail = Nothing
End Sub


  • The first macro do:

    Macro name: Unzipar_Arquivos (calls the macro UnzipAFile)

It has two macros, it unzip any zip file attached in any e-mail called by the rule on Outlook.



Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.NameSpace(unzipToPath).CopyHere ShellApp.NameSpace(zippedFileFullName).Items

End Sub


Sub Unzipar_Arquivos()

Dim diretorio As Variant
Dim diretorio_ext As Variant
Dim nome_arquivo As String


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1658 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201658"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201658" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1717 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201717"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201717" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1775 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201775"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201775" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop

End Sub



  • The second macro do:

    Macro name: Reply_Email

Discover the name of each file that was saved before and then add the name on the body of the HTML e-mail that it's going to reply to all.



Sub Reply_Email()

Dim strFolder As String
Const strPattern As String = "*.txt"
Dim strFile As String
Dim nome_cnab As String
Dim quantidade As Integer
Dim add_msg As String
Dim validador As Integer
Dim i As Integer

Debug.Print "Diretório Macro Responder_Email:"
Debug.Print strFolder
'Define o nome do caminho de acordo com o assunto (produto da funcesp que o cnab está sendo registrado) do e-mail enviado pelo funcionário solicitando o registro
strFolder = DiretorioAnexos
'Define a quantidade inicial de arquivos dentro da pasta que foi registrada
quantidade = 0
'Define o validador inicial igual a 0, isso significa que ainda não começou a montar o e-mail de resposta para a pessoa
validador = 0
'Nome do passo quando ele montar o e-mail, e adicionará os nomes dos arquivos cnab através do loop
Add_Nome_Cnab:
strFile = Dir(strFolder & strPattern, vbNormal)
Do While Len(strFile) > 0
'Caso queira ver o nome do arquivo CNAB na janela de verificação imediata (CTRL + G)
'Debug.Print strFile
strFile = Dir
nome_cnab = strFile
'Adiciono 1 na quantidade toda vez que passar por aqui, assim teremos a quantidade de arquivos salvos de cada e-mail
quantidade = quantidade + 1
'Se o validador for 1, ele grava o nome do arquivo na variavel
If validador = 1 Then
add_msg = nome_cnab
'Vai para o passo de adicionar de fato o nome do arquivo no corpo do e-mail através da variavel criada acima
GoTo Check_Validador
End If
Loop

Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply

For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
'Define o validador como 1, para começar a montar o e-mail
validador = 1
'Se tiver 1 arquivo ou mais, ele começa a montar o e-mail
If quantidade > 0 Then
For i = 1 To quantidade
'Vai para o passo de gravar o nome do arquivo na variavel
GoTo Add_Nome_Cnab
Check_Validador:
'Essa etapa que ele adiciona de fato o nome no corpo do e-mail através da variavel criada acima
olReply.HTMLBody = "<br>" & add_msg & vbCrLf & olReply.HTMLBody
DoEvents
Next i
Else
olReply.HTMLBody = "<br>" & "Nenhum arquivo CNAB registrado" & "<br>" & vbCrLf & olReply.HTMLBody
End If
'Escreve as duas primeiras linhas no corpo do e-mail: "Arquivos registrados no dia e hora: " + Data e Hora + "Segue arquivos registrados: "
olReply.HTMLBody = "<br>" & "Arquivos registrados no dia e hora: " & Now & "<br>" & "Segue arquivos registrados: " & "<br>" & vbCrLf & olReply.HTMLBody
DoEvents
'Mostra o e-mail na tela
olReply.Display
DoEvents
'Envia o e-mail
olReply.Send
DoEvents
Next olItem
End Sub


All the macros works as a charm individually, but my problem is when the master macro "Salvar_CNAB_Registro" calls the last macro (Reply_Email) and the e-mail doesn't send by itself automatically.



So, if I run the script alone, it works!!! But, it doesn't work called by another macro.



EDIT 1:



I did some tests, but still can't work unless I debug.



What I did:



Added the macro to test all the macros together, each one calling each other.



Sub Test()
Dim x, mailItem As Outlook.mailItem
For Each x In Application.ActiveExplorer.Selection
If TypeName(x) = "MailItem" Then
Set mailItem = x
Call Salvar_CNAB_Registro(mailItem)
End If
Next
End Sub



So, still works sending the e-mail by debugging but it doesn't work by calling from the rule. I mean, all the macro works, but only don't display and send the e-mail.



I tried the solution from @0m3r, removing the line Application.ActiveExplorer.Selection from the macro Reply_Email, using Sub Reply_Email(ByVal Email As Object) and then calling it like Reply_Email(Email), but this method don't work.



I tried even using Sub Reply_Email(Email As Outlook.mailItem) and then calling it like Reply_Email(Email), this method worked by debugging again, but not automatically.



I also tried this method (How to Auto Reply with Outlook rule), by replying the e-mail from the rule directly but the original message in the body was not there, also I can't sign this code in my work.










share|improve this question



















  • 1





    its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

    – 0m3r
    Mar 26 at 21:18












  • @0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

    – Guilherme Matheus
    Mar 27 at 11:34












  • @0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

    – Guilherme Matheus
    Mar 28 at 11:20











  • @0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

    – Guilherme Matheus
    Apr 1 at 17:16











  • @0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

    – Guilherme Matheus
    Apr 4 at 14:40

















1















(Since I'm from Brazil, there is some text in Portuguese, so if you need some help, just let me know).



I've got 2 macros in my Outlook "This Outlook Session" in 1 master macro that calls the others 2 that I mentioned before.



  • The master macro do:

    Macro name: "Salvar_CNAB_Registro"

Discovers the subject of the e-mail and give the path I want depending what it's writing.
After discover the path, save all the attachments from e-mail on the path discovered.



Sub Salvar_CNAB_Registro(Email As MailItem) 
'Dim strSubject As String
Dim objMsg As Outlook.MailItem
Dim objSubject As String

objSubject = Email.Subject

'Defino qual caminho salvará os registros dos arquivos CNAB dependendo do produto da Funcesp ou da forma de liquidação
If InStr(1, objSubject, "Registro de Boletos de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
'DiretorioAnexos = "K:Divisao_Administrativa_FinanceiraTesourariaContas_ReceberCOBRANÇASSAÚDE20193 MARÇO 201925.03.2019TESTE"
ElseIf InStr(1, objSubject, "Registro de Boletos de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
ElseIf InStr(1, objSubject, "Registro de Boletos de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Boletos de Empréstimo") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
End If

Debug.Print "Diretório Macro Salvar_CNAB_Registro:"
Debug.Print DiretorioAnexos

Dim MailID As String
Dim Mail As Outlook.MailItem

MailID = Email.EntryID
Set Mail = Application.Session.GetItemFromID(MailID)

'Verifico se o anexo no e-mail é arquivo unixo TXT e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "txt" Then
Anexo.SaveAsFile DiretorioAnexos & "" & Anexo.FileName
End If
Next

'Verifico se o anexo no e-mail é arquivo unixo zip e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "zip" Then
Anexo.SaveAsFile DiretorioAnexos & Anexo.FileName
Call Unzipar_Arquivos
End If

Next

DoEvents
Call Reply_Email

Set Mail = Nothing
End Sub


  • The first macro do:

    Macro name: Unzipar_Arquivos (calls the macro UnzipAFile)

It has two macros, it unzip any zip file attached in any e-mail called by the rule on Outlook.



Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.NameSpace(unzipToPath).CopyHere ShellApp.NameSpace(zippedFileFullName).Items

End Sub


Sub Unzipar_Arquivos()

Dim diretorio As Variant
Dim diretorio_ext As Variant
Dim nome_arquivo As String


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1658 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201658"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201658" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1717 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201717"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201717" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1775 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201775"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201775" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop

End Sub



  • The second macro do:

    Macro name: Reply_Email

Discover the name of each file that was saved before and then add the name on the body of the HTML e-mail that it's going to reply to all.



Sub Reply_Email()

Dim strFolder As String
Const strPattern As String = "*.txt"
Dim strFile As String
Dim nome_cnab As String
Dim quantidade As Integer
Dim add_msg As String
Dim validador As Integer
Dim i As Integer

Debug.Print "Diretório Macro Responder_Email:"
Debug.Print strFolder
'Define o nome do caminho de acordo com o assunto (produto da funcesp que o cnab está sendo registrado) do e-mail enviado pelo funcionário solicitando o registro
strFolder = DiretorioAnexos
'Define a quantidade inicial de arquivos dentro da pasta que foi registrada
quantidade = 0
'Define o validador inicial igual a 0, isso significa que ainda não começou a montar o e-mail de resposta para a pessoa
validador = 0
'Nome do passo quando ele montar o e-mail, e adicionará os nomes dos arquivos cnab através do loop
Add_Nome_Cnab:
strFile = Dir(strFolder & strPattern, vbNormal)
Do While Len(strFile) > 0
'Caso queira ver o nome do arquivo CNAB na janela de verificação imediata (CTRL + G)
'Debug.Print strFile
strFile = Dir
nome_cnab = strFile
'Adiciono 1 na quantidade toda vez que passar por aqui, assim teremos a quantidade de arquivos salvos de cada e-mail
quantidade = quantidade + 1
'Se o validador for 1, ele grava o nome do arquivo na variavel
If validador = 1 Then
add_msg = nome_cnab
'Vai para o passo de adicionar de fato o nome do arquivo no corpo do e-mail através da variavel criada acima
GoTo Check_Validador
End If
Loop

Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply

For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
'Define o validador como 1, para começar a montar o e-mail
validador = 1
'Se tiver 1 arquivo ou mais, ele começa a montar o e-mail
If quantidade > 0 Then
For i = 1 To quantidade
'Vai para o passo de gravar o nome do arquivo na variavel
GoTo Add_Nome_Cnab
Check_Validador:
'Essa etapa que ele adiciona de fato o nome no corpo do e-mail através da variavel criada acima
olReply.HTMLBody = "<br>" & add_msg & vbCrLf & olReply.HTMLBody
DoEvents
Next i
Else
olReply.HTMLBody = "<br>" & "Nenhum arquivo CNAB registrado" & "<br>" & vbCrLf & olReply.HTMLBody
End If
'Escreve as duas primeiras linhas no corpo do e-mail: "Arquivos registrados no dia e hora: " + Data e Hora + "Segue arquivos registrados: "
olReply.HTMLBody = "<br>" & "Arquivos registrados no dia e hora: " & Now & "<br>" & "Segue arquivos registrados: " & "<br>" & vbCrLf & olReply.HTMLBody
DoEvents
'Mostra o e-mail na tela
olReply.Display
DoEvents
'Envia o e-mail
olReply.Send
DoEvents
Next olItem
End Sub


All the macros works as a charm individually, but my problem is when the master macro "Salvar_CNAB_Registro" calls the last macro (Reply_Email) and the e-mail doesn't send by itself automatically.



So, if I run the script alone, it works!!! But, it doesn't work called by another macro.



EDIT 1:



I did some tests, but still can't work unless I debug.



What I did:



Added the macro to test all the macros together, each one calling each other.



Sub Test()
Dim x, mailItem As Outlook.mailItem
For Each x In Application.ActiveExplorer.Selection
If TypeName(x) = "MailItem" Then
Set mailItem = x
Call Salvar_CNAB_Registro(mailItem)
End If
Next
End Sub



So, still works sending the e-mail by debugging but it doesn't work by calling from the rule. I mean, all the macro works, but only don't display and send the e-mail.



I tried the solution from @0m3r, removing the line Application.ActiveExplorer.Selection from the macro Reply_Email, using Sub Reply_Email(ByVal Email As Object) and then calling it like Reply_Email(Email), but this method don't work.



I tried even using Sub Reply_Email(Email As Outlook.mailItem) and then calling it like Reply_Email(Email), this method worked by debugging again, but not automatically.



I also tried this method (How to Auto Reply with Outlook rule), by replying the e-mail from the rule directly but the original message in the body was not there, also I can't sign this code in my work.










share|improve this question



















  • 1





    its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

    – 0m3r
    Mar 26 at 21:18












  • @0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

    – Guilherme Matheus
    Mar 27 at 11:34












  • @0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

    – Guilherme Matheus
    Mar 28 at 11:20











  • @0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

    – Guilherme Matheus
    Apr 1 at 17:16











  • @0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

    – Guilherme Matheus
    Apr 4 at 14:40













1












1








1








(Since I'm from Brazil, there is some text in Portuguese, so if you need some help, just let me know).



I've got 2 macros in my Outlook "This Outlook Session" in 1 master macro that calls the others 2 that I mentioned before.



  • The master macro do:

    Macro name: "Salvar_CNAB_Registro"

Discovers the subject of the e-mail and give the path I want depending what it's writing.
After discover the path, save all the attachments from e-mail on the path discovered.



Sub Salvar_CNAB_Registro(Email As MailItem) 
'Dim strSubject As String
Dim objMsg As Outlook.MailItem
Dim objSubject As String

objSubject = Email.Subject

'Defino qual caminho salvará os registros dos arquivos CNAB dependendo do produto da Funcesp ou da forma de liquidação
If InStr(1, objSubject, "Registro de Boletos de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
'DiretorioAnexos = "K:Divisao_Administrativa_FinanceiraTesourariaContas_ReceberCOBRANÇASSAÚDE20193 MARÇO 201925.03.2019TESTE"
ElseIf InStr(1, objSubject, "Registro de Boletos de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
ElseIf InStr(1, objSubject, "Registro de Boletos de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Boletos de Empréstimo") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
End If

Debug.Print "Diretório Macro Salvar_CNAB_Registro:"
Debug.Print DiretorioAnexos

Dim MailID As String
Dim Mail As Outlook.MailItem

MailID = Email.EntryID
Set Mail = Application.Session.GetItemFromID(MailID)

'Verifico se o anexo no e-mail é arquivo unixo TXT e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "txt" Then
Anexo.SaveAsFile DiretorioAnexos & "" & Anexo.FileName
End If
Next

'Verifico se o anexo no e-mail é arquivo unixo zip e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "zip" Then
Anexo.SaveAsFile DiretorioAnexos & Anexo.FileName
Call Unzipar_Arquivos
End If

Next

DoEvents
Call Reply_Email

Set Mail = Nothing
End Sub


  • The first macro do:

    Macro name: Unzipar_Arquivos (calls the macro UnzipAFile)

It has two macros, it unzip any zip file attached in any e-mail called by the rule on Outlook.



Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.NameSpace(unzipToPath).CopyHere ShellApp.NameSpace(zippedFileFullName).Items

End Sub


Sub Unzipar_Arquivos()

Dim diretorio As Variant
Dim diretorio_ext As Variant
Dim nome_arquivo As String


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1658 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201658"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201658" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1717 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201717"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201717" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1775 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201775"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201775" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop

End Sub



  • The second macro do:

    Macro name: Reply_Email

Discover the name of each file that was saved before and then add the name on the body of the HTML e-mail that it's going to reply to all.



Sub Reply_Email()

Dim strFolder As String
Const strPattern As String = "*.txt"
Dim strFile As String
Dim nome_cnab As String
Dim quantidade As Integer
Dim add_msg As String
Dim validador As Integer
Dim i As Integer

Debug.Print "Diretório Macro Responder_Email:"
Debug.Print strFolder
'Define o nome do caminho de acordo com o assunto (produto da funcesp que o cnab está sendo registrado) do e-mail enviado pelo funcionário solicitando o registro
strFolder = DiretorioAnexos
'Define a quantidade inicial de arquivos dentro da pasta que foi registrada
quantidade = 0
'Define o validador inicial igual a 0, isso significa que ainda não começou a montar o e-mail de resposta para a pessoa
validador = 0
'Nome do passo quando ele montar o e-mail, e adicionará os nomes dos arquivos cnab através do loop
Add_Nome_Cnab:
strFile = Dir(strFolder & strPattern, vbNormal)
Do While Len(strFile) > 0
'Caso queira ver o nome do arquivo CNAB na janela de verificação imediata (CTRL + G)
'Debug.Print strFile
strFile = Dir
nome_cnab = strFile
'Adiciono 1 na quantidade toda vez que passar por aqui, assim teremos a quantidade de arquivos salvos de cada e-mail
quantidade = quantidade + 1
'Se o validador for 1, ele grava o nome do arquivo na variavel
If validador = 1 Then
add_msg = nome_cnab
'Vai para o passo de adicionar de fato o nome do arquivo no corpo do e-mail através da variavel criada acima
GoTo Check_Validador
End If
Loop

Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply

For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
'Define o validador como 1, para começar a montar o e-mail
validador = 1
'Se tiver 1 arquivo ou mais, ele começa a montar o e-mail
If quantidade > 0 Then
For i = 1 To quantidade
'Vai para o passo de gravar o nome do arquivo na variavel
GoTo Add_Nome_Cnab
Check_Validador:
'Essa etapa que ele adiciona de fato o nome no corpo do e-mail através da variavel criada acima
olReply.HTMLBody = "<br>" & add_msg & vbCrLf & olReply.HTMLBody
DoEvents
Next i
Else
olReply.HTMLBody = "<br>" & "Nenhum arquivo CNAB registrado" & "<br>" & vbCrLf & olReply.HTMLBody
End If
'Escreve as duas primeiras linhas no corpo do e-mail: "Arquivos registrados no dia e hora: " + Data e Hora + "Segue arquivos registrados: "
olReply.HTMLBody = "<br>" & "Arquivos registrados no dia e hora: " & Now & "<br>" & "Segue arquivos registrados: " & "<br>" & vbCrLf & olReply.HTMLBody
DoEvents
'Mostra o e-mail na tela
olReply.Display
DoEvents
'Envia o e-mail
olReply.Send
DoEvents
Next olItem
End Sub


All the macros works as a charm individually, but my problem is when the master macro "Salvar_CNAB_Registro" calls the last macro (Reply_Email) and the e-mail doesn't send by itself automatically.



So, if I run the script alone, it works!!! But, it doesn't work called by another macro.



EDIT 1:



I did some tests, but still can't work unless I debug.



What I did:



Added the macro to test all the macros together, each one calling each other.



Sub Test()
Dim x, mailItem As Outlook.mailItem
For Each x In Application.ActiveExplorer.Selection
If TypeName(x) = "MailItem" Then
Set mailItem = x
Call Salvar_CNAB_Registro(mailItem)
End If
Next
End Sub



So, still works sending the e-mail by debugging but it doesn't work by calling from the rule. I mean, all the macro works, but only don't display and send the e-mail.



I tried the solution from @0m3r, removing the line Application.ActiveExplorer.Selection from the macro Reply_Email, using Sub Reply_Email(ByVal Email As Object) and then calling it like Reply_Email(Email), but this method don't work.



I tried even using Sub Reply_Email(Email As Outlook.mailItem) and then calling it like Reply_Email(Email), this method worked by debugging again, but not automatically.



I also tried this method (How to Auto Reply with Outlook rule), by replying the e-mail from the rule directly but the original message in the body was not there, also I can't sign this code in my work.










share|improve this question
















(Since I'm from Brazil, there is some text in Portuguese, so if you need some help, just let me know).



I've got 2 macros in my Outlook "This Outlook Session" in 1 master macro that calls the others 2 that I mentioned before.



  • The master macro do:

    Macro name: "Salvar_CNAB_Registro"

Discovers the subject of the e-mail and give the path I want depending what it's writing.
After discover the path, save all the attachments from e-mail on the path discovered.



Sub Salvar_CNAB_Registro(Email As MailItem) 
'Dim strSubject As String
Dim objMsg As Outlook.MailItem
Dim objSubject As String

objSubject = Email.Subject

'Defino qual caminho salvará os registros dos arquivos CNAB dependendo do produto da Funcesp ou da forma de liquidação
If InStr(1, objSubject, "Registro de Boletos de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
'DiretorioAnexos = "K:Divisao_Administrativa_FinanceiraTesourariaContas_ReceberCOBRANÇASSAÚDE20193 MARÇO 201925.03.2019TESTE"
ElseIf InStr(1, objSubject, "Registro de Boletos de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201658"
ElseIf InStr(1, objSubject, "Registro de Boletos de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Saúde - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Autopatrocínio - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Débito Automático de Seguros - Vencimento") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201775"
ElseIf InStr(1, objSubject, "Registro de Boletos de Empréstimo") > 0 Then
DiretorioAnexos = "S:AFTDataOUTBOXGE201717"
End If

Debug.Print "Diretório Macro Salvar_CNAB_Registro:"
Debug.Print DiretorioAnexos

Dim MailID As String
Dim Mail As Outlook.MailItem

MailID = Email.EntryID
Set Mail = Application.Session.GetItemFromID(MailID)

'Verifico se o anexo no e-mail é arquivo unixo TXT e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "txt" Then
Anexo.SaveAsFile DiretorioAnexos & "" & Anexo.FileName
End If
Next

'Verifico se o anexo no e-mail é arquivo unixo zip e salvo todos
For Each Anexo In Mail.Attachments
If Right(Anexo.FileName, 3) = "zip" Then
Anexo.SaveAsFile DiretorioAnexos & Anexo.FileName
Call Unzipar_Arquivos
End If

Next

DoEvents
Call Reply_Email

Set Mail = Nothing
End Sub


  • The first macro do:

    Macro name: Unzipar_Arquivos (calls the macro UnzipAFile)

It has two macros, it unzip any zip file attached in any e-mail called by the rule on Outlook.



Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.NameSpace(unzipToPath).CopyHere ShellApp.NameSpace(zippedFileFullName).Items

End Sub


Sub Unzipar_Arquivos()

Dim diretorio As Variant
Dim diretorio_ext As Variant
Dim nome_arquivo As String


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1658 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201658"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201658" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1717 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201717"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201717" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop


'------------------------------------ Extraio os arquivos da pasta do Santander Outbox 1775 --------------------------------'


'Caminho que o arquivo será unzipado
diretorio_ext = "S:AFTDataOUTBOXGE201775"

'Descobre o nome do arquivo zip + caminho que ele se encontra
nome_arquivo = Dir(diretorio_ext & "*.zip")

'Caminho que encontra-se o arquivo zipado
diretorio = "S:AFTDataOUTBOXGE201775" & nome_arquivo

'Executo a macro enquanto houver arquivos zipados na pasta
Do While Len(nome_arquivo) > 0

'A primeira parte refere-se ao nome do arquivo que será unzipado, e a segunda parte refere-se ao caminho que ele será unzipado
Call UnzipAFile(diretorio, diretorio_ext)

'Apago o primeiro arquivo zip que foi extraído
'Primeiro remove o atributo de arquivo "somente leitura" caso esteja definido
On Error Resume Next
SetAttr FileToDelete, vbNormal
'Depois apago o arquivo
Kill diretorio

'Procura o próximo arquivo
nome_arquivo = Dir

'Exibe mensagem de sucesso
MsgBox "Arquivo " & nome_arquivo & "descompactado e arquivos registrados! " & "no diretório: " & diretorio_ext

Loop

End Sub



  • The second macro do:

    Macro name: Reply_Email

Discover the name of each file that was saved before and then add the name on the body of the HTML e-mail that it's going to reply to all.



Sub Reply_Email()

Dim strFolder As String
Const strPattern As String = "*.txt"
Dim strFile As String
Dim nome_cnab As String
Dim quantidade As Integer
Dim add_msg As String
Dim validador As Integer
Dim i As Integer

Debug.Print "Diretório Macro Responder_Email:"
Debug.Print strFolder
'Define o nome do caminho de acordo com o assunto (produto da funcesp que o cnab está sendo registrado) do e-mail enviado pelo funcionário solicitando o registro
strFolder = DiretorioAnexos
'Define a quantidade inicial de arquivos dentro da pasta que foi registrada
quantidade = 0
'Define o validador inicial igual a 0, isso significa que ainda não começou a montar o e-mail de resposta para a pessoa
validador = 0
'Nome do passo quando ele montar o e-mail, e adicionará os nomes dos arquivos cnab através do loop
Add_Nome_Cnab:
strFile = Dir(strFolder & strPattern, vbNormal)
Do While Len(strFile) > 0
'Caso queira ver o nome do arquivo CNAB na janela de verificação imediata (CTRL + G)
'Debug.Print strFile
strFile = Dir
nome_cnab = strFile
'Adiciono 1 na quantidade toda vez que passar por aqui, assim teremos a quantidade de arquivos salvos de cada e-mail
quantidade = quantidade + 1
'Se o validador for 1, ele grava o nome do arquivo na variavel
If validador = 1 Then
add_msg = nome_cnab
'Vai para o passo de adicionar de fato o nome do arquivo no corpo do e-mail através da variavel criada acima
GoTo Check_Validador
End If
Loop

Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply

For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
'Define o validador como 1, para começar a montar o e-mail
validador = 1
'Se tiver 1 arquivo ou mais, ele começa a montar o e-mail
If quantidade > 0 Then
For i = 1 To quantidade
'Vai para o passo de gravar o nome do arquivo na variavel
GoTo Add_Nome_Cnab
Check_Validador:
'Essa etapa que ele adiciona de fato o nome no corpo do e-mail através da variavel criada acima
olReply.HTMLBody = "<br>" & add_msg & vbCrLf & olReply.HTMLBody
DoEvents
Next i
Else
olReply.HTMLBody = "<br>" & "Nenhum arquivo CNAB registrado" & "<br>" & vbCrLf & olReply.HTMLBody
End If
'Escreve as duas primeiras linhas no corpo do e-mail: "Arquivos registrados no dia e hora: " + Data e Hora + "Segue arquivos registrados: "
olReply.HTMLBody = "<br>" & "Arquivos registrados no dia e hora: " & Now & "<br>" & "Segue arquivos registrados: " & "<br>" & vbCrLf & olReply.HTMLBody
DoEvents
'Mostra o e-mail na tela
olReply.Display
DoEvents
'Envia o e-mail
olReply.Send
DoEvents
Next olItem
End Sub


All the macros works as a charm individually, but my problem is when the master macro "Salvar_CNAB_Registro" calls the last macro (Reply_Email) and the e-mail doesn't send by itself automatically.



So, if I run the script alone, it works!!! But, it doesn't work called by another macro.



EDIT 1:



I did some tests, but still can't work unless I debug.



What I did:



Added the macro to test all the macros together, each one calling each other.



Sub Test()
Dim x, mailItem As Outlook.mailItem
For Each x In Application.ActiveExplorer.Selection
If TypeName(x) = "MailItem" Then
Set mailItem = x
Call Salvar_CNAB_Registro(mailItem)
End If
Next
End Sub



So, still works sending the e-mail by debugging but it doesn't work by calling from the rule. I mean, all the macro works, but only don't display and send the e-mail.



I tried the solution from @0m3r, removing the line Application.ActiveExplorer.Selection from the macro Reply_Email, using Sub Reply_Email(ByVal Email As Object) and then calling it like Reply_Email(Email), but this method don't work.



I tried even using Sub Reply_Email(Email As Outlook.mailItem) and then calling it like Reply_Email(Email), this method worked by debugging again, but not automatically.



I also tried this method (How to Auto Reply with Outlook rule), by replying the e-mail from the rule directly but the original message in the body was not there, also I can't sign this code in my work.







vba outlook outlook-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 5 at 17:56









TylerH

16.4k10 gold badges56 silver badges71 bronze badges




16.4k10 gold badges56 silver badges71 bronze badges










asked Mar 26 at 18:45









Guilherme MatheusGuilherme Matheus

326 bronze badges




326 bronze badges







  • 1





    its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

    – 0m3r
    Mar 26 at 21:18












  • @0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

    – Guilherme Matheus
    Mar 27 at 11:34












  • @0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

    – Guilherme Matheus
    Mar 28 at 11:20











  • @0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

    – Guilherme Matheus
    Apr 1 at 17:16











  • @0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

    – Guilherme Matheus
    Apr 4 at 14:40












  • 1





    its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

    – 0m3r
    Mar 26 at 21:18












  • @0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

    – Guilherme Matheus
    Mar 27 at 11:34












  • @0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

    – Guilherme Matheus
    Mar 28 at 11:20











  • @0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

    – Guilherme Matheus
    Apr 1 at 17:16











  • @0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

    – Guilherme Matheus
    Apr 4 at 14:40







1




1





its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

– 0m3r
Mar 26 at 21:18






its not working because its expecting Application.ActiveExplorer.Selection you need to remove that and use ByVal Exmple Sub Reply_Email(ByVal Email As Object) Then call it from your master macro - Example Responder_Email Email

– 0m3r
Mar 26 at 21:18














@0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

– Guilherme Matheus
Mar 27 at 11:34






@0m3r it didn't work, I removed Application.ActiveExplorer.Selection and I used Sub Reply_Email(ByVal Email As Object) as you told. But I didn't understand by calling it in my master macro, should I just call it by it name (as usual)? Or something else? Because you wrote: Responder_Email Email (macro name is Reply_Email as above in my explanation, I just renamed to post in here) so I didn't understand that Email part.

– Guilherme Matheus
Mar 27 at 11:34














@0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

– Guilherme Matheus
Mar 28 at 11:20





@0m3r also it's strange that, all macros runs as perfect separetely, even display the email and send after. But calling the macro to reply email, it doesn't. Don't even display the e-mail that is going to be send.

– Guilherme Matheus
Mar 28 at 11:20













@0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

– Guilherme Matheus
Apr 1 at 17:16





@0m3r it worked! I edit (edit 2), my original question. But the problem now, is that it is sending several e-mails instead of one. Do you know why?

– Guilherme Matheus
Apr 1 at 17:16













@0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

– Guilherme Matheus
Apr 4 at 14:40





@0m3r Can you help me with the issue please? After runing the macro from rule, instead of replying only one time. I already received 13 e-mails. But there's no Loop in my code, so I don't know why it's happening this.

– Guilherme Matheus
Apr 4 at 14:40












1 Answer
1






active

oldest

votes


















1














It worked! I followed @0m3r tips, and also I did some research on web to try to fix this issue.



What I did:



Now, my macro is Sub Reply_Email(ByVal Email As Object) I named only Dim olReply As mailItem and Set olReply = Email.ReplyAll.



And the main difference that I saw was this part:



With olReply
'Envia o e-mail
.Send
End With


So after added this, the e-mail was send. The macro is called by Call Reply_Email(Email).



And finally, I added a rule that will not reply the e-mail if there is the word "ENC:" or "RES:" in the subject, it means that if there is some reply e-mail in the inbox, it will do nothing.






share|improve this answer
























    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',
    autoActivateHeartbeat: false,
    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%2f55364251%2freply-by-send-e-mail-outlook-run-script-rule-not-triggering-vba-script-for%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It worked! I followed @0m3r tips, and also I did some research on web to try to fix this issue.



    What I did:



    Now, my macro is Sub Reply_Email(ByVal Email As Object) I named only Dim olReply As mailItem and Set olReply = Email.ReplyAll.



    And the main difference that I saw was this part:



    With olReply
    'Envia o e-mail
    .Send
    End With


    So after added this, the e-mail was send. The macro is called by Call Reply_Email(Email).



    And finally, I added a rule that will not reply the e-mail if there is the word "ENC:" or "RES:" in the subject, it means that if there is some reply e-mail in the inbox, it will do nothing.






    share|improve this answer





























      1














      It worked! I followed @0m3r tips, and also I did some research on web to try to fix this issue.



      What I did:



      Now, my macro is Sub Reply_Email(ByVal Email As Object) I named only Dim olReply As mailItem and Set olReply = Email.ReplyAll.



      And the main difference that I saw was this part:



      With olReply
      'Envia o e-mail
      .Send
      End With


      So after added this, the e-mail was send. The macro is called by Call Reply_Email(Email).



      And finally, I added a rule that will not reply the e-mail if there is the word "ENC:" or "RES:" in the subject, it means that if there is some reply e-mail in the inbox, it will do nothing.






      share|improve this answer



























        1












        1








        1







        It worked! I followed @0m3r tips, and also I did some research on web to try to fix this issue.



        What I did:



        Now, my macro is Sub Reply_Email(ByVal Email As Object) I named only Dim olReply As mailItem and Set olReply = Email.ReplyAll.



        And the main difference that I saw was this part:



        With olReply
        'Envia o e-mail
        .Send
        End With


        So after added this, the e-mail was send. The macro is called by Call Reply_Email(Email).



        And finally, I added a rule that will not reply the e-mail if there is the word "ENC:" or "RES:" in the subject, it means that if there is some reply e-mail in the inbox, it will do nothing.






        share|improve this answer















        It worked! I followed @0m3r tips, and also I did some research on web to try to fix this issue.



        What I did:



        Now, my macro is Sub Reply_Email(ByVal Email As Object) I named only Dim olReply As mailItem and Set olReply = Email.ReplyAll.



        And the main difference that I saw was this part:



        With olReply
        'Envia o e-mail
        .Send
        End With


        So after added this, the e-mail was send. The macro is called by Call Reply_Email(Email).



        And finally, I added a rule that will not reply the e-mail if there is the word "ENC:" or "RES:" in the subject, it means that if there is some reply e-mail in the inbox, it will do nothing.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 5 at 17:56









        TylerH

        16.4k10 gold badges56 silver badges71 bronze badges




        16.4k10 gold badges56 silver badges71 bronze badges










        answered Apr 5 at 14:36









        Guilherme MatheusGuilherme Matheus

        326 bronze badges




        326 bronze badges
















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364251%2freply-by-send-e-mail-outlook-run-script-rule-not-triggering-vba-script-for%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현