6 Replies - 2280 Views - Last Post: 02 July 2012 - 07:21 AM Rate Topic: -----

#1 MVSb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-June 12

To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 06:54 PM

HI,
I have been using the following code for downloading attachments from outlook to a folder.
I now wanted to make it more specific for my purpose.
I just want to download files with .doc and pdf type to my folder.
Can some specify some way to do it
The code i hav used is as belows:

Imports Microsoft.Office.Interop




Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objOL As Outlook.Application
        Dim objMsg As Outlook.MailItem 'Object
        Dim objAttachments As Outlook.Attachments
        Dim objSelection As Outlook.Selection
        Dim I As Long
        Dim lngCount As Long
        Dim strFile As String
        Dim strFolderpath As String
        Dim strDeletedFiles As String
        Dim Counter As Long
        Dim timeStamp As DateTime = DateTime.Now

        strFolderpath = "C:\Documents and Settings\E1002176\My Documents\" + TextBox1.Text + "/" + TextBox2.Text




        If (Dir$(strFolderpath, vbDirectory) = "") Then
            MsgBox("'" & strFolderpath & "'  not exist")
            MkDir(strFolderpath)
            MsgBox("'" & strFolderpath & "'  we create it")

        Else
            MsgBox("'" & strFolderpath & "'  exist")
        End If

        ' Get the path to your My Documents folder
        'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
        strFolderpath = strFolderpath & "\"
        On Error Resume Next

        ' Instantiate an Outlook Application object.
        objOL = CreateObject("Outlook.Application")

        ' Get the collection of selected objects.
        objSelection = objOL.ActiveExplorer.Selection

        ' The attachment folder needs to exist
        ' You can change this to another folder name of your choice

        ' Set the Attachment folder.
        strFolderpath = strFolderpath

        ' Check each selected item for attachments.
        Counter = 1
        For Each objMsg In objSelection

            objAttachments = objMsg.Attachments
            lngCount = objAttachments.Count

            If lngCount > 0 Then

                ' Use a count down loop for removing items
                ' from a collection. Otherwise, the loop counter gets
                ' confused and only every other item is removed.

                For I = lngCount To 1 Step -1

                    ' Get the file name.
                    strFile = objAttachments.Item(I).FileName

                    ' Combine with the path to the Temp folder.
                    strFile = strFolderpath & Counter & "_" & strFile

                    ' Save the attachment as a file.
                    objAttachments.Item(I).SaveAsFile(strFile)
                    Counter = Counter + 1
                Next I
            End If

        Next

ExitSub:

        objAttachments = Nothing
        objMsg = Nothing
        objSelection = Nothing
        objOL = Nothing
        MsgBox("All Selected Attachments Have Been Downloaded ...")
    End Sub

   

       

   
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: To extract .doc and Pdf files from outlook attachments

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 08:04 PM

Line 21: Don't hardcode paths like that. Use the SpecialFolders enumeration to get your documents folder and System.IO.Path.Combine to add multiple strings together to form a path.

Loop 65-77: This is where you are just saving all attachments. So do an 'if' check on the attachment name to see if its extension is one of the two you want. If it is, copy it. If not, don't.

<HINT>
System.IO.Path.GetExtension(strFile)
Was This Post Helpful? 1
  • +
  • -

#3 MVSb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-June 12

Re: To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 08:34 PM

HI, thanks for the post. It had been really helpful.
Was This Post Helpful? 0
  • +
  • -

#4 MVSb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-June 12

Re: To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 09:02 PM

View PosttlhIn`toq, on 01 July 2012 - 08:04 PM, said:

Line 21: Don't hardcode paths like that. Use the SpecialFolders enumeration to get your documents folder and System.IO.Path.Combine to add multiple strings together to form a path.

Loop 65-77: This is where you are just saving all attachments. So do an 'if' check on the attachment name to see if its extension is one of the two you want. If it is, copy it. If not, don't.

<HINT>
System.IO.Path.GetExtension(strFile)

Can you also please help me what is the syntax to be given to for using pdf and .doc file extensions to be saved. I mean how to give them?
Thank You
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 09:08 PM

I don't exactly understand the question.

Can you show me your updated code where you tried to use the suggestion I made?
Was This Post Helpful? 1
  • +
  • -

#6 MVSb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-June 12

Re: To extract .doc and Pdf files from outlook attachments

Posted 01 July 2012 - 09:57 PM

View PosttlhIn`toq, on 01 July 2012 - 09:08 PM, said:

I don't exactly understand the question.

Can you show me your updated code where you tried to use the suggestion I made?

.............................
If System.IO.Path.GetExtension(.doc) Then
                    For I = lngCount To 1 Step -1

                        ' Get the file name.
                        strFile = objAttachments.Item(I).FileName

                        ' Combine with the path to the Temp folder.
                        strFile = strFolderpath & Counter & "_" & strFile

                        ' Save the attachment as a file.
                        objAttachments.Item(I).SaveAsFile(strFile)
                        Counter = Counter + 1
                    Next I
                End If
            End If


...................................
i have replaced the 65-77 lines of my previous code with this.
really thanks a lot for ur patience and help.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: To extract .doc and Pdf files from outlook attachments

Posted 02 July 2012 - 07:21 AM

If System.IO.Path.GetExtension(.doc) Then


You can't just make up the syntax for a coding language until you guess the right way to use it. Look it up on the MSDN
http://msdn.microsof...textension.aspx
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1