PDA

View Full Version : Macro no longer working



alundra828
02-03-2016, 06:03 AM
Afternoon, I have a problem. I've been trying to set up a macro to run automatically, but I have run into a few problems.

Firstly, I went about executing my macro automatically but creating a reminder routine to send me an email, and then have an Outlook rule fire off the macro from there. Problem was, the macro I'd written didn't appear in the scripts. After a bit of google-fu I found that you needed to add the below. Only problem is, it adds it to the 'run script' selector in rule wizard, but removes it from the run macro screen (raaaage)


(item As Outlook.MailItem)

I added that code into the sub so that my code looked like this.


Option Explicit






Sub SendTEC(item As Outlook.MailItem)
Const strPath As String = "PATH"
Dim strChar As String
Dim strFile As String
strFile = Dir$(strPath & "*.*") 'any file in the folder
While strFile <> ""
CreateMessage strPath & strFile, Mid(strFile, 2, 2)
DoEvents
strFile = Dir$()
Wend
Exit Sub
End Sub


Sub CreateMessage(strAtt As String, strChar As String)
Dim oAccount As Account
Dim olMail As MailItem
Dim olAttach As Attachment
Const strAcc = "ACCOUNT" 'the name of the account to use
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = strAcc Then
Set olMail = Outlook.CreateItem(olMailItem)
With olMail
.SentOnBehalfOfName = oAccount
.To = "EMAIL"
.Subject = strChar
.Body = "" ' clear the body
Set olAttach = .Attachments.Add(strAtt)
'.Display
'.Send '- Restore after testing
End With
Exit For
End If
Next
lbl_Exit:
Set olMail = Nothing
Set olAttach = Nothing
Set oAccount = Nothing
Exit Sub
End Sub

It worked, the script now appeared in the script selector in the rule wizard. But now, the macro will not run. I even stripped out the mail item bit to get the code to how it was before, but even then I run the macro and nothing happens, I select the macro, press run, and nothing happens, it flashes up as [running] for a split second but never actually carries out the task. Is there anything I can try to help debug why it isn't running any more? I'm tearing my hair out trying to figure this out. I've been using this macro fine for months, it seems a bit weird that after adding a bit of code it breaks it, and even after you remove it is still broken.



Sub SendTEC()
Const strPath As String = "PATH"
Dim strChar As String
Dim strFile As String
strFile = Dir$(strPath & "*.*") 'any file in the folder
While strFile <> ""
CreateMessage strPath & strFile, Mid(strFile, 2, 2)
DoEvents
strFile = Dir$()
Wend
Exit Sub
End Sub


Sub CreateMessage(strAtt As String, strChar As String)
Dim oAccount As Account
Dim olMail As MailItem
Dim olAttach As Attachment
Const strAcc = "ACCOUNT" 'the name of the account to use
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = strAcc Then
Set olMail = Outlook.CreateItem(olMailItem)
With olMail
.SentOnBehalfOfName = oAccount
.To = "EMAIL"
.Subject = strChar
.Body = "" ' clear the body
Set olAttach = .Attachments.Add(strAtt)
'.Display
'.Send '- Restore after testing
End With
Exit For
End If
Next
lbl_Exit:
Set olMail = Nothing
Set olAttach = Nothing
Set oAccount = Nothing
Exit Sub
End Sub

Any questions or more info you need, please don't hesitate to ask. I'll be on this all day!

gmayor
02-03-2016, 06:47 AM
You have BOTH .Display and .Send commented out, so you will never see the messages. The macro you have posted does work provided 'ACCOUNT', 'PATH' and 'EMAIL' are valid.
I am not sure of the point of the two character subject.

alundra828
02-03-2016, 07:08 AM
Hey Gmayor, Apologies, the .send is NOT commented out on the live code, but the .display is (purely because i don't need it). And the whole subject shebang is a must for the type of emails I'm sending off to identify the type of file.

I will triple check the 'ACCOUNT', 'PATH' and 'EMAIL'. I'm sure they're correct, but i'll try it.

gmayor
02-03-2016, 08:30 AM
Display is for ease of testing and eliminates the risk of sending while testing.
Did you ensure that the path is valid (and includes the final backslash -Chr(92)- character)?
If necessary use the FolderExists function from my web site to validate.
Strip out the account lines and see if it then works.

alundra828
02-04-2016, 06:00 AM
Hey Gmayor, I found the problem.

Turns out the account used (which is a company account) uses a ` rather than a '. God freaking dammit, man... Thanks for the help as always Gmayor!