2017-04-07 109 views
1

我想創建一個將特定範圍複製到Outlook(帶圖像)的宏。但它不工作就像我想要它...通過MailEnvelope發送電子郵件(VBA,Excel)

Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope() 
'Working in Excel 2002-2016 
Dim AWorksheet As Worksheet 
Dim Sendrng As Range 
Dim rng As Range 

On Error GoTo StopMacro 

With Application 
    .ScreenUpdating = False 
    .EnableEvents = False 
End With 

'Fill in the Worksheet/range you want to mail 
'Note: if you use one cell it will send the whole worksheet 
Set Sendrng = Worksheets("EMAIL").Range("B2:W41") 

'Remember the activesheet 
Set AWorksheet = ActiveSheet 

With Sendrng 

    ' Select the worksheet with the range you want to send 
    .Parent.Select 

    'Remember the ActiveCell on that worksheet 
    Set rng = ActiveCell 

    'Select the range you want to mail 
    .Select 

    ' Create the mail and send it 
    ActiveWorkbook.EnvelopeVisible = True 
    With .Parent.MailEnvelope 

     ' Set the optional introduction field thats adds 
     ' some header text to the email body. 
     .Introduction = "This is test mail 2." 

     With .Item 
      .To = ThisWorkbook.Sheets("EMAIL").Range("Z1").Value 
      .CC = ThisWorkbook.Sheets("EMAIL").Range("Z1").Value 
      .BCC = "" 
      .Subject = ThisWorkbook.Sheets("EMAIL").Range("D1").Value 
      .Display 
     End With 

    End With 

    'select the original ActiveCell 
    rng.Select 
End With 

'Activate the sheet that was active before you run the macro 
AWorksheet.Select 

StopMacro: 
With Application 
    .ScreenUpdating = True 
    .EnableEvents = True 
End With 
ActiveWorkbook.EnvelopeVisible = False 

End Sub 

有人可以告訴我爲什麼MailEnvelope只顯示1秒,事後沒有任何反應?似乎它不適合我。

我用.display替換了.send,但仍然沒有任何變化。另外我嘗試使用RNGtoHTML代碼,但是這段代碼不會複製圖像(我在表單「EMAIL」中有動態鏈接圖片)。

回答

0

你的問題是,你使用

。顯示

,但你不等待發送,然後關閉信封,而不發送: ActiveWorkbook.EnvelopeVisible =假

  • 只是評論它和對話框將留在那裏供您發送。

如果你需要使用。發送,或。顯示不能在所有的工作:

  • 在管理員模式下運行Outlook解決它
  • 改變微軟的Outlook設置程序化進入*「 (使用時。發送顯示煩人的對話框)如果警告我。」解決它那種對我來說
  • 把MSGBOX Err.description在結束調試你的錯誤

    MSGBOX Err.descriptio n End Sub

  • 仍試圖找出實際的解決方案

郵件發送宏爲我工作,但之後停止。不知道爲什麼。我甚至試圖添加一些註冊表項(https://social.technet.microsoft.com/Forums/ie/en-US/e2c89fec-beb3-4224-a6cb-112704406907/cannot-change-programmatic-access-security?forum=outlook)。但那並不奏效。

*什麼是最搞笑的是,如果你禁用編程方式訪問微軟的Outlook警告(https://www.slipstick.com/developer/change-programmatic-access-options/) - 在管理啓動MS O和去那個設置 - 它停止顯示warnig也停止請求和功能。這幾天通常的微軟行爲......

相關問題