title: Run Executable File to Get Outlook Attachments
body: <p>I've written Python code that downloads Outlook attachments from ABC@XYZ.com and saves them in particular_folder. It works great when I run it in Spyder, but when I convert it to an executable and run it, the attachments aren't downloaded, though no errors are thrown. I'm very confused about what the problem could be.</p>
  <pre><code>import win32com.client as win
  import datetime as dt
  
  
  # Set up connection to outlook
  outlook = win.Dispatch(&quot;Outlook.Application&quot;).GetNamespace(&quot;MAPI&quot;)
  
  # Go to the inbox folder
  inbox = outlook.GetDefaultFolder(6)
  
  # Record the messages
  messages = inbox.Items
  
  # Get the first message
  message = messages.GetLast()
  
  # Record today's date
  today = dt.date.today()
  
  # Initialize email date to today
  email_date = today
  
  # Get yesterday's date
  yesterday = today - dt.timedelta(days = 1)
  
  # Initialize count at 0
  count = 0
  
  # Record the number of emails read
  emails_read = 0
  
  # Record the number of tries
  tries = 0
  
  # Record the number of excepts
  excepts = 0
  
  # Was the break reached?
  break_reached = 0
  
  # While the count is less than three
  while count &lt; 4:
  
    emails_read += 1
    
    try:
  
        # Record the email address
        email_address = message.SenderEmailAddress
        
        # Record the date when the message was sent
        email_date = message.senton
          
        # Convert the date to a dt.date object
        email_date = dt.date(year = email_date.year, month = email_date.month, day = email_date.day)
          
        # If the email is from ABC@XYZ.com and the email was send yesterday... 
        if (email_address == 'ABC@XYZ.com') &amp; ((email_date == yesterday)|(email_date == today)):
              
            # Record the attachment  
            attachments = message.Attachments
                
            # Get the first attachment
            attachment = attachments.Item(1)
                
            # Get the name of the attachment
            attachment_name = str(attachment)
                
            # Save the attachment in my ICE folder
            attachment.SaveASFile(particular_location + attachment_name)
                
            # Add 1 to the count
            count += 1
                
        # Move on to the next message
        message = messages.GetPrevious()
        
        tries += 1
         
    # ... when not possible...
    except:
          
        # ... move on to the next message            
        message = messages.GetPrevious()
        
        excepts += 1
            
      
    if (emails_read &gt; 1e4)|(email_date == dt.date.today() - dt.timedelta(days = 2)):
        
        break_reached += 1
        
        break
    
  # Create a log dictionary
  log_dict = {'count':count, 'emails_read':emails_read, 'tries':tries, 'excepts':excepts, 
              'break_reached':break_reached, 'email_date':email_date.strftime(&quot;%m-%d-%y&quot;)}
  
     
  # Record the hyperparameters from the cross-validation
  with open(other_location + 'log.txt', 'w') as f: 
      
      # Look over the dictionary of best hyperparameters
      for key, value in log_dict.items(): 
          
          # Print key: value (space)
          f.write('%s:%s\n' % (key, value))
  </code></pre>
  <p>I've added a log to the code which is shown above. The log reads:</p>
  <blockquote>
  <p>count:0 emails_read:10001 tries:0 excepts:10001 break_reached:1
  email_date:03-30-22</p>
  </blockquote>
  <p>So, it turns out I'm always getting an error in the try case and it goes to the except.</p>