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