Search code examples
vbscript

Launch Adobe Acrobat Reader and media file simultaneously using VBScript


On Windows 10, I would like to create a VBScript that launches Adobe Acrobat Reader in full-screen mode (to display a pdf), while simultaneously starting playback of a media file in the background. (The displayed file must be a pdf, so alternatives using PowerPoint or image files are not possible.) I have this script that does all of this but it gives me a pesky error despite all functionality. I tried to suppress the error message, but that made the whole thing crash. So, instead, I'm interested in solving the underlying error. Here's the script:

Set objShell = CreateObject("WScript.Shell")

acrobatPath = """C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"""
pdfFile = "C:\Users\X\Documents\Test.pdf"
command = acrobatPath & " " & pdfFile

objShell.Run command, 1, False

' Wait for Adobe Acrobat Reader to open
WScript.Sleep 1000

objShell.AppActivate("Adobe Acrobat")
WScript.Sleep 1000
' Open in full-screen mode
objShell.SendKeys("^l")

WScript.Sleep 1000 

' Start audio playback
Set objWMPlayer = CreateObject("WMPlayer.OCX.7")
objWMPlayer.settings.autoStart = True
objWMPlayer.settings.volume = 100
objWMPlayer.URL = "C:\Users\X\Documents\Test.wav"


Do While objAVDoc.GetAVDoc.GetView.ViewState = 1 ' 1 indicates that the PDF is open
    WScript.Sleep 100
Loop

The error I get is with the objAVDoc part, the error message says 'object required' (error code 800A01A8). (Note however, that despite the error, the script does what it is supposed to do, i.e. launches the presentation in full-screen mode and starts the playback.)

EDIT: In the above code, the objAVDoc is not instantiated. To fix this, I defined the object, but this runs into another problem (error 800A01AD), which stops the execution of the script.

Set objShell = CreateObject("WScript.Shell")

'path to pdf
pdfFile = "C:\Users\X\Documents\Test.pdf"
'path to sound file
soundFile = "C:\Users\X\Documents\Test.wav"

Set objAVDoc = CreateObject("AcroPDF.PDDoc")
Set objAVDoc.GetAVDoc = CreateObject("AcroPDF.AVDoc")

acrobatPath = """C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"""
command = acrobatPath & " " & pdfFile

objShell.Run command, 1, False

' Wait for Adobe Acrobat Reader to open
WScript.Sleep 1000

objShell.AppActivate("Adobe Acrobat")
WScript.Sleep 1000
' Open in full-screen mode
objShell.SendKeys("^l")

WScript.Sleep 1000 

' Start audio playback
Set objWMPlayer = CreateObject("WMPlayer.OCX.7")
objWMPlayer.settings.autoStart = True
objWMPlayer.settings.volume = 100
objWMPlayer.URL = soundFile

Do While objAVDoc.GetAVDoc.GetView.ViewState = 1 ' 1 indicates that the PDF is open
    WScript.Sleep 100
Loop

I also tried to create the objAVDoc object using

Set objAcroExch = CreateObject("AcroExch.App")
Set objAVDoc = objAcroExch.GetActiveDoc

but that also does not work.

Any other approach that achieves the desired outcome (the simultaneous start of the pdf in full-screen mode and the playback of a sound in the background) is acceptable.


Solution

  • Instead of trying to communicate with the Adobe Acrobat Reader, I used a different approach. I started both the Reader and then launched the media player. I also included a part that would stop the media playback in case the Acrobat Reader is no longer running. The following script does all of that:

    Set objShell = CreateObject("WScript.Shell")
    
    ' Path to Adobe Acrobat Reader executable
    acrobatPath = """C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"""
    pdfFile = "C:\Users\X\Documents\Test.pdf"
    command = acrobatPath & " /A ""pagemode=FullScreen"" " & pdfFile
    
    ' Start Adobe Acrobat Reader in full-screen mode with the specified PDF file
    objShell.Run command, 1, False
    
    ' Wait for Adobe Acrobat Reader to open in full-screen mode
    WScript.Sleep 1000
    
    ' Simulate Ctrl+L (full-screen mode hotkey)
    objShell.AppActivate("Adobe Acrobat")
    WScript.Sleep 1000
    objShell.SendKeys("^l")
    
    ' Start audio playback independently
    Set objWMPlayer = CreateObject("WMPlayer.OCX.7")
    objWMPlayer.settings.autoStart = True
    objWMPlayer.settings.volume = 100
    objWMPlayer.URL = "C:\Users\X\Documents\Test.wav"
    
    ' Allow time for both processes to initiate
    WScript.Sleep(1000) ' Adjust the sleep duration as needed
    
    ' Monitor if Adobe Acrobat Reader is running
    Do
        If Not IsProcessRunning("Acrobat.exe") Then
            ' Adobe Acrobat Reader is closed, so shut down media player
            objShell.Run "taskkill /f /im wmplayer.exe", 0, True
            Exit Do
        End If
    
        WScript.Sleep 1000
    Loop
    
    ' Function to check if a process is running
    Function IsProcessRunning(processName)
        Set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process")
        For Each objProcess In colProcesses
            If InStr(objProcess.Name, processName) > 0 Then
                IsProcessRunning = True
                Exit Function
            End If
        Next
        IsProcessRunning = False
    End Function