Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2018, 02:20 AM
Welshgasman Welshgasman is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2007
Novice
Outlook code not running now and again?
 
Join Date: Jun 2011
Posts: 25
Welshgasman is on a distinguished road
Default Outlook code not running now and again?

Hi all,

I found some code on the net and modified it for my needs.
For the most part, it works fine. However some times the code does not run, until I close Outlook and open it up again. I shut down my computer each evening.

I am looking for any tips that would explain why it might stop working.?


The code is located in ThisOutlookSession module

TIA
Code:
Option Explicit
Public WithEvents myOlApp As Outlook.Application
Private Sub Application_Startup()
    Initialize_handler
End Sub
Public Sub Initialize_handler()
    Set myOlApp = Outlook.Application
End Sub

Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strGreeting As String
    Dim iTime As Integer
    Dim strBody As String, strDivCode As String
    strGreeting = ""
    strDivCode = "<div class=WordSection1><p class=MsoNormal>"
    iTime = Val(Format(Now(), "hh"))
  
    ' Quit if not a mail item
    If TypeName(Item) <> "MailItem" Then
        Exit Sub
    End If
    
    Select Case iTime
    Case Is < 12
        strGreeting = "morning "
    Case Is < 17
        strGreeting = "afternoon "
    Case Else
        strGreeting = "evening "
    End Select

    strGreeting = "Good " & strGreeting

    
    strBody = Item.HTMLBody
    strBody = Replace(strBody, "Good day", strGreeting)
    'strBody = Replace(strBody, strDivCode, strDivCode & strGreeting)
  
    Item.HTMLBody = strBody
End Sub
Reply With Quote
  #2  
Old 04-12-2018, 05:27 AM
gmayor's Avatar
gmayor gmayor is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If it works when you restart Outlook, it is almost certainly losing the startup event, which in any case shouldn't be necessary. Change the code to

Code:
Option Explicit
'Public WithEvents myOlApp As Outlook.Application
'Private Sub Application_Startup()
'    Initialize_handler
'End Sub
'Public Sub Initialize_handler()
'    Set myOlApp = Outlook.Application
'End Sub

'Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strGreeting As String
    'etc
and there is no event handler to lose.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-12-2018, 06:44 AM
Welshgasman Welshgasman is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2007
Novice
Outlook code not running now and again?
 
Join Date: Jun 2011
Posts: 25
Welshgasman is on a distinguished road
Default

Thank you Graham,

I'll give that a go and report back.
Reply With Quote
  #4  
Old 05-04-2018, 01:10 AM
Welshgasman Welshgasman is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2007
Novice
Outlook code not running now and again?
 
Join Date: Jun 2011
Posts: 25
Welshgasman is on a distinguished road
Default

Hi Graham,
Well I've given it a test for a few weeks and all has worked as I wanted.

Thank you for your help.
Reply With Quote
  #5  
Old 12-14-2018, 08:33 AM
Welshgasman Welshgasman is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2007
Novice
Outlook code not running now and again?
 
Join Date: Jun 2011
Posts: 25
Welshgasman is on a distinguished road
Default

Thought I had posted this the other day?

It is not working again after some time. Cannot pin down exactly when it happens. I just notice a sent email has not changed the greeting.
I close Outlook and start again and it works.?

Any possible ideas to the cause please.?

TIA
Reply With Quote
  #6  
Old 12-18-2018, 05:25 AM
gmayor's Avatar
gmayor gmayor is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I have been away for a couple of weeks so am just catching up on older stuff.
Did you change any code in Outlook VBA as that could stop it working. If it stops either restart Outlook or re-run the macro - Initialize_handler
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 12-18-2018, 08:39 AM
Welshgasman Welshgasman is offline Outlook code not running now and again? Windows 10 Outlook code not running now and again? Office 2007
Novice
Outlook code not running now and again?
 
Join Date: Jun 2011
Posts: 25
Welshgasman is on a distinguished road
Default

Hi Graham,
Thank you for the reply.

There is no Initialize_handler any more, that was removed/replaced as you said it was not necessary.?

Code is now as below and in module ThisOutlookSession, where it has always been located. The other day it stopped working almost immediately.

If I restart Outlook it then works again (for a short while at least) which was the same effect when using the Initialize_handler method.
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strGreeting As String
    Dim iTime As Integer
    Dim strBody As String, strDivCode As String
    strGreeting = ""
    strDivCode = "<div class=WordSection1><p class=MsoNormal>"
    iTime = Val(Format(Now(), "hh"))
  
    ' Quit if not a mail item
    If TypeName(Item) <> "MailItem" Then
        Exit Sub
    End If
    
    Select Case iTime
    Case Is < 12
        strGreeting = "morning "
    Case Is < 17
        strGreeting = "afternoon "
    Case Else
        strGreeting = "evening "
    End Select

    strGreeting = "Good " & strGreeting

    
    strBody = Item.HTMLBody
    strBody = Replace(strBody, "Good day", strGreeting)
    'strBody = Replace(strBody, strDivCode, strDivCode & strGreeting)
  
    Item.HTMLBody = strBody
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook Macros not Running from a Toolbar OTPM Outlook 0 03-16-2017 09:52 AM
Outlook 2007 running v-e-r-y s-l-o-w-l-y on old XP machine Jennifer Murphy Outlook 4 03-10-2017 01:07 AM
you must restart any running copies of outlook... otuatail Outlook 2 12-13-2015 03:38 AM
Outlook code not running now and again? Running outlook on what is now a slave drive. alexb123 Outlook 1 01-13-2012 10:48 AM
Outlook 2010 won't open links if Internet Explorer 8 is already running Svart Testare Outlook 0 01-28-2010 11:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft