PDA

View Full Version : Debug Code VBA



Romulo Avila
07-28-2016, 07:34 AM
Good Morning,

I have the code below in a rule Outlook to run when new mail arrives, but I'm 02 doubts.

- How do I debug this code when Teclo F8 does not run, if you dial F5 opens the MACRO box but does not appear


- Its run to read all the emails that I have in my inbox


Thank you


===>> Code <<===

Public Sub SalvarXML(Email As MailItem)
Dim DiretorioAnexos As String
DiretorioAnexos = "C:\XML\"
Dim MailID As String
Dim Mail As Outlook.MailItem
Dim fso

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

For Each Anexo In Mail.Attachments
On Error Resume Next
If Right(Anexo.FileName, 4) = ".xml" Or Right(Anexo.FileName, 4) = ".XML" Then
Anexo.SaveAsFile DiretorioAnexos & Anexo.FileName
Set objParser = CreateObject("Microsoft.XMLDOM")
objParser.Load (DiretorioAnexos + Anexo.FileName)

oldfilename = DiretorioAnexos + Anexo.FileName

Set ElemList = objParser.getElementsByTagName("chNFe")
chNFe = ElemList.Item(0).Text

NewFileName = DiretorioAnexos + chNFe + ".xml"

Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile oldfilename, NewFileName

End If
Next

Set Mail = Nothing
Email.UnRead = False
Email.Delete
End Sub

gmayor
07-28-2016, 09:36 PM
Why are you obsessed with running the code from a rule? I have already given you code that demonstrates how to process an individual message and how to process a folder. Run the macro from a ribbon or QAT (Quick Access Toolbar) button, and leave the rules to process individual messages as they arrive.

Romulo Avila
07-29-2016, 02:36 AM
Graham,
Good morning, I'm wanting is to automate this task to save the attachment so that the message comes, how will I receive many emails like this, take some time to access them and run the macro for each case.

And in the case of debug, it is at I try to follow the step-by-step macro, so I can try to understand.

Thanks again for your help

gmayor
07-29-2016, 03:52 AM
I gave you the code for that also - which bears little relationship to what you have posted. Your macro script is

Public Sub SalvarXML(Email As MailItem)

As I have explained repeatedly, the incoming message is defined in your script as EMail thus
'MailID = Email.EntryID 'Not required
'Set Mail = Application.Session.GetItemFromID(MailID) 'Not required
For Each Anexo In Email.Attachments 'Change

I have not tested the rest of your code which bears no resemblance to how I told you to process it. I feel I am wasting my time.

Romulo Avila
07-29-2016, 04:41 AM
Graham, good day, do not want to take your time, thank you and excuse my insistence, I lay in VBA, I'm starting now in this area.


I will trying here, thanks a lot .. hugs ..