• Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
Charliechacha ,Everything about Dividend 關於股息率投資的方法

自學Excel VBA系列-如何用VBA控制OutLook?

2022 年 7 月 18 日
Home Lifestyle

內容目錄

  • 前言:
  • 顯示電子郵件視窗:
  • 操作Outlook的概念
  • 傳送電子郵件練習:
  • 傳送多封郵件練習:

前言:

上次講解過如何用VBA控制Word,希望大家都能實際運用,節省時間。

我相信很多朋友對於如何提升生產力,優化時間管理的效益有很大需求,那麼我們今天再來講解一下VBA對於Outlook的操作。

如果有讀過《自學Excel VBA系列-如何用VBA控制WORD?》,對於如何控制外部程式應該有一定的經驗,解說部分我們就相對簡短一點。

同樣,開始之前一定要在VBA裡面的工具–>設定引用項目–>勾選Microsoft Outlook 14.0 Object Library。

自學Excel VBA系列-如何用VBA控制OutLook

顯示電子郵件視窗:

Sub CreateMail()

‘outlook應用程式儲存在物件變數中
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

‘建立MailItem物件
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

‘預覽郵件
olMail.Display

MsgBox “已打開 OUTLOOK”

‘刪除物件變數參照
Set olMail = Nothing
Set olApp = Nothing

End Sub

這個程式碼主要是執行以下程序:

  1. 宣告olApp為Outlook.Application型的物件變數
  2. 使用new生成Outlook的物件,並儲存在olApp內
  3. 打開一封新郵件
  4. 彈出Msgbox

聽起來好像比Word要複雜一點,沒關係,我們一步一步慢慢來,先嘗試執行程式碼,應該可以打開一封新郵件。

操作Outlook的概念

自學Excel VBA系列-如何用VBA控制OutLook

之後若要操作Outlook應用程式,只要寫成 (變數名稱.屬性) 及 (變數名稱.方法)就可以了。

備註:至於這一句olApp.CreateItem(olMailItem),括號裡面的東西有點疑問,不放東西會有錯誤,隨便改個名字也能通過,後面也不會再用到這個變數。

傳送電子郵件練習:

  • 收件者:abc@excel.com
  • 主旨:你好
  • 內容:郵件內容
  • 格式:文字格式

Sub SendMail()

‘outlook應用程式儲存在物件變數中
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

‘建立MailItem物件
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

‘電子郵件資料
With olMail

   '收件者
   .To = "abc@excel.com"
   '主旨
   .Subject = "你好"
   '郵件內容
   .Body = "郵件內容"
   '郵件格式
   .BodyFormat = olFormatPlain '文字格式

End With

‘加入附件
olMail.Attachments.Add ThisWorkbook.Path & “\pic1.png” ‘要記得建立png檔。否則,直接刪除這一行

‘傳送電子郵件
olMail.Save ‘儲存草稿
olMail.Display ‘顯示預覽
olMail.Send ‘傳送

MsgBox “已傳送郵件”

‘刪除物件變數參照
Set olMail = Nothing
Set olApp = Nothing

End Sub

至此,剛才介紹過的屬性及方法已全部操作過。

另外,如果想要加入副本,就是olMail.Cc;設定密件副本,就是olMail.Bcc。

傳送多封郵件練習:

首先在工作表中建立清單以及郵件內容資料,

Sub SendMultiMail()

‘outlook應用程式儲存在物件變數中
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application

‘取得清單的最後一列
Dim maxRow As Long
maxRow = Worksheets(“清單”).Cells(Rows.Count, 1).End(xlUp).Row
‘MsgBox maxRow ‘測試用

‘回圈執行直至最後一列
Dim i As Long
For i = 2 To maxRow

‘建立MailItem物件
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

‘電子郵件資料
With olMail

  '收件者
  .To = Worksheets("清單").Cells(i, 3).Value
  '主旨
  .Subject = Worksheets("郵件內容").Range("B1").Value
  '郵件內容
  Dim str As String
  str = Worksheets("郵件內容").Range("B2").Value
  str = Replace(str, "[公司名稱]", Worksheets("清單").Cells(i, 1).Value)
  str = Replace(str, "[姓名]", Worksheets("清單").Cells(i, 2).Value)
  .Body = str

  '郵件格式
  .BodyFormat = olFormatPlain '文字格式

End With

‘加入附件
olMail.Attachments.Add ThisWorkbook.Path & “\pic1.png”

‘傳送電子郵件
olMail.Save ‘儲存草稿
‘olMail.Display ‘顯示預覽
‘olMail.Send ‘傳送

Set olMail = Nothing

Next i

‘刪除物件變數參照
Set olApp = Nothing

End Sub

為了便於測試,預覽和傳送都變成備註。實際操作下來,其實也沒有太複雜,海量傳送郵件的感覺還是很不錯的,有任何問題歡迎留言給我。

Tags: ExcelOUTLOOKVBA程式學習自學
Share219Tweet137Share55
Charlie chacha

Charlie chacha

Related Posts

自學Excel VBA系列-如何用VBA控制OutLook?
Lifestyle

Excel強大的組合函數練習,資料庫關鍵字搜索,Address + Match + Index + Find 組合拳

2023 年 3 月 18 日

想要把Excel的功能變得強大,函數組合使用必不可少,例如之前介紹的Offset函數,《一個強大的Excel函數OFFSET,動態選取資料範圍》,非常好用。本文介紹另外一個經常會使用到的組合拳:Add...

OpenAI 技术 GPT-3 语言生成模型 AI 研究 机器学习应用 人工智能技术 自然语言处理 数据分析 机器人技术 智能语音识别
Market

探索人工智能,了解OpenAI的未來與潛力–ChatGPT使用心得 ChatGPT可以幫到你做什麼?人工智能 語言生成 數據分析

2023 年 3 月 18 日

OpenAI ChatGPT ChatGPT的名號,大家都應該非常熟悉。地區破解什麼就不討論,兩個條件,地區VPN,以及SMS認證服務,還不能是虛擬號碼,我花了幾天時間也找不到方便安全的省錢方法,建議...

vba 自學
Programming

自學VBA系列,網頁抓取的按鈕選擇及下拉式選單問題

2022 年 12 月 29 日

網頁抓取的過程中經常會出現各種各樣的問題,今天就按鈕選擇及下拉式選單碰到的問題略作解釋。 按鈕選擇問題: Sub NewUpdatedata() 'Dim ur As String, b, ie As...

How to save money on Microsoft Office with legal and safe
Lifestyle

How to save money on Microsoft Office with legal and safe

2022 年 11 月 14 日

Once you buy a new computer or reinstall the Window, you would think about Office. As Microsoft poli...

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

About Me

Charliechacha ,Everything about Dividend 關於股息率投資的方法

Charlie Chacha

Blogger and Knowledge Seeker

Welcome to my blog! My name is Charlie chacha, I share practical knowledge, whick is different from theoretical. Focus on your life, how you spend your time . Learn hard , live well and make money while you sleep.

Categories

  • Finance (31)
  • Lifestyle (7)
  • Market (16)
  • Programming (12)
  • Reading (39)
  • Research (21)
  • Running (39)
  • Travel (2)

Popular

  • JAVA學習筆記 ArrayList

    一個強大的Excel函數OFFSET,動態選取資料範圍

    1113 shares
    Share 445 Tweet 278
  • Excel VBA的自學心得分享

    1026 shares
    Share 410 Tweet 257
  • 自學Excel VBA系列-如何用VBA控制WORD?

    611 shares
    Share 244 Tweet 153
  • 自學Excel VBA系列-如何用VBA控制OutLook?

    548 shares
    Share 219 Tweet 137
  • 日元貶值,日本央行為什麼不加息?

    458 shares
    Share 183 Tweet 115

Instagram

    Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to connect your Instagram account.
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me