VBAでchromeを使ってgmailを添付ファイル付で下書作成する方法

Excelマクロ(VBA)

Google Workspaceを導入し、gmailを使用して業務をされている会社に勤められている方、多いのではないかと思います。

更に、セキュリティーポリシーの関係から、outlookなどのメールアプリの使用が禁止されている場合も、少なくないと思いますが、このような制約下において、メール作成の効率化を実現する方法として、VBAでメール新規作成する方法を紹介します。

業務効率化の参考になれば、幸いです。

マクロの初期設定

まず、マクロの初期設定が必要です。

Seleniumのインストールがまだの方は、まずは以下記事を参考に、 Seleniumを設定してください。

Gmailメール下書きを保存するVBAコード

高速化のため、簡易版HTMLのgmailサイトを利用して、メールの下書きを保存する仕様としています。

gmail/簡易版HTML

以下がマクロのコードです。

public driver As New ChromeDriver

Sub gmail起票()


  Dim Gto As String
  Dim Gcc As String
  Dim Gsu As String
  Dim Gbody As String
  Dim Gattachment(100) As String


  Gto = "XXXX@XXXX" '宛先
  Gcc = "XXXX@XXXX" 'CC
  Gsu = "XXXXXX" '件名
  Gbody = "XXXXXX" '本文
  Gattachment(1) = "XXXXXX" 'ファイルのパス
  Gattachment(2) = "XXXXXX" 'ファイルのパス
  PROFILE_PATH = "\\XXX\XXX" '上項で作ったユーザープロファイルフォルダのパス

    Set driver = Nothing
  driver.AddArgument ("user-data-dir=" & PROFILE_PATH)
    driver.Start "chrome"

    ' 新しいタブを開く
    driver.ExecuteScript "window.open()"
    ' タブを切り替える
    driver.SwitchToNextWindow
   

    driver.Get "https://mail.google.com/mail/u/0/h/1o77yokxyutgo/?&cs=b&pv=tl&v=b"
   
    driver.FindElementByCss("#to").SendKeys Gto  '宛先
    driver.FindElementByCss("#cc").SendKeys Gcc  'cc
   
    driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > form > table.compose > tbody > tr:nth-child(4) > td:nth-child(2) > input").SendKeys Gsu '件名
   
    driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > form > table.compose > tbody > tr:nth-child(8) > td:nth-child(2) > textarea").SendKeys Gbody  '本文


    If Gattachment(2) <> "" Then
    
        driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > form > table.compose > tbody > tr:nth-child(7) > td:nth-child(2) > input[type=submit]").Click
    
        For i = 1 To 10

            If Gattachment(i) <> "" Then

                driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > table:nth-child(3) > tbody > tr:nth-child(" & i & ") > td:nth-child(2) > input[type=file]").SendKeys Gattachment(i)  '添付
            
            Else
                Exit For
            End If
    
        Next i
        
        driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > input[type=submit]:nth-child(1)").Click

    Else
       If Gattachment(1) <> "" Then
       
                driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > form > table.compose > tbody > tr:nth-child(6) > td:nth-child(2) > input[type=file]").SendKeys Gattachment(1)  '添付
             
       End If
    End If


    driver.FindElementByCss("body > table:nth-child(16) > tbody > tr > td:nth-child(2) > table:nth-child(1) > tbody > tr > td:nth-child(2) > form > table:nth-child(6) > tbody > tr > td > input[type=submit]:nth-child(2)").Click '保存

    Set driver = Nothing




End Sub

簡易版HTMLは起動が早くて、このようなマクロに利用するのには適していると考えますが、一方で、宛先の設定時に、入力文字から、宛先を提案する機能がないとか、添付ファイルをドラッグアンドドロップで添付できないなど、不便な点もあります。

なので、下書きを保存したら、自動操作用のchromeは閉じてしまう仕様としています。

別の、作業用に開いているメインのchromeで、自動作成された下書きの編集を再開する想定です。

まとめ

いかがでしたでしょうか? 業務効率化の参考にしてみてください。

本記事のVBAマクロのサポートやカスタマイズについては、条件によって、有償にて対応受付しております。

以下、クラウドワークスのアカウントよりご依頼いただければ幸いです。

FIRE太郎さん(その他)に依頼・外注する | 簡単ネット発注なら【クラウドワークス】
日本最大級のクラウドソーシング「クラウドワークス」にご登録いただいているFIRE太郎さんのプロフィールです。その他のスキルを持つプロフェッショナルに1時間から仕事を気軽に発注!発注者は手数料無料。

その他、エクセルマクロの便利ツールは以下記事を参照ください。

コメント

タイトルとURLをコピーしました