• Automating Contacts Exports (OL2000)

    Author
    Topic
    #432976

    Hi there,

    As a backup measure I routinely export my contacts folder with its subfolders to a PST file. I then close Outlook (so it lets go of the newborn PST), encrypt the PST and e-mail it to an account I keep for storage purposes.

    However, I find this procedure kind of awkward to the extent that I tend to delay it as much as possible. So I tought about using VBA to automate the relevant part within Outlook.

    I’ve had experiences with VBA in Access and Excel, but don’t know where to start in Outlook. For instance, I don’t know what to call the “ThisOutlookSession” thing hanging from “Project 1” in the VB Editor. Does anyone have a hint?

    Thank you in advance

    Viewing 0 reply threads
    Author
    Replies
    • #1016979

      > I don’t know what to call the “ThisOutlookSession” thing hanging from “Project 1” in the VB Editor. Does anyone have a hint?

      I’m not sure what that is called either, other than ThisOutlookSession. Is it important? If you plan to automate Outlook (so that you can close it and continue executing code), it probably is easier to store your code externally, whether in a VB form, a VBScript file, or another VBA host.

      Some potentially useful objects and methods:

      dim appOutlook as Outlook.Application
      dim ns as Outlook.NameSpace
      'get/create application -- snag code from elsewhere
      'create ref to namespace
      set ns = appOutlook.GetNamespace("MAPI")
      'create new pst for "export"
      dim strName as string, strPath as string
      strname = "backups_contacts-" & format(Date,"yyyymmdd")
      strpath = "c:" & replace(strname,"_","") & ".pst"
      ns.AddStoreEx strpath,olStoreUnicode
      'why doesn't that method return a reference to the folder? beats me...
      'choose source and destination (new PST) folders
      dim fldSource as outlook.mapifolder, fldDest as outlook.mapifolder
      set fldsource = ns.pickfolder
      set flddest = ns.pickfolder
      'copy source folder; might need to iterate over subfolders
      fldsource.copyto(flddest)
      'close/disconnect new PST (rename from generic "Personal Folders" now that we know fldDest)
      flddest.name = strname
      ns.removestore(strname)
      'exit Outlook
      ns.logoff
      appOutlook.Quit
      set flddest = nothing
      set fldsource = nothing
      set ns = nothing
      set appOutlook = nothing
      'now do file system stuff with strPath

      • #1017135

        Hello Jefferson,
        [indent]


        I’m not sure what that is called either, other than ThisOutlookSession. Is it important?


        [/indent]Not really so. I was trying to mean I didn’t know whether that was the right place to place the VBA script on. On one hand, it seems important because it has a name. On the other hand, there’s only ONE such thing. That confused me.[indent]


        If you plan to automate Outlook (so that you can close it and continue executing code), it probably is easier to store your code externally, whether in a VB form, a VBScript file, or another VBA host.


        [/indent]I understand what you say, but I don’t have the VB standalone product at home. I might try learning how to code the script at work, but that will take some spare time put to good use. As for VBScript, I don’t know how to use it either. Does it require any programming package?

        Regarding the code itself, I’ll need some time to understand it and if possible ellaborate on it a little. I’ll get back to you when I have some news.

        Thank you for your time!

        • #1017136

          VBScript is similar to VBA, but with just enough differences to give you a headache. You might consider using Excel as your host. Just create a new module in a workbook, add a reference to the Microsoft Office Outlook object library (Tools>References in the VB Editor), and automate Outlook from there.

          • #1017285

            Good. Your explanation will help me work things out. I’ll keep you posted.
            Thanks!

        • #1017196

          A couple of things that may help explain stuff further.

          You have probably figured out that Outlook doesn’t have a macro recorder. The Outlook Object model is very far from fully exposed the way Word, Access, PowerPoint and Excel are – partly because Outlook is a mix of a MAPI mail engine and a PIM, and very probably because Microsoft is trying to limit the use of Outlook as a spam engine. For example, the file export capabilities you find in the Outlook menu are not exposed, presumably because someone could write a virus to steal contacts email lists too easily. However there are other things that are not exposed in the Object model which I can’t fathom as being a target of potential viruses, such as the detail of user Views, so who knows why MS is limiting exposure of some sections the Object Model.

          The ‘ThisOutlookSession’ module is a kind of Class Module, primarily geared to providing a location for hooks into Outlook Events. ThisOutlookSession can also hold non-event code, or you can create a separate Module for other code just as you would in any other Office program.

          For a couple of examples of code that sucks Contact information out into another format, see Jefferson’s and Rory’s posts using ADO in this VBA thread, or to get ALL the Contact Properties, you could try my post in Excel which is WAY slower than the ADO methods. If you use a VBA technique to directly access Contact Properties, install ClickYes.

          HTH.

          • #1017287

            Hello John,

            Thanks for your input too. I couldn’t get hands to this today, but hopefully I’ll get started this weekend.
            thankyou

          • #1019644

            Hello Jefferson and John,

            It’s been a while since I last posted. Well here’s what I tried:

            1) I first tried using Excel and Word as hosts for the VBA code. I could advance very little. I was aware that Outlook’s object model was somewhat different, as John states above. In fact I got to know Outlook Redemption before my first post. But I had the hope a simple export wouldn’t require me sitting for so long. I don’t have the time to deal with this “cleanly”, so instead
            2) I wrote a little AutoIt script that, although not so politically correct, does the work. I can post the script if you will, but I don’t think it will be of much use because it’s based on Send() and Sleep() functions, the former being reliant on the Outlook version and language. So instead of dealing with objects it just automates human keystrokes. The procedure is automated to the extent of closing Outlook via Alt+F4. What’s left for me is to check that no instance of Outlook.exe is still running in the background, zip the resulting PST file and finally email it to one of my hosting accounts.

            I appreciate all your time and dedication. I’m sure I’ll have another go at this some time in the future, but I don’t have the time right now.

            Thank you!

            • #1019748

              I wouldn’t describe Send() or the VBA method SendKeys as politically incorrect. Just not reliable enough to distribute to others as a general purpose solution due to potentially strange timing issues and environmental differences.

            • #1019965

              I agree.

              BTW, when I first picked this up I tried placing VBA code in the ThisOutlookSession module. I believe this is what caused Outlook to prompt me to enable/disable macros each time I click on the SpamBayes (a third-party antispam plugin) “Delete as Spam” toolbar button (see attached). I removed the code in the module but I still get prompted.

              Has something similar ever happened to you? Is there something I can do to stop Outlook from prompting me?

              Thanks

              Edited to include attachment

            • #1019984

              If SpamBayes is not “signed” in a manner that bypasses security, then you should receive a request for permission. Did it behave differently before? Perhaps the checkbox to trust add-ins got unchecked (or maybe I am thinking of Word…).

            • #1020144

              Hi Jeff,

              Yes, SpamBayes used to behave OK before I placed the code in the module. I clicked on the toolbar button and nothing happened, except that the message was processed and then sent to the SPAM folder. No prompts.
              I cannot find the checkbox you mention. Where is it supposed to be? I’m using OL 2000.

            • #1020034

              Or perhaps you changed Macro Security from Low to Medium?

            • #1020146

              Not intentionally. However, I cannot find that dialog box / tab either. There’s a Security tab in Tools | Options, but nothing like what you mention in there…

            • #1020147

              Found it – Bingo.

              As I mentioned I didn’t change it manually, so altering the ThisOutlookSession module must have set the security to Medium.

              Thanks again Jeff and John for your help!

    Viewing 0 reply threads
    Reply To: Automating Contacts Exports (OL2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: