Outlook: copy & paste

Status
Not open for further replies.

Andrzej

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hello,

I am new in VBA.
Can you help me with the following problem:
I need to copy and paste msg files from c:\msg\*.msg to Outlook 2007 folder (for example “for_send”)
I did it by copy function (see code below) but unfortunately outlook can't see it as mailitem.
Outlook also changes an icon of message into unread message.
When I do copy and paste – it works.
When I use attached macro – also works, but as mentioned before Outlook “does” something with msg file.
Can you tell me how to realize function copy and paste to work as doing it “by hand”?
Maybe completely different way… but how?

Yours faithfully,
Andrzej


Sub copy()

Dim inPath As String
Dim thisFile As String
Dim olApp As Outlook.Application
Dim msgFile As Object

Set olApp = CreateObject("Outlook.Application")

inPath = "C:\msg\"
thisFile = Dir(inPath & "\*.msg")

Do While thisFile <> ""
Set msgFile = olApp.CopyFile(inPath + thisFile, "for_send")
thisFile = Dir
Loop

End Sub
 
Thank you very much Diane,
I adapted macro from link, it works, but ... after copying msg files into Outlook folder message remains open for edition - I can't change this.
I would like to have the same effect as doing copy& paste.

Is there maybe another method to copy files from hard drive (msg) into Outlook folder as mailitem ?
 
ok... was able to make it work by opening the messages then copying -

Top of module:
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


After you get the file path:

ShellExecute 0, "open", FileItem.Path, vbNullString, vbNullString, 0

' need the timer so the message has time to open
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop

Set openMsg = Application.ActiveInspector.CurrentItem

openMsg.Display

'saves to inbox
openMsg.Copy

openMsg.Close olDiscard
 
ok - here is a better, faster version - it checks for msg files in the sourcefolder and copies them to outlook - you can hard-code the path or use the current folder.

Code:
Sub ImportMessagesInFolder()
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim SourceFolderName As String
    Dim FileItem As Scripting.File
    Dim strFile, strFileType As String
    Dim openMsg As Object
    Dim copiedMsg As MailItem
    Dim Savefolder As Outlook.Folder
   
    Set FSO = New Scripting.FileSystemObject
    SourceFolderName = "C:\Users\drcp\Documents\Attachments\"
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
   
   'Set the Outlook folder - use specific or use current folder
    'Set Savefolder = Session.GetDefaultFolder(olFolderInbox).Folders("Saved")
   Set Savefolder = Application.ActiveExplorer.CurrentFolder
   
    For Each FileItem In SourceFolder.Files
      strFile = FileItem.Name
      
' This code looks at the last 4 characters in a filename
    strFileType = LCase$(Right$(strFile, 4))
    If strFileType = ".msg" Then
    
    Set openMsg = Session.OpenSharedItem(FileItem.Path)
        On Error Resume Next
    Set copiedMsg = openMsg.Copy
        copiedMsg.Move Savefolder

    Set copiedMsg = Nothing
    openMsg.Close olDiscard
    Set openMsg = Nothing
    End If
     
    Next FileItem

    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
     
End Sub
 
Diane,
You are genius !
THANK YOU VERY MUCH!

Andrzej
 
Dear Diane,
Can you help me once again?
I've noticed that after sub is finished the copied e-mail remains in Inbox folder.
I have them doubled : in Inbox and in Savefolder.
I've checked it on Outlook 2010 and Outlook 2013.
Shall I wrote a piece of code which removes e-mails from Inbox or is there another solution?

Andrzej
 
After you copy to the inbox, you should be able to delete the original from the hard drive after closing it
openMsg.Close olDiscard
openMsg.Delete

If not try this -

openMsg.Close olDiscard
fso.DeleteFile openMsg

Accessing Files with FileSystemObject
 
Thank you very much !
It works.
I really appreciate your help.

Andrzej
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
G outlook 13 copy & paste without losing formatting Using Outlook 1
R Copy/paste mailing address Outlook 2013 Using Outlook 6
M "Attachment Detacher for Outlook" add in, does it update the server copy of the email? Using Outlook 1
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
O Outlook 365 - How to create / copy a new contact from an existing one? Using Outlook 5
C Copy Outlook contact field value to another field Outlook VBA and Custom Forms 1
J Copy to calendar function no longer working in outlook 365 Using Outlook 5
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
N Outlook rules don't create a copy for bcc'ed emails Using Outlook 3
geofferyh Outlook 2010 How to Copy Outlook Attachment to a Specific Folder? Outlook VBA and Custom Forms 3
R Copy Outlook Public Folders to a File Server Shared Folder Using Outlook 0
K Outlook Rules: Move a Copy Using Outlook 4
E Copy e-mail body from outlook and insert into excel Outlook VBA and Custom Forms 3
K ind specific Subject line from outlook and copy the content of the email body to exce Outlook VBA and Custom Forms 0
J Copy or Export Outlook Mail to Excel Outlook VBA and Custom Forms 6
Carrie Dickey Outlook 2016 created two calendars titled Calendar1 - appear to be a copy Using Outlook 2
P How to copy and append data from Outlook 2016 message into Excel 2016 workbook Using Outlook 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook Using Outlook 0
joflo copy contents from outlook to excell - please help. Using Outlook 4
A Macro to Copy Data from Outlook to Excel Spreadsheet Using Outlook 2
B Outlook being closed causing problems during copy to Excel Outlook VBA and Custom Forms 6
Mary B Outlook 2013: Rule for copying new email to folder & marking that copy as read Using Outlook 1
Vijay Kumar Copy tables from outlook to excel Using Outlook 12
L Outlook 2007 Copy Email Address in To Field Using Outlook 11
Aussie Looking for Outlook macro to Copy Recipient Names into Email Body Outlook VBA and Custom Forms 3
P Copy email address from outlook 2013/2010 Using Outlook 1
R How to copy outlook 2010 shared calendar appointment or meeting to personal calendar by vba? Outlook VBA and Custom Forms 1
J Cannot copy outlook form to mailbox of exchange 2k13 user Exchange Server Administration 1
L Outlook 2007 Copy Text Field Using Outlook 8
J Copy emails from a dead Outlook 2010 account Using Outlook 1
Z Copy Outlook 2007 "AutoComplete Contacts” to Outlook 2010 Using Outlook 7
N Restoring Outlook Calendar after iCloud copy Using Outlook 1
W Copy individual file folders in Outlook 2010 PST to a My Documents folder Using Outlook 4
C outlook 2003 always not remove temp copy of opened attachments in temporary fo Using Outlook 4
D outlook to save copy of hotmail? Using Outlook 3
N error when copying outlook data "can not copy: the path too deep" Using Outlook 6
P Outlook looking for Copy of Outlook.pst Outlook VBA and Custom Forms 1
A Outlook can't remember outlook.com, Exchange password. Using Outlook 0
S Related messages show in main Outlook window vice new Advanced Find windows Using Outlook 1
H Force Outlook 2019 with GMail 2-Step to Require Login? Using Outlook 0
G Retaining Tabs in outlook body Using Outlook 2
V Setting up Outlook 2021 on new computer Using Outlook 2
G Add Map It button to Custom Contacts Form in Outlook Outlook VBA and Custom Forms 1
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
Victor_50 Problem - Google Workspace will stop "unsafe" access to Outlook end 2024 Using Outlook 3
C New pc, new outlook, is it possible to import auto-complete emailaddress Using Outlook 4
T Outlook 365 won't take new working password Using Outlook 0
S Create Outlook Task from Template and append Body with Email Body Outlook VBA and Custom Forms 4
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2

Similar threads

Back
Top