2 mostly identical scripts, only one works

Status
Not open for further replies.

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
hello friends,

i have 2 functionally identical scripts in outlook, but only 1 of them works.

this one works:
Public Sub Name1(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "S:\Folder\Folder"
Dim dateFormat As String
dateFormat = Format(itm.ReceivedTime, "mm.dd.yyyy")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "File Name " & dateFormat & ".xlsx"
Next
End Sub
this one does not:
Public Sub Name2(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "S:\Slightly\Different\Folder"
Dim dateFormat As String
dateFormat = Format(itm.ReceivedTime, "mm.dd.yyyy")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "Slightly Different File Name " & dateFormat & ".pdf"
Next
End Sub
When i try to run these as a rule the second one gives the error "the script "" doesnt exist or is invalid"

im not sure what to do...
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
BTW, use this macro to step through the 'bad' macro without running rules - to use it, select a message with the attachments and step into the macro - where does it error? (and change the name of the macro you want to run)

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
macro2 objItem

End Sub
 

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
BTW, use this macro to step through the 'bad' macro without running rules - to use it, select a message with the attachments and step into the macro - where does it error? (and change the name of the macro you want to run)

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
macro2 objItem

End Sub


yes both scripts are in separate modules, named differently.

i would prefer to run them as rules, we receive several reports every day with the same information. i dont necessarily need to read/review the information, i would just like it to be in a local folder with date for when i do.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Use the script i posted to test it outside of rules - it basically fakes the rule, so you don't need to send mail or use run rules. Use Step into and watch each line it hits - this could give you a clue as to which line is the problem, provided it doesn't error out immediately.

I'm assuming you copied the working macro and made changes? If not, there could be "weird" spaces that VBA does like - usually at the beginning or end of code. Backspace to remove them.
 

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Use the script i posted to test it outside of rules - it basically fakes the rule, so you don't need to send mail or use run rules. Use Step into and watch each line it hits - this could give you a clue as to which line is the problem, provided it doesn't error out immediately.

I'm assuming you copied the working macro and made changes? If not, there could be "weird" spaces that VBA does like - usually at the beginning or end of code. Backspace to remove them.

sorry i have been on leave from work,

im not entirely sure i follow. i use the step into function and i get a yellow arrow (i assume error) on the first line of the macro you provided.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
InventoryAdjustments objItem

End Sub

i have also attached a picture of my modules to make sure these are organized appropriately.

 

Attachments

  • stuff.png
    stuff.png
    4.8 KB · Views: 374

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
nothing wrong with the organization - put them all in a module (they can share one module, especially since they are small - long macros are easier to work on when in separate modules as its easier to find them).
It works here - i had to change the folder path, but that shouldn't cause an error until you get to that point.
This is a video of my steps:
MacroTest-20170712

The yellow line when you step into a macro shows what line you are at. It only means error when there is an error message. :)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
That's running this script? Sub RunScript()
Where does it stop?
Is this the name of the run a script macro? InventoryAdjustments (which is Name2 in this thread).

The module name (RuleScripts in my video) is only to make it easier for you to find the macro (if you have a lot). You don't actually use it for anything directly in Outlook.
 

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
so when i run it the way you do in the video it "works" except

1. The PDF file generated is "not a pdf or corrupted"
2. Still will not run as part of the rule.
 

Attachments

  • 1.png
    1.png
    15.6 KB · Views: 379

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
OK everything seems to be operating smoothly on the rule side of things. THANK YOU!!!

but now i have a new problem. some of the emails that i am pulling information from have tiny (<500 Bytes) useless attachments from whatever software generates them, or the same reports in a different format.

When the script is run, it pulls each attachment and saves it to the correct location, but since the other attachments are so small it will post/save first even though the relevant attachment is processed first.

is there a way for the script to reference only the first attachment? they are always appended in the same order.
multiattach.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Do you want all files or just the pdf? You can use an if statement to only grab he pdf.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Example code - Save Attachments to the Hard Drive

For Each objAtt In itm.Attachments
If lcase(right(objatt.filename, 4)) = ".pdf"
objAtt.SaveAsFile saveFolder & "\" & "Slightly Different File Name " & dateFormat & ".pdf"
End if
Next
End Sub
 

Tim Hagen

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Example code - Save Attachments to the Hard Drive

For Each objAtt In itm.Attachments
If lcase(right(objatt.filename, 4)) = ".pdf"
objAtt.SaveAsFile saveFolder & "\" & "Slightly Different File Name " & dateFormat & ".pdf"
End if
Next
End Sub

i would just need the attachment in the first position, which should always be the same file type. ill try the if statement.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
i would just need the attachment in the first position, which should always be the same file type. ill try the if statement.

There is an example at the link that goes by the count - you'd use (1) to always get the first. But it's safer to use the file type if it should always be a pdf (or another file type)


Sent from my iPad using Tapatalk
 
Status
Not open for further replies.

Similar threads

Top