0

I work in bank and we have a lot of restrictions. I can't use the Developer option in Outlook. I can use Excel VBA.

I would like to automate saving a zip file, which is received everyday, in a local drive folder and automatically unzip it and replace the yesterday's file.

I would like to create a button in an Excel sheet. Once I press the button the attachment in Outlook should save in a local folder in whatever destination I want and the attachment should unzip.

I have tried some things saving attachments from Outlook by using VBA, but it doesn't help much.

karel
  • 5,489
  • 46
  • 45
  • 50
Uday
  • 1
  • 2
  • Have you tried anything like this? If your tech people can stop you using the Developer tab then they can probably stop you accessing your emails with a workbook macro. If you can access your emails from a workbook, you do not need a button. This workbook will do nothing else but save the zip file so you can use Auto-Execute. You open the workbook and it immediately does whatever you want. – Tony Dallimore Oct 22 '19 at 09:32
  • Missing from your question is information on how you recognise the email containing the zip file attachment. Also missing is how to identify the output folder. Is it fixed? Is it, for example, based on the date? Do you want to specify a different folder each time you run the macro? If the output folder is fixed, does each zip file have a unique name? If not, do you mind overwriting a previous zip file? – Tony Dallimore Oct 22 '19 at 09:33
  • Hi tony, Thanks for writing. I can't really clarify about the restrictions since it is a bank they have lot of ruel taht need to follow. this ristriction is not just for me or our department. it's for whole bank. – Uday Oct 22 '19 at 09:48
  • So, tony I tried this " by using a code save attachment in the folder and use run a script in the out look" but because of the bank it ristrictions the option run the scrip is not available in the rule wizard. comming to the point the mail i recive everyday is aclled "sanctions" it contains zip file (This is how i recgnise and this email comes with a zip file everyday). next quetion: No, the zip file has tobe unziped and replace or overwrite the old or previous zipfile. Zip file has same name forever. no, the file saving folder should fime constant or same forever. – Uday Oct 22 '19 at 09:58
  • Thanks for bringing this questions up. In total, A zip file from same email and same file name from outlook need to be saved in one particular folder forever and need to be unziped by clicking a button everyday. this is the email subject (ISIN List: Financial Sanctions - ISIN screening). The zip file name is: GSM_1_004_EU_RU_NU_US_INSTR_FULL_... date...ZIP File 667KB. – Uday Oct 22 '19 at 10:05
  • https://stackoverflow.com/a/42480900/4539709 – 0m3r Oct 22 '19 at 18:13

1 Answers1

1

I am not surprised a bank doesn’t want its emails accessed. You could change the sender, add or remove recipients or change the text. It is difficult to do any of these without leaving a trail but it is possible. You do not want to change anything; you just want to automate saving an attachment so this might be allowed by your tech people and Outlook.

Before attempting the more complicated parts of your requirement, let us check your requirement is possible. I do not know how much you know about Excel VBA. If I ask you to do something you do not understand, come back with questions.

Create a macro-enabled workbook somewhere convenient. The name of the workbook does not matter.

Open the workbook and then the VBA Editor.

Click [Tools] and then [References]. You will get a drop-down menu of all the available libraries. Scroll down until you find “Microsoft Outlook nn.0 Object Library”. “nn” identifies the version of Outlook in use which I understand will be “14” for you. Click the box to the left and a tick will appear. Click [OK]. This will give you access to Outlook from Excel.

In the Project Explorer, you will see something like:

-  VBAProject (YourNameForWorkbook.xlsm)
  -  Microsoft Excel Objects
       Sheet1 (Sheet1)
       ThisWorkbook  

If either of the minuses is a plus, click that plus.

Click [ThisWorkbook]. An empty code area will appear on the right of the VBA Editor window. Copy the code below to this area.

Within the code you will find lines starting ‘###. These lines tell you about changes you must make or things you must check. Make the necessary changes and then save and close the workbook. Reopen the workbook. With good fortune, the macro will run automatically and the default worksheet will report what it has done. It will probably have found the wrong email and saved the wrong attachment. This does not matter. If you can save any attachment, you can save the attachment you want.

Option Explicit
Sub Workbook_Open()

  '### Replace "C:\DataArea\SO\" with the name of a disc folder on your system
  '    Make sure your folder name ends with \.
  Const DiscFldrDest As String = "C:\DataArea\SO\"
  '### The name of the default worksheet depend on the local language. Replace
  '    "Sheet1" is this is not the default name for you.
  Const WshtOutName As String = "Sheet1"
  ' ### The subject of the email. Correct if I have misunderstood your comment    ' ###
  Const Subject As String = "ISIN List: Financial Sanctions - ISIN screening"     ' ###

  Dim AppOut As Outlook.Application
  Dim Found As Boolean
  Dim InxA As Long
  Dim InxI As Long
  Dim OutFldrInbox As Outlook.Folder
  Dim RowNext As Long
  Dim WshtOut As Worksheet

  Set AppOut = CreateObject("Outlook.Application")

  With AppOut
    With .Session
      Set OutFldrInbox = .GetDefaultFolder(olFolderInbox)
    End With
  End With

  Set WshtOut = Worksheets(WshtOutName)
  RowNext = WshtOut.Cells(Rows.Count, "A").End(xlUp).Row + 1

  '### Change if you prefer different date or time formats
  WshtOut.Cells(RowNext, "A").Value = "Macro activated at " & _
                                      Format(Now(), "h:mm") & " on " & _
                                      Format(Now(), "d mmm yy")
  RowNext = RowNext + 1

  '### GetDefaultFolder is not much use on my system because I have two
  '    email addresses, each with their own Inbox, neither of which is
  '    the default Inbox.  Probably you only have one work email address
  '    which is the default for you.  To check, the following statement
  '    outputs the name of the default Inbox's mailbox.  Tell me if it is
  '    not the mail box you want.
  WshtOut.Cells(RowNext, "A").Value = Space(6) & "Inbox accessed"
  WshtOut.Cells(RowNext, "B").Value = OutFldrInbox.Parent.Name
  RowNext = RowNext + 1

  Found = False

  With OutFldrInbox
    For InxI = .Items.Count To 1 Step -1
      With .Items(InxI)
        If .Subject = Subject And .Attachments.Count > 0 Then                            '###
          WshtOut.Cells(RowNext, "A").Value = Space(6) & "Attachment saved from email"   '###
          WshtOut.Cells(RowNext, "B").Value = "With subject"
          WshtOut.Cells(RowNext, "C").Value = .Subject
          RowNext = RowNext + 1
          WshtOut.Cells(RowNext, "B").Value = "Received"
          'WshtOut.Cells(RowNext, "C").Value = .ReceivedTime
          WshtOut.Cells(RowNext, "C").Value = Format(.ReceivedTime, "\a\t h:mm \o\n d mmm yy")
          'WshtOut.Cells(RowNext, "C").NumberFormat = "at h:mm on d mmm yy"
          RowNext = RowNext + 1
          WshtOut.Cells(RowNext, "A").Value = Space(6) & "Attachment saved"             '###
          For InxA = 1 To .Attachments.Count                                            '###
            If UCase(Right$(.Attachments(InxA), 4)) = ".ZIP" Then                       '###
              WshtOut.Cells(RowNext, "B").Value = .Attachments(InxA).Filename           '###
              .Attachments(1).SaveAsFile DiscFldrDest & .Attachments(1).Filename        '###
              Found = True                                                              '###
              Exit For                                                                  '###
            End If                                                                      '###
          Next                                                                          '###
        End If
      End With
    Next

    With WshtOut
      If Not Found Then
        .Cells(RowNext, "B").Value = "No email with correct subject and a ZIP attachment found"
        RowNext = RowNext + 1
      End If
      .Columns.AutoFit
      .Cells(RowNext, "A").Select
    End With

  End With

End Sub
Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • Hi Tony, Thanks for the code. it is actually working and I appreciate. the code did extracted and saved a attachment in the location where I wanted. now how to ebit or alter the exact or specific email address attachment I want to save? Our next step from here should be "to extract attachment from specific email and unzip it and it should over write every day I run the Macro. greetings from Germany. – Uday Oct 23 '19 at 10:03
  • I have updated my code to look for a specific subject and an attachment with an extension of "ZIP". New or amended lines end with '###. I suspect it will be easier to replace my original code and then redo the necessary changes but your choice. Your comments implies the zip file name changes each day. Providing there is only one zip file attachment within the email, my current test will be adequate. If there are two or more zip files, please tell me more about the zip file names and I will create a more specific test. – Tony Dallimore Oct 23 '19 at 11:17
  • You would like to automatically unzip the zip file. I use WinZip. To call WinZip from a macro or program, I have downloaded the free Command Line Interface for WinZip. Do you use WinZip? Can you download the Command Line Interface? If not, do you think your tech people would download it for you? – Tony Dallimore Oct 23 '19 at 11:28
  • @UdaySimhaReddyPucha If we can unzip your file, I will need to know if the zip file is password protected? I do not want to know the password; just if there is one. With the CLI, including "-sxxx" in the command string cause WinZip to use password "xxx" for the unzip. – Tony Dallimore Oct 23 '19 at 11:32
  • The system has pre installed 7-Zip. – Uday Oct 23 '19 at 12:20
  • When I run the macro it says "No email with correct subject and a ZIP attchment found. – Uday Oct 23 '19 at 12:30
  • Check I have specified the Subject correctly. I have used an exact match so every character must be correct including the case. If the subject is not fixed I could, for example, check the first 20 characters of the subject. If the subject is fixed, it might be easiest to copy the subject from one of these emails to the top of my macro. This would help if the problem is characters that look the same but are not. If a corrected subject does not work, I will give you some diagnostic code which will help identify why one of these emails is being ignored. – Tony Dallimore Oct 23 '19 at 12:58
  • @UdaySimhaReddyPucha. 7za.exe is the CLI version of 7-Zip. Your tech people may have downloaded it at the same time they downloaded the Windows' version. If not, they might be willing to download it for you. You are only trying to automate something you can already do so I do not see why they would refuse. – Tony Dallimore Oct 23 '19 at 13:04
  • So, the version is jsut 7-Zip (in the copyrights it says 7-zip is free software nothing else). – Uday Oct 23 '19 at 13:17
  • About the subject: Yeah true every character of the subject you input in the code is same as my inbox mail subject. I dint know why error is occurring. Can you check it by running in your system? sample with a different subject from one of your eamil which might has a zip file? do you know what i mean. – Uday Oct 23 '19 at 13:22
  • I sent myself an email with that subject and a zip file attachment. My macro found that email and extracted the zip file. I will add some diagnostics so we can see what is happening on your system. – Tony Dallimore Oct 23 '19 at 13:43
  • A program can normally perform lots of different tasks so it needs to be told what tasks to perform today. Options include: asking the user via a Windows interface, a command line interface and a text file of parameters. 7-Zip offers all these different options but not in the same program. One program provides the Windows interface while another program provides the CLI and the text file options. These different programs are part of one copyrighted package. A macro cannot use the Windows interface; it can only use the CLI interface. – Tony Dallimore Oct 23 '19 at 13:44
  • @UdaySimhaReddyPucha If you look in “C:\Program Files”, you will probably find a folder named “7-Zip” or similar. Failing that, try “C:\Program Files (x86)”. What programs are listed in this folder? – Tony Dallimore Oct 23 '19 at 13:44
  • If i understood the last comment correct, there are these kind of files inside “C:\Program Files (x86)”: Adobe, Aanvil,Bloomberg, IBM, Intel, and microsoft related Silverlight, SQL sever compact edition, visual studio.net 2003 and some other company related files – Uday Oct 23 '19 at 14:00
  • Can I send you an email? – Uday Oct 23 '19 at 14:01
  • I do not know the difference between “C:\Program Files” and “C:\Program Files (x86)”. I think it something to do with the type of processor. The folders within these folders identify the company that provided their contents. So "...\Adobe\ will include all the programs and files provided by Adobe. The 7-Zip programs will be somewhere within “C:\Program Files” or “C:\Program Files (x86)” in a folder with a name such as "7Zip" or "7-Zip". – Tony Dallimore Oct 23 '19 at 15:15
  • Hello Tony, I sent you an email please cheeck. – Uday Oct 24 '19 at 08:07
  • Can you send me the excel sheet you ran your code yesterday? – Uday Oct 24 '19 at 08:26
  • I did run macro today after doing some changes it worked but the file did not unzip when I ran the code. – Uday Oct 24 '19 at 14:43
  • actually in the zip file name "GSM_1_004_EU-RU-UN-US_INSTR_FULL_20191023.zip" there is a date 2019 10 23 in the end. Which means we recive the zip file with same name everyday except that the date is changed to the day we receive. – Uday Oct 25 '19 at 07:56
  • @Uday Do you receive these files every day including weekends and holidays or only on weekdays? Are the files dated for the previous [working] day or does it take two or three days to accumulate the data for a file? – Tony Dallimore Oct 25 '19 at 13:29
  • Only weekdays and the day we receive the mail is the date – Uday Oct 26 '19 at 09:23