Open a Database Based on a Rule in Outlook **Help Needed Please**

graviz

Registered User.
Local time
Today, 01:58
Joined
Aug 4, 2009
Messages
167
Is there a way to open a database (C:\Email Attachments\Test.mdb) when a rule in Outlook is triggered? I've seen you can call a Script but I don't know where to input one, nor how to write such a script. So basically a few questions:

1. Where do I input the script? (The vba editor?) If so I tried to do a function and save it but I didn't see it listed. Below is what I tried to do:

Public Function ODB()

Dim LPath As String
Dim LCategoryID As Long
LPath = "C:\Email Attachments\Test.mdb"
Set oApp = CreateObject("Access.Application")
oApp.Visible = True
oApp.OpenCurrentDatabase LPath

End Function

2. Can anyone please assist me with this basic script to open the DB above?

Thanks
 
You need to create a macro, in which there is a Sub (not function) that takes a MailItem as parameter. Then it should appear in the list of code to run, when you create a Rule to run some code. You can force Outlook to run the rule, and thus you can test your stuff.

Code:
Sub MyTestCode(MyMail As MailItem)

'in here you can do whatever you want
MsgBox("Hello world")

' you can also call other subs or functions
End Sub

BTW: "Help needed please " is redundant. Few postings, if any, are listed here for any other reasons. The best titles describe the issue at hand - like the first part of yours.
 
Last edited:
You need to create a macro, in which there is a Sub (not function) that takes a MailItem as parameter. Then it should appear in the list of code to run, when you create a Rule to run some code. You can force Outlook to run the rule, and thus you can test your stuff.

Code:
Sub MyTestCode(MyMail As MailItem)
 
'in here you can do whatever you want
MsgBox("Hello world")
 
' you can also call other subs or functions
End Sub

BTW: "Help needed please " is redundant. Few postings, if any, are listed here for any other reasons. The best titles describe the issue at hand - like the first part of yours.


Thank you. So it now appears in the Sripts however I don't think my code is correct. Here it is below:

Sub ODB(MyMail As MailItem)
Dim LPath As String
Dim LCategoryID As Long
LPath = "C:\Email Attachments\Test.mdb"
Set oApp = CreateObject("Access.Application")
oApp.Visible = True
oApp.OpenCurrentDatabase LPath

End Sub

When I try and run it from the vba editor it asks for a macro name. It also doesn't seem to do open the database when I run the rule. What am I missing?
 
Code:
When I try and run it from the vba editor it asks for a macro name.

You can't run this thing from the VBA editor, because it needs a MailItem.

Code:
It  also doesn't seem to do open the database when I run the rule.  What am I  missing?

Dunno. It's a while since I have messed around in Outlook. Does your sub run when triggered from a rule? Put a breakpoint in it, or put MsgBox "Hello World". Once that works then the next step is to sort out the code itself.
 
which version of outlook are you using.
 
Actually I am thinking in your case it does not matter which version of Outlook as rules and VBA are not the best way of doing this.

Here is what you can do to test this.

Please understand 2 important things.

1. This only works on items coming into your inbox. Thus if you already have a rule moving items to another folder it will not work.

2. You need to "Test" the email coming in - my example shows a test of the subject. It will only call you special routine IF and ONLY IF the subject has in it "My Test"

To enter the code in the Visual Basic Editor:

1. On the Tools menu, point to Macro, and then click Visual Basic Editor.
2. In the Project pane, click to expand the folders, and then double-click the ThisOutlookSession icon.
3. Type or paste the following code into the Code window.

Code:
 Dim WithEvents objInboxItems As Outlook.Items


' Run this code to start your rule.
Sub StartRule()
   Dim objNameSpace As Outlook.NameSpace
   Dim objInboxFolder As Outlook.MAPIFolder

   Set objNameSpace = Application.Session
   Set objInboxFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
   Set objInboxItems = objInboxFolder.Items
   
End Sub

' Run this code to stop your rule.
Sub StopRule()
   Set objInboxItems = Nothing
End Sub

' This code is the actual rule.
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
   If Item.Subject = "My Test" Then
      Call ODB
   End If
End Sub

Private Sub ODB()

Dim LPath As String
Dim LCategoryID As Long
LPath = "C:\Email Attachments\Test.mdb"
Set oApp = CreateObject("Access.Application")
oApp.Visible = True
oApp.OpenCurrentDatabase LPath

End Sub

4. On the File menu, click Save VbaProject.OTM.
5. You can now run the StartRule and StopRule macros to turn the rule on and off.
6. Quit the Visual Basic Editor.

(You might need to start and stop Outlook to get the variables to "Hook".
 

Users who are viewing this thread

Back
Top Bottom