Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How do I convert VBA script to Perl? - Using Win32::OLE

by Win (Novice)
on Jan 24, 2006 at 15:28 UTC ( #525200=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Please could monks guide me on converting the VBA script (below) to Perl using Win32::OLE. I do not ask anyone to go through it all. However, guidance on the bits that are not straight forward would be very welcome. For example,the Exit Sub command. Do I just use the 'last' command here? How do I declare these variable types? Do I do it using Win32::OLE or do I do it using standard Perl syntax? A few bits that I have tried to convert are:
$Inbox = $ns->GetDefaultFolder(olFolderInbox); #Set Inbox = ns.GetD +efaultFolder(olFolderInbox)
and
if ($Inbox->Items->$Count = 0) { #If Inbox.Items.Count = 0 T +hen
and
foreach ($Inbox->Items){ #For Each Item In Ibox.Items
The bit of code in question is:
Option Explicit Private Sub Application_NewMail() On Error GoTo GetAttachments_err ' Declare variables Dim ns As NameSpace Dim Inbox As MAPIFolder Dim Item As Object Dim Atmt As Attachment Dim FileName As String Dim i As Integer Set ns = GetNamespace("MAPI") Set Inbox = ns.GetDefaultFolder(olFolderInbox) i = 0 'Check Inbox for messages and exit of none found If Inbox.Items.Count = 0 Then Exit Sub End If ' Check each message for attachments For Each Item In Inbox.Items ' Save any attachments found For Each Atmt In Item.Attachments ' This path must exist! Change folder name as necessary. If Left(Atmt.FileName, 10) = "Flat_file_" Then FileName = "G:\Input\" & Atmt.FileName Atmt.SaveAsFile FileName i = i + 1 End If Next Atmt Next Item 'Clear memory GetAttachments_exit: Set Atmt = Nothing Set Item = Nothing Set ns = Nothing Exit Sub GetAttachments_err: Resume GetAttachments_exit End Sub
This question follows my last one where very good links were offered by monks. Maybe I can execute this block of code in a single go.

Replies are listed 'Best First'.
Re: How do I convert VBA script to Perl? - Using Win32::OLE
by marto (Cardinal) on Jan 24, 2006 at 15:44 UTC
    Win,

    By the looks of it VB code you have posted uses outlook to query emails for attachments and save the attachments do disk. If this question is because you dont understand VB is there a reason why you have to do this via Win32::OLE and not using other modules? The reason I ask is that a Super Search of the topic returns Exchange Server and IMAP, the reply posts would be a good place to start looking. This even came up yesterday with Save Email Attachment to File which may suit you down to the ground.

    As I suggested in reply to your previous question, Super Search is your friend :)

    Martin
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do I convert VBA script to Perl? - Using Win32::OLE
by murugu (Curate) on Jan 24, 2006 at 15:57 UTC

    Hi Win,

    There is a VBscript converter in Activestate PDK. Basically I use PDK to manage my perl modules and for debugging, as it has an excellent graphichal interface. I never used VBScript converter. You can check it here.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: How do I convert VBA script to Perl? - Using Win32::OLE
by serf (Chaplain) on Jan 24, 2006 at 15:48 UTC
    If you're going to be building this block of code the same way in Perl i.e. using a sub then you could use return where it has Exit Sub.

    I'm sorry I'm on Linux so can't text Win32 MAPI stuff for you, but if your piece of code:

    if ($Inbox->Items->$Count == 0)
    works (by the way that's == for a test, not = which would be an assignment) then you could use:
    return if ($Inbox->Items->$Count == 0);
Re: How do I convert VBA script to Perl? - Using Win32::OLE
by pKai (Priest) on Jan 24, 2006 at 17:50 UTC

    Years ago i made a Windows service which among other things also saved attachments from incoming messages from an inbox. Then I used IMAP as suggested already.

    Also, if you search the internet for doing that with MAPI you learn that this can be a pain if possible at all inside your environment parameters (perl, in a service,...)

    But because recently we decided we badly need that functionallity (IMAP will be disabled at some point in the future for the Exchange servers in question), I made a new attempt last month.

    The following is a boiled down code from the real code I finally brought to production, which roughly resembles the VB in the OP.

    The VB code from the OP remained as perl comments here for orientation, what happens about around that line.
    Indented comments are all mine. If that is too distracting that VB code can be filtered out with something like

    perl -ne "print unless /^#/" mapi.pl

    This code is untested but should work modulo typoes.

    Edit: Cleaned up some incoherently on the fly renamed variables

    You may comment the added lines with the "filter unread messages only" and the "sort with respect to message delivery time", if not needed.

    Final remark: The code makes up a dynamic temporary MAPI profile. This feature is only available with a resonable (?!) current CDO on your machine. You should be fine, if OL 2002 or (2003) is installed on your machine :-/ (not tested otherwise).

    Enjoy!

    Edit: ADO?? I must have been involved in AD automation too much lately
    :1,.-2s/ADO/CDO/g

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do I convert VBA script to Perl? - Using Win32::OLE
by ikegami (Pope) on Jan 24, 2006 at 16:17 UTC

    I can't help you much — this is outside my area of expertise — but I see some problems.

    In Inbox = $ns->GetDefaultFolder(olFolderInbox);, are you sure olFolderInbox is correct? Be sure to use use strict; and use warnings;.

    if ($Inbox->Items->$Count = 0) {
    should be
    if ($Inbox->Items->Count = 0) {

    foreach ($Inbox->Items) {
    should be
    foreach (in $Inbox->Items) {
    Don't forget to import in:
    use Win32::OLE qw( in );

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://525200]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-11-27 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?