vb.net 程式自動開啟 PowerPoint 並填入密碼

小舗有人問
  1. Findwindow 抓Powerpoint密碼對話框的問題
  2. FindWindow後PostMessage Enter的問題
其實把問題合在一起,就如圖
把程式逐一分解來看
1.取得 PPT 彈出密碼的表單 
<DllImport("user32.dll", SetLastError:=True)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    Dim hwd1 As IntPtr
    hwd1 = FindWindow(vbNullString, "密碼")
    If hwd1.Equals(IntPtr.Zero) Then MsgBox("未開啟PPT")
2.填入密碼並送出 Enter
<DllImport("user32.dll", SetLastError:=True)> Public Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function
    SetForegroundWindow(hwd1)
    SendKeys.Send("PWD")
    SendKeys.Send("{enter}")
----------------------------------------------
程式如下


Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("user32.dll", SetLastError:=True)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)> Public Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hwd1 As IntPtr
        hwd1 = FindWindow(vbNullString, "密碼")

        If hwd1.Equals(IntPtr.Zero) Then
            MsgBox("未開啟PPT")
        Else
            SetForegroundWindow(hwd1)
            SendKeys.Send("123")
            SendKeys.Send("{enter}")
        End If
    End Sub
End Class

沒有留言:

張貼留言