Consulting

Results 1 to 5 of 5

Thread: Macro no longer working

  1. #1

    Macro no longer working

    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!

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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.

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •