Office関連

閲覧(プレビュー)ウィンドウの選択文字列を取得するOutlookマクロ

Microsoft Communityにあった質問「Outlookの閲覧ウインドウのメールの一部(選択状態のもの)をVBAで取得する方法」への回答として書いたコードです。

※ 下記コードは32ビット版のOffice 2010で検証を行いました。
※ 下記コードはVBEから実行すると上手く動作しません。

Option Explicit

Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Sub Sample()
  Dim s As String
  
  s = GetSelectedTextFromReadingPane()
  If Len(Trim(s)) > 0 Then MsgBox s
End Sub

Private Function GetSelectedTextFromReadingPane() As String
'閲覧(プレビュー)ウィンドウの選択文字列を取得
  Dim h As Long
  Dim clsName As String
  Dim clsBuf As String * 255
  Dim winName As String
  Dim winBuf As String * 255
  Dim ret As String
  
  ret = "" '初期化
  With Application.ActiveExplorer
    If .IsPaneVisible(olPreview) = True Then
      h = GetFocus()
      GetClassName h, clsBuf, Len(clsBuf)
      clsName = Left$(clsBuf, InStr(clsBuf, vbNullChar) - 1)
      GetWindowText h, winBuf, Len(winBuf)
      winName = Left$(winBuf, InStr(winBuf, vbNullChar) - 1)
      If clsName = "_WwG" And winName = "メッセージ" Then
        '"コピー"の有効・無効判別
        If .CommandBars.FindControl(ID:=19).Enabled = True Then
          .CommandBars.FindControl(ID:=19).Execute
          ret = CreateObject("htmlfile").parentWindow.clipboardData.getData("text") 'クリップボードから文字列取得
        End If
      End If
    End If
  End With
  GetSelectedTextFromReadingPane = ret
End Function

Outlookのオブジェクトを眺めてみても、閲覧ウィンドウから直接選択文字列を取得できるようなメソッドやプロパティは見つけられなかったので、上記コードのような方法をとることにしました。

後になって調べたところ、上記コードと似たようなコードが書かれた質問を見つけました。

・How do I get the selected Text from the reading pane in Outlook 2003?
http://social.msdn.microsoft.com/Forums/vstudio/en-us/7f40c0d0-203b-44fb-ad8a-ce134f793563/

Selected text – Outlook’s reading pane」にも書かれていましたが、Outlookのオブジェクトモデルには、閲覧ウィンドウから選択文字列を取得できるようなものは用意されていないのかもしれません。

Office 2013のコントロールIDリストが更新されました。前のページ

[Google Apps Script]Google アナリティクスのデータを取得する。次のページ

関連記事

  1. Office関連

    [Outlook VBA]Outlookオブジェクトモデルとして公開されていないプロパティにアクセス…

    前回の記事で久しぶりにOutlookのマクロを触りましたが、ついでに昔…

  2. Office関連

    ExcelのWebクエリからのアクセス情報

    mougに面白い質問がありました。・Querytables.a…

  3. Office関連

    Word 2013の「個人用テンプレート」はどこ?

    Word 2010では、から「個人用テンプレート」(カスタム テンプレ…

  4. Office関連

    「傍点をふる」をWord 2007/2010で簡単に使う方法

    Wordで文字列を強調したいときに便利なのが「傍点をふる」コマンド。…

  5. Office関連

    Instagram APIをVBAから呼び出してみる。

    最近画像共有系のSNS、Instagram(インスタグラム)を使い始め…

  6. Office関連

    文字列を横方向に移動するWordマクロ(WordBasic編)

    いつもお世話になっているWord MVPの新田さんが、まるでカニの動き…

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP