title: Open password protected pdf in Word using excel VBA
body: <p>I have a macro that opens a PDF in Word but the PDF is password protected. I also have VBA code to enter the password in the dialog box that pops up but the code stops before the code to enter the password. If I enter the password manually it continues but I would like the code to entered automatically using the code I have.</p>
  
  <pre><code>    Option Explicit
  
      #If VBA7 And Win64 Then
      Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
      Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
      Public Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
      Public Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
      Public Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
     #Else
      Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
      Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
      Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpClassName As String, ByVal lpWindowName As String) As Long
      Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
      Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  #End If
  Public Const SW_HIDE As Long = 0
  Public Const SW_SHOWNORMAL As Long = 1
  Public Const SW_SHOWMAXIMIZED As Long = 3
  Public Const SW_SHOWMINIMIZED As Long = 2
  Public Const WM_SETTEXT = &amp;HC
  Public Const VK_RETURN = &amp;HD
  Public Const WM_KEYDOWN = &amp;H100
  Public Const WM_KEYUP = &amp;H101
  
  Public Sub OpenLockedPdf() 'Password As String)
  
      #If VBA7 And Win64 Then
          Dim parentWindow            As LongPtr
          Dim firstChildWindow        As LongPtr
          Dim secondChildFirstWindow  As LongPtr
          Dim secondChildSecondWindow  As LongPtr
      #Else
          Dim parentWindow            As Long
          Dim firstChildWindow        As Long
          Dim secondChildFirstWindow  As Long
          Dim secondChildSecondWindow  As Long
      #End If
      Dim timeCount                   As Date
      'Find the handle of the pop-up window.
  
      Dim Word As New Word.Application
      Dim WordDoc As New Word.Document
      Dim Doc_Path As String
      Dim Password As String
      Doc_Path = "Examplepdf.PDF"
      Set WordDoc = Word.Documents.Open(Filename:=Doc_Path, Format:="PDF Files", ConfirmConversions:=False)
      Password = "Password"
      timeCount = Now()
      Do Until Now() &gt; timeCount + TimeValue("00:00:05")
          parentWindow = 0
          DoEvents
          parentWindow = FindWindow("NUIDialog", "Password")
          If parentWindow &lt;&gt; 0 Then Exit Do
      Loop
      If parentWindow &lt;&gt; 0 Then
  
          'Find the handle of the first child window (it is a group box).
          timeCount = Now()
          Do Until Now() &gt; timeCount + TimeValue("00:00:05")
              firstChildWindow = 0
              DoEvents
              firstChildWindow = FindWindowEx(parentWindow, ByVal 0&amp;, "NetUIHWND", vbNullString)
              If firstChildWindow &lt;&gt; 0 Then Exit Do
          Loop
          If firstChildWindow &lt;&gt; 0 Then
              timeCount = Now()
              Do Until Now() &gt; timeCount + TimeValue("00:00:05")
                  secondChildFirstWindow = 0
                  DoEvents
                  secondChildFirstWindow = FindWindowEx(firstChildWindow, ByVal 0&amp;, "NetUICtrlNotifySink", vbNullString)
                  If secondChildFirstWindow &lt;&gt; 0 Then Exit Do
              Loop
              If secondChildFirstWindow &lt;&gt; 0 Then
                  timeCount = Now()
                  Do Until Now() &gt; timeCount + TimeValue("00:00:05")
                      secondChildSecondWindow = 0
                      DoEvents
                      secondChildSecondWindow = FindWindowEx(secondChildFirstWindow, ByVal 0&amp;, "RICHEDIT60W", vbNullString)
                      If secondChildSecondWindow &lt;&gt; 0 Then Exit Do
                  Loop
                  If secondChildSecondWindow &lt;&gt; 0 Then
                      'Fill the password in the text box.
                      SendMessage secondChildSecondWindow, WM_SETTEXT, 0&amp;, ByVal Password
                      'Press the OK button (it is the default action, so no need to find the handle of the button).
                      PostMessage secondChildSecondWindow, WM_KEYDOWN, VK_RETURN, 0
                      PostMessage secondChildSecondWindow, WM_KEYUP, VK_RETURN, 0
                  End If
              End If
          End If
      End If
  
  End Sub
  </code></pre>