Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

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

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, "h.mm am/pm")
strFolderNumber = InputBox("Please enter file number.")


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

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


    'ActiveWindow.Close

End Sub
0
andrewpiconnect
Asked:
andrewpiconnect
  • 4
  • 3
1 Solution
 
travalCommented:
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?
0
 
andrewpiconnectAuthor Commented:
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?
0
 
travalCommented:
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.Display
    objMessage.SaveAs directory
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
andrewpiconnectAuthor Commented:
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
0
 
andrewpiconnectAuthor Commented:
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.
0
 
travalCommented:
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
0
 
andrewpiconnectAuthor Commented:
Fantastic - thank you very, very much
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now