Help with Rules and Scripts

S

SteveB

I wrote a VBA "script" that I'm attempting to execute thru a rule in
Outlook 2003. The "script" works just fine as a "macro" but when I try
to run it from the rule I get a "Rules in Error" message stating that
"The script 'Project1.xxxxxx' doesn't exist or is invalid".

I assume that since I can pick the script from the "Select Script"
dialog box that the script does exist so something must be invalid. I
cannot determine what that "something" is that's making it invalid.

Any ideas on how to debug or troubleshoot?

THANKS!
 
S

Sue Mosher [MVP-Outlook]

Show your code, or enough of it so we can see how you're set up the procedure.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SteveB

Here it is, please be gentle, I'm not a programmer!

Sub SaveAttachmentRule(objItem As MailItem)
Dim objApp As Outlook.Application
Dim objItem As Object

Set objApp = CreateObject("Outlook.Application")
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
Call CopyAttachments(objItem)

Set objApp = Nothing
Set objItem = Nothing
End Sub

'=============================================================
Sub CopyAttachments()
Dim fso As Scripting.FileSystemObject
Dim fldTemp As Scripting.Folder
Dim objAtt As Outlook.Attachment
Dim strHoldFilePath As String
Dim strFilePath As String
Dim strFileExt As String
Dim strFileName As String
Dim intLoc As Integer
Dim I As Integer

Set fso = CreateObject("Scripting.FileSystemObject")
strHoldFilePath = "Q:\LicenseRenewalTesting\"
For Each objAtt In objSourceItem.Attachments
If objAtt.Type = olByValue Then
strFilePath = strHoldFilePath & objAtt.FileName
If fso.FileExists(strFilePath) Then
intLoc = Len("objAtt.FileName")
strFileName = left(objAtt.FileName, intLoc - 4)
strFileExt = Right(objAtt.FileName, 3)
I = 1
Do
strFilePath = strHoldFilePath & strFileName &
CStr(I) & _
"." & strFileExt
I = I + 1
Loop While fso.FileExists(strFilePath)
End If
objAtt.SaveAsFile strFilePath
End If
Next

Set objAtt = Nothing
Set fldTemp = Nothing
Set fso = Nothing

End Sub
 
S

Sue Mosher [MVP-Outlook]

The way you have the procedure declared looks fine -- so you might just restart Outlook and see if the rule will take.

But ... the procedure doesn't actually do anything with the incoming message, so it's not going to be effective. A "run a script" rule action needs to look something like this:

Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
MsgBox msg.SUbject

Set msg = Nothing
Set olNS = Nothing
End Sub

msg is the object you want your code to work on.

The CopyAttachments procedure also won't do anything because it contains no information about which item's attachments are being copied. (Add an Option Explicit declaration at the top of your code module to help prevent such obvious coding errors.) It does contain an objSourceItem, which presumably is the item you want to act on. Include that as a paramter in your procedure declaration:

Sub CopyAttachments(objSourceItem)

If you do that, then you can call the procedure from the rule "script":

Call CopyAttachments(msg)

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Joined
Aug 8, 2011
Messages
1
Reaction score
0
I had this same issue... what fixed it for me was removing the rule, then renaming the project, then adding the rule back in...

i was getting the " Cannot run "" script" error....

Also make sure you compile your script.... Debug ... Compile Project..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top