Outlook: copy & paste

Status
Not open for further replies.

Andrzej

New 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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange

Andrzej

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
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 ?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Andrzej

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Diane,
You are genius !
THANK YOU VERY MUCH!

Andrzej
 

Andrzej

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Andrzej

New Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Thank you very much !
It works.
I really appreciate your help.

Andrzej
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
G outlook 13 copy & paste without losing formatting Using Outlook 1
R Copy/paste mailing address Outlook 2013 Using Outlook 6
S I'm not able to copy and paste on outlook. Using Outlook 1
M Outlook 2007 Copy-Paste problems Using Outlook 6
F I cannot highlight text in Microsoft Outlook 2010 e-mails, and copy/paste to the body of another e-m Using Outlook 3
J How to copy and paste in Outlook 2010 ? Using Outlook 3
S Copy/Paste from Office Word 2003 to Outlook 2003 Using Outlook 3
O How do I copy and paste calendar dates in 2010 outlook? Using Outlook 1
N Outlook 2007 - Attachment Copy/Paste issue Using Outlook 1
N User is trying to COPY and PASTE text document in outlook. But the output of copied text is an image. Using Outlook 5
J Copy/paste in outlook & pasted text is in html Using Outlook 17
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 New Slipstick.com Articles 0
Diane Poremsky Use a macro to copy data in Outlook email to Excel workbook New Slipstick.com Articles 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
P I have Outlook 2007 on a two year old computer running Vista. How do I copy my Outlook Contacts list and move them over to Outlook 2010 on a brand new Using Outlook 1
F Need to copy an email folder to another computer. How can i do this? Outlook 2010 Using Outlook 2
R Upgrading to licensed copy from 2010 beta - Outlook won't work Using Outlook 1
H how do i copy outlook mails from my office system to my laptop Using Outlook 2
B Export from Outlook Express and Windows Live Mail fails to copy to Outlook 2010 Using Outlook 1
D Outlook will not import backup copy of contacts. I have tried file-import-etc. but nothing happens. Using Outlook 3
D How can I export a .pst file without a working copy of Outlook? Using Outlook 3
B Would like to copy a logo in my signature of my email in outlook Using Outlook 1
Similar threads


















































Top