Link to home
Start Free TrialLog in
Avatar of jana
janaFlag for United States of America

asked on

What is causing a VBA script, in ThisOutlookSession to not work in Outlook 2007

We have a script (see below) placed in "ThisOutlookSession", under "Microsoft Office Outlook Objects" section, which after a while it tops working.

However, we exit Outlook and started again and it works.  The point is, the script just stops working.  We also place a stop and the control never goes to thos lines again, but when Outlook restarted, it works again.
Avatar of jana
jana
Flag of United States of America image

ASKER

Ooops! forgot the script (this script EE helped us):

[This a VBA script we use to have the BCC filled with a specific email.  unfortunately, after while, it stops working and no BCC is placed when sending, replying or forwarding an email]

'At the top of thisOutlookSession
'--------------------------------------------
'For placing in BCC an email
 Private WithEvents m_Inspectors As Outlook.Inspectors

Private Sub Application_Startup()
 'For placing in BCC an email
  Set m_Inspectors = Application.Inspectors
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Inspector)
'For placing in BCC an email
 Dim objRecip As Recipient
 Dim strMsg As String
 Dim strBCC As String
 If TypeName(Inspector.CurrentItem) = "MailItem" Then
    If Inspector.CurrentItem.EntryID = "" Then
       'Address for Bcc -- must be SMTP address or resolvable to a name in the address book
        strBCC = "email@domain.com"
        Set objRecip = Inspector.CurrentItem.Recipients.Add(strBCC)
        objRecip.Type = olBCC
        If Not objRecip.Resolve Then
           strMsg = "Could not resolve the Bcc recipient."
           MsgBox strMsg, vbInformation, "Could Not Resolve Bcc Recipient"
           End If
        Set objRecip = Nothing
        End If
    End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Thanx.  How do I run startup application?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Did it and got this error message:

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Sub or Function not defined
---------------------------
OK   Help  
---------------------------
The sub is in your snippet ... and it is what is run when you start the application in order to initialise the event handler.  Personally i'm confused

Chris
Avatar of jana

ASKER

The sub "Application_Startup" does exist and as recommended, in the immediate window I typed the Application_Startup and then pressed Enter key.

At this point, the sub m_Inspectors_NewInspector (see ID: 38259898) now  doesn't work.

The curious thing is that other VBA scripts outside ThisOutlookSession (that is, Forms and Modules), works perfectly.

Please advice.
Avatar of jana

ASKER

Just exited & restarted Outlook and works ok again.


Hope we can find the cause & solution.

Thanx in advance.
The other subs will be fine as long as they do not use events, whereas the event handler should be re-initialised via startup.

Hopefully someone will have solution tho I will try to think why the startup sub isn't initialising it here

Chris
Avatar of jana

ASKER

ok thanx; we are also googling to see if anything
Avatar of jana

ASKER

Maybe this info can help:

We were testing to see what is causing it and pass the info to the expert.  After restarting Outlook, we ran a macro not from ThisOutlookSession (the other subs).  Afterward click for new email so it can fire the macro that places an email in the BCC field (see code in ID: 38259898) and it didn't work; was disabled again the events.

So there is something in the sub that is causing dishable the events.  Here is the code that we ran prior:

Sub FillColumnSUPPORT()
 WhereFrom = 1 
 Call frmSUPPORT.Show
 If OptionSelected = False Then End 'Exit completely if non selected
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Type", olText, True)
            olkProp.Value = SUPPORTSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing
End Sub

Open in new window


Please review to see what is causing the event to get lost or disabled?

Thanx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Yeah worked!  We could get it activated again.

Please advice on our previous post.
I have to admit I do not have any clue. You can try adding an  ON ERROR RESUME NEXT at the very beginning of the sub, so the sub will not terminate on error. That might help.

On another note, this part of code looks strange:
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Type", olText, True)
            olkProp.Value = SUPPORTSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing

Open in new window

You can replace that by
        olkMsg.UserProperties.Add("Type", olText, True) = SUPPORTSelected
        olkMsg.Save

Open in new window

which does exactly the same.
Avatar of jana

ASKER

Understood.  How can we make the script work or how to place it in the sub the recommended "ON ERROR RESUME NEXT"
See in your original code you make no assumptions for the object type.  Set the type for olkMsg to object, add a test for TypeName(Inspector.CurrentItem) to ensure you are dealing in fact with a mail item and set olkProp to something ... at present you declare it and then immediately test for nothing ... it will always be nothing unless set to something and on every loop you again set it to nothing so you are not testing for the property as existing before going on to create it as new.

Sub FillColumnSUPPORT()
 WhereFrom = 1 
 Call frmSUPPORT.Show
 If OptionSelected = False Then End 'Exit completely if non selected
    Dim olkMsg As object, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Type", olText, True)
            olkProp.Value = SUPPORTSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing
End Sub

Open in new window


Chris
Avatar of jana

ASKER

We understood from the star, thanx.  Where do we pace ON ERROR RESUME and have it set correctly?
You miss the point I think .... Resume next can help albeit slowly for unknown errors but there are some things abo e you can fix which is more efficient

Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My mentioning of ON ERROR RESUME NEXT was just for testing purposes, or whenever it is much easier to ignore a (known or unknown) potential error in one or two lines. If it works with the RESUME stuff, we know we have to search for errors somewhere ...
Avatar of jana

ASKER

Goof observation.  We commented the line Call frmSUPPORT.Show  and it didn't affect the BCC script; all went ok.  We looked in the script and is just a simple message box:

Private Sub UserForm_Initialize()
'Just display form

End Sub 

Open in new window


We proceed and uncommented line of Call frmSUPPORT.Show so it can be executed and again the error of disabling ThisOutlookSession.
Avatar of jana

ASKER

Question: seeing that all we need is to run "Call ThisOutlookSession.Application_Startup", we placed at the end the sub (see below):

Sub FillColumnSUPPORT()
 WhereFrom = 1 
 Call frmSUPPORT.Show
 If OptionSelected = False Then End 'Exit completely if non selected
    Dim olkMsg As Outlook.MailItem, olkProp As Object
    For Each olkMsg In Application.ActiveExplorer.Selection
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkMsg.UserProperties.Add("Type", olText, True)
            olkProp.Value = SUPPORTSelected
            olkMsg.Save
        End If
        Set olkProp = Nothing
    Next
    Set olkMsg = Nothing

'Restart OutlookSession
 call ThisOutlookSession.Application_Startup

End Sub

Open in new window


But it gaves us the following error message:

         ---------------------------
          Microsoft Visual Basic
          ---------------------------
          Compile error:

          Method or data member not found
          ---------------------------
          OK   Help  
          ---------------------------


In conclusion, can we just place the "Call" at the end of the sub so it can be activated again??
If FillColumnSUPPORT is in the same module as Application_Startup, you can just use
   call Application_Startup
as the "context" is already set correctly (it works both for me). Though that should not make any difference.
Is the error line really the CALL?
Avatar of jana

ASKER

Yes, when the Call is executed, the event is disabled.  When I comment it, it not disable.

Note: FillColumnSUPPORT is not in the same module as Application_Startup, rather is "Modules" sections.

Maybe that is why of the error,
Avatar of jana

ASKER

Also we did find a way to run Application_Startup from the "Module" section:

- created a public sub in ThisOutlookSession:

          Sub AppStartup()
          Call Application_Startup
          End Sub

- then we call it from FillColumnSUPPORT as follows:

       Call ThisOutlookSession.AppStartup

And no error.

Nevertheless it sometimes work and other it just doesn't.

Any suggestion?
Avatar of jana

ASKER

I think we finally identify why it worked and sometimes it doesn't.  We noticed that Application_Startup doesn't work if sub FillColumnSUPPORT  is ended in line 4 (see ID: 38261898).  But if ended as normal End Sub, line 19, the Application_Startup works.

Please explained and how can we have it to always work prior exiting the sub.
Avatar of jana

ASKER

doesn't work even if i place it as follow, changing the "IF" line:

If OptionSelected = False Then
    Call ThisOutlookSession.AppStartup
    End 'Exit completely if non selected
    End If
>>> We looked in the script and is just a simple message box:

Assuming there is no other code in the form then it would seem the form is left open all the time so that is likely part of the problem.  Exactly what is the intended usage of the form?

Chris
Avatar of jana

ASKER

Is a ListBox since the form has a lot of lines, we deleted everything leaving it empty so as to just display or ".show" the form (this way to assure if it was a specific line).  And still the same error.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Yes it Worked !!!! By restructuring the "If" as recommended made it possible to maintain events enabled.

Thanx!
Avatar of jana

ASKER

Ok I think with this, we can close the question.  

In conclusion, and for future references, is it safe to say that in order for the "Events" not be wiped or disabled, we must contain the control within each sub routine and assure that not to use "If X = False Then End " instead restructure the "If" to continue and have the routine to exit as it supposed to?

Please advice and shed some light in this doubt we have.

Thanx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Thanx a Lot !!!!!
(at this point we are changing all subroutines with this issue. Thank you very much for everything)
Avatar of jana

ASKER

Thanx all!