vb code that asks for file name, then saves to file works in word but not outlook

Posted on 2005-04-27
Last Modified: 2010-08-05
I have the following macro which works fine in word, and allows me to save a file automatically to a desired destination called "email DD/MM/YYYYY" but when I run this code in Outlook, it falls over both at ChangeFileOpenDirectory...... and then on the next line ActiveDocument.SaveAs FileName........

The code I have is as follows, I assume that the file should be saved as a .msg file rather than .doc file for outlook but this makes no diference to the running of this: -

Sub emailsave()
' emailsave Macro
' Macro created 27/04/2005 by Andrew Paterson
Dim strFolderNumber As String
CurrentDate = Format(Date, "mm-dd-yy")
CurrentTime = Format(Now, " am/pm")
strFolderNumber = InputBox("Please enter file number.")

    ChangeFileOpenDirectory _
        "\\Server1\files\" + strFolderNumber

ActiveDocument.SaveAs FileName:="Email" & " " & CurrentDate & " " & CurrentTime & " " & ".doc"


End Sub
Question by:andrewpiconnect
    LVL 21

    Expert Comment

    Can you attempt it first on a message using static values for the directory and filename (ie just ChangeFileOpenDirectory "\\Server1\files\tempfolder" without the string being added), to see if that is OK?

    Author Comment

    Thanks for that but yes, have done - just returns with Sub or Function not defined -- -- seems to not like ChangeFileOpenDirectory command, but this works fine when running the macro in word? As it is an e-mail stored in my inbox that is open rather than a "file" could this be the cause of my problem - I am not really a VB expert, so wondered if there is another command to change the values of the directory?
    LVL 21

    Accepted Solution

    OK, I hadn't ever used that ChangeFileOpenDirectory option in Outlook, so your test seems to indicate you can't use it at all.  

    This Macro works, using saveas and a string value you could create called directory.  I'm not dynamically creating the directory string, I'm just setting it statically, but that won't have any affect on the functionality of the objMessage.SaveAs directory statement

    Public Sub SaveMsg()
        Dim objApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objInbox As Outlook.MAPIFolder
        Dim objMessage As Outlook.MailItem
        Dim objOutbox As Outlook.MAPIFolder
        Dim directory As String
        Set objApp = CreateObject("outlook.application")
        Set objNS = objApp.GetNamespace("MAPI")
        Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
        directory = "C:\temp\testing.msg"

        'save the first message of Inbox as a .msg file
        Set objMessage = objInbox.Items.GetFirst
        objMessage.SaveAs directory
    End Sub

    Author Comment

    Fantastic -  worked a dream once I added my input box -  thank you very much indeed, this will save me no end of time in the future

    Author Comment

    Traval - just one thing -  how do I get the code to save a highlighted email rather than first one received as i do not always work systematically! Also I assume that  I can run a similiar code for the sent items, I assume line ---  Set objMessage = objInbox.Items.GetFirst  ----- needs changing?

    Again, many thanks - I know I have already awarded points, but a quick confirmation to the above would be very much appreciated.
    LVL 21

    Expert Comment

    Glad that is working.  Try changing the line to this to use the currently open item from any folder rather than the first item in the inbox:

    Set objMessage = Application.ActiveInspector.CurrentItem

    Author Comment

    Fantastic - thank you very, very much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Learn more about the importance of email disclaimers with our top 10 email disclaimer DOs and DON’Ts.
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now