LoginSignup
3
11

More than 3 years have passed since last update.

退屈なことは PowerShell にやらせよう (Microsoft Office 編)

Last updated at Posted at 2020-04-20

Microsoft Word

Word の文字列を取得する

Characters プロパティは N 文字目、Words プロパティは N 語目、Sentences プロパティは N 行目を指定する。

get_word.ps1
function GetWordContent {
    $file_path = "C:\Users\user\Desktop\test.docx"

    # アプリケーションを開始する
    $app = New-Object -ComObject Word.Application
    $app.Visible = $true

    # Word を開く
    $doc = $app.Documents.Open($file_path)

    # 文字列を取得する
    $var_char = $doc.Characters(1).Text
    $var_word = $doc.Words(1).Text
    $var_sentence = $doc.Sentences(1).Text

    # Word を閉じる
    $doc.Close()

    # アプリケーションを終了する
    $app.Quit()

    Write-Output -InputObject "var_char: $var_char"
    Write-Output -InputObject "var_word: $var_word"
    Write-Output -InputObject "var_sentence: $var_sentence"
}

GetWordContent

Word に文字列を挿入する

insert_word.ps1
function InsertWordContent {
    $file_path = "C:\Users\user\Desktop\test.docx"

    # アプリケーションを開始する
    $app = New-Object -ComObject Word.Application
    $app.Visible = $true

    # Word を開く
    $doc = $app.Documents.Open($file_path)

    # 文章を挿入する
    $doc.Content.InsertBefore("Before")
    $doc.Content.InsertAfter("After")

    # Word を保存する
    $doc.Save()

    # Word を閉じる
    $doc.Close()

    # アプリケーションを終了する
    $app.Quit()
}

InsertWordContent

Word の文字列を置換する

replace_word.ps1
function ReplaceWordContent {
    $file_path = "C:\Users\user\Desktop\test.docx"

    # アプリケーションを開始する
    $app = New-Object -ComObject Word.Application
    $app.Visible = $true

    # Word を開く
    $doc = $app.Documents.Open($file_path)

    # 文字列を置換する
    $doc.Characters(1).Text = "A"
    $doc.Words(2).Text = "DEF"
    $doc.Sentences(2).Text = $doc.Sentences(1).Text

    # Word を保存する
    $doc.Save()

    # Word を閉じる
    $doc.Close()

    # アプリケーションを終了する
    $app.Quit()
}

ReplaceWordContent

Microsoft Outlook

Outlook でメールを作成する

create_mail.ps1
function CreateMail {
    # アプリケーションを開始する
    $app = New-Object -ComObject Outlook.Application

    # メールを作成する
    $item = $app.CreateItem(0)

    # メールの設定を入力する
    $item.To = "makoto@example.com"
    $item.CC = "sekai@example.com"
    $item.Subject = ""
    $item.BodyFormat = 1 # olFormatPlain
    $item.Body = "ごめん`n`n`n`n`nさよなら"
    #$item.Attachments.Add("C:\Users\user\Desktop\attachment.txt")
    $item.Display()

    # メールを保存する
    $item.Save()

    # アプリケーションを終了する
    # $app.Quit()

    # MailItem object
    # https://docs.microsoft.com/ja-jp/office/vba/api/outlook.mailitem

    # OlBodyFormat enumeration
    # https://docs.microsoft.com/ja-jp/office/vba/api/outlook.olbodyformat
}

CreateMail

Outlook のメールを取得する

get_mail.ps1
function GetMail {
    # アプリケーションを開始する
    $app = New-Object -ComObject Outlook.Application
    $namespace = $app.GetNamespace("MAPI")

    # 受信フォルダのメールを取得する
    $folder = $namespace.GetDefaultFolder(olFolderInbox)
    $items = $folder.Items

    # アプリケーションを終了する
    # $app.Quit()

    # 取得した情報の出力
    foreach ($item in $items) {
        Write-Output -InputObject $item.SentOn
        Write-Output -InputObject $item.Subject
        Write-Output -InputObject $item.SenderName
        Write-Output -InputObject $item.Sender.Address
        Write-Output -InputObject $item.To
        Write-Output -InputObject $item.CC
    }

    # MailItem object
    # https://docs.microsoft.com/ja-jp/office/vba/api/outlook.mailitem

    # OlDefaultFolders enumeration
    # https://docs.microsoft.com/ja-jp/office/vba/api/outlook.oldefaultfolders
}

GetMail

Microsoft Excel

Excel の値を取得する

get_excel.ps1
function GetExcelContent {
    # アプリケーションを開始する
    $app = New-Object -ComObject Excel.Application
    $app.Visible = $true

    # Excel を開く
    $book = $app.Workbooks.Open("c:\Users\user\Desktop\hoge.xlsx")

    # シートを開く
    $sheet = $book.Worksheets.Item("Sheet1")

    # 値を取得する
    $cell = $sheet.Cells.Item(1, 1).Text

    # Excel を保存する
    $book.Save()

    # Excel を終了する
    $book.Close()

    # アプリケーションを終了する
    $app.Quit()

    Write-Output -InputObject $cell
}

GetExcelContent

Internet Explorer (おまけ)

Web ブラウザを操作する

ie.ps1
function ManipulateIE {
    $uri = "https://www.google.co.jp/"

    # Web サイトへアクセス
    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.Visible = $true
    $ie.Navigate($uri)

    # ページが読み込まれるまでスリープする
    while ($ie.Busy) {
        Start-Sleep -Seconds 1
    }

    # 値を入力する
    $txt = $ie.Document.getElementsByName("q")
    $txt[0].value = "PowerShell"

    # ページが読み込まれるまでスリープする
    while ($ie.Busy) {
        Start-Sleep -Seconds 1
    }

    # ボタンを押す
    $btn = $ie.Document.getElementsByName("btnK")
    $btn[0].click()
}

ManipulateIE
3
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
11