Results 1 to 1 of 1

Thread: Copy and merge word doc. attachments from outlook?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Copy and merge word doc. attachments from outlook?

    Hi there,

    I'm a VB noob, any help would be appreciated.

    I'd like to modify this code, so that not only it saves the word document attachment on your computer from the email, but also merges the documents into 1 large file document.

    I would also be getting multiple docs with the same filename. I've looked at several options on this site,
    VB Code:
    1. Sub SaveAttachment()
    2.  
    3.     'Declaration
    4.     Dim myItems, myItem, myAttachments, myAttachment As Object
    5.     Dim myOrt As String
    6.     Dim myOlApp As New Outlook.Application
    7.     Dim myOlExp As Outlook.Explorer
    8.     Dim myOlSel As Outlook.Selection
    9.    
    10.     'Ask for destination folder
    11.     myOrt = InputBox("Destination", "Save Attachments", "C:\")
    12.  
    13.     On Error Resume Next
    14.    
    15.     'work on selected items
    16.     Set myOlExp = myOlApp.ActiveExplorer
    17.     Set myOlSel = myOlExp.Selection
    18.    
    19.     'for all items do...
    20.     For Each myItem In myOlSel
    21.    
    22.         'point on attachments
    23.         Set myAttachments = myItem.Attachments
    24.        
    25.         'if there are some...
    26.         If myAttachments.Count > 0 Then
    27.        
    28.             'add remark to message text
    29.             myItem.Body = myItem.Body & vbCrLf & _
    30.                 "Removed Attachments:" & vbCrLf
    31.                
    32.             'for all attachments do...
    33.             For i = 1 To myAttachments.Count
    34.            
    35.                 'save them to destination
    36.                 myAttachments(i).SaveAsFile myOrt & _
    37.                     myAttachments(i).DisplayName
    38.  
    39.                 'add name and destination to message text
    40.                 myItem.Body = myItem.Body & _
    41.                     "File: " & myOrt & _
    42.                     myAttachments(i).DisplayName & vbCrLf
    43.                    
    44.             Next i
    45.            
    46.             'for all attachments do...
    47.             'While myAttachments.Count > 0
    48.            
    49.                 'remove it (use this method in Outlook XP)
    50.                 'myAttachments.Remove 1
    51.                
    52.                 'remove it (use this method in Outlook 2000)
    53.                 'myAttachments(1).Delete
    54.                
    55.             Wend
    56.            
    57.             'save item without attachments
    58.             myItem.Save
    59.         End If
    60.        
    61.     Next
    62.    
    63.     'free variables
    64.     Set myItems = Nothing
    65.     Set myItem = Nothing
    66.     Set myAttachments = Nothing
    67.     Set myAttachment = Nothing
    68.     Set myOlApp = Nothing
    69.     Set myOlExp = Nothing
    70.     Set myOlSel = Nothing
    71.    
    72. End Sub

    I thought about using this code here to look for docs with the same filename and change them. However i'm not sure how to implement it.
    VB Code:
    1. If ActiveWorkbook.Saved = True Then
    2.         Dim sName As String
    3.         Dim i As Integer
    4.         Dim bNum As Boolean
    5.         sName = ActiveWorkbook.Name
    6.         'look for the version number
    7.         i = 1
    8.         Do While bNum = False
    9.             If IsNumeric(Mid$(ActiveWorkbook.Name, i, 1)) = True Then
    10.                 bNum = True
    11.                 Exit Do
    12.             End If
    13.             i = i + 1
    14.         Loop
    15.         If bNum = True Then
    16.             'Save and increment the number by 1
    17.             ActiveWorkbook.SaveAs Replace(ActiveWorkbook.Name, Mid$(ActiveWorkbook.Name, i, 1), Mid$(ActiveWorkbook.Name, i, 1) + 1)
    18.         Else
    19.             'Add a 1 to the end of the name
    20.             ActiveWorkbook.SaveAs Replace(ActiveWorkbook.FullName, ".xls", "1.xls")
    21.         End If
    22.     Else 'New book so save with name and 1.xls
    23.         ActiveWorkbook.SaveAs "C:\SomeFileName1.xls"
    24.     End If

    The whole thing would be to take multiple emails pull there attachments from outlook, and merge them all into one word document. However some of the docs have the same filename, eventhough there contents would not be the same. Any help or guidence would be much appreciated. I'm getting tons of errors here and there, so i'm not even sure where to exactly how to merge these two very different scripts.
    Last edited by si_the_geek; Nov 29th, 2005 at 01:18 PM. Reason: added VBCode tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width