Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

332
Visualizações
Macro not working when outlook is not open

Can anybody tell me why this macro only works if outlook is open? It works fine as long as i have the outlook app running, and my attempts to open the app before running this macro have failed. Specifically, it fails at 'add.attachemnt.pdf'.

Sub sendEmail()
        
        ' this sends an email
          
        Application.DisplayAlerts = False
        
          Dim IsCreated As Boolean
          Dim i As Long
          Dim PdfFile As String, Title As String
          Dim OutlApp As Object
         
         
         Worksheets("REPORT").Visible = True
        
         Worksheets("REPORT").Select
        
          Title = Range("AH5")
         
          ' Define PDF filename
          PdfFile = Title
          i = InStrRev(PdfFile, ".")
          If i > 1 Then PdfFile = Left(PdfFile, i - 1)
          PdfFile = PdfFile & ".pdf"
         
          ' Export activesheet as PDF
          With ActiveSheet
            .ExportAsFixedFormat Type:=xlTypePDF, FileNAME:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
          End With
         
           'Use already open Outlook if possible
          On Error Resume Next
          Set OutlApp = GetObject(, "Outlook.Application")
          If Err Then
            Set OutlApp = CreateObject("Outlook.Application")
            IsCreated = True
         End If
         OutlApp.Visible = True
          On Error GoTo 0
         
          ' Prepare e-mail with PDF attachment
          With OutlApp.CreateItem(0)
           
            ' Prepare e-mail
            .Subject = Title
            .To = " emails "
            
            
        .HTMLBody = "Key Indicators are as follows: <br><br>" _
             
          
            .Attachments.Add PdfFile
           
    ' Try to send
        On Error Resume Next
        .Display
        Application.Visible = True
        On Error GoTo 0
       
      End With
     
      ' Delete PDF file
      Kill PdfFile
     
      ' Quit Outlook if it was created by this code
      If IsCreated Then OutlApp.Quit
     
      ' Release the memory of object variable
      Set OutlApp = Nothing
      
    Worksheets("REPORT").Visible = xlSheetHidden
    
End Sub
about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda