I'm trying to launch Acrobat Reader inside a child form of my application (specifically inside a frame on that form). The external program fires up OK but it's not inside my child form (which sits empty behind Acrobat Reader and has to be closed separately after I close Reader).
If I understood better the code I'm using (it was just cut and pasted from somewhere I forget!) I could sort this myself but I don't unfortunately. Anyone any ideas?
Here is the code I'm using:
VB Code:
Option Explicit
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, _
lpRect As RECT) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, _
ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private 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
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private lngGetApp As Long
Private Sub Form_Unload(Cancel As Integer)
DestroyWindow lngGetApp
End Sub
Private Sub Form_Load()
Dim RectCoord As RECT
Dim lr As Long
lr = ShellExecute(Frame1.hwnd, "Open", FileToLoad, "", "", vbNormalFocus)
If (lr < 0) Or (lr > 32) Then
' success '
Else
MsgBox "Failed to start '" & FileToLoad & "'", vbInformation
you can use the Acrobat Reader control (pdf.ocx) which you can drop on a VB Form and link up the source file and viola, a pdf file opened on your form. Different versions of Acrobat Reader come with different ocx controls so for 7/8 version of reader its adifferent control name.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
maybe this is a dumb idea, but acrobat has a plugin that runs within internet explorer. it might be possible to use a webbrowser control to run the plugin. maybe just using navigate2 ("test.pdf"). could work.
otherwise, youre likely looking at a lot of complicated subclassing...
I have it on both my systems and I only have Acrobat Reader which is free. You may not be able to distribute it but you can include the download link and installation of Reader from the installation of your app.
If you have Reader 5 or 6 then its pdf.ocx. If you have Reader 7 or 8 then its AcroPDF.dll
Add the control to your toolbox....
Project > Components ... > check "Adobe Acrobat 7.0 Control Type Library" > click OK.
(C:\...\Acrobat\ActiveX\AcroPDF.dll)
Then drag the pdf control from the toolbox and drop on your form, resize, and supply a pdf file/path to the src property.
OK, I've put this control on my form and specified the src property but the file won't load. I get the Reader error " File doesn't begin with '%PDF-' ".
Don't tell me - Adobe doesn't like spaces in the path? I'll test this now but if so this is yet another thing to hate about Adobe and pdf files - grr!
Paul Orton
VB6
Visual Web Developer 2008 Express Edition
Microsoft Visual Basic 2012 Express
Is the pdf file generated via some other creator and not Acrobat? If so then the pdf file may not be 100% compatible?[/color]
No, it was created by Acrobat 6. I think, given the difficulty I'm having, I will confine myself to just printing these files (the most important functionality I need by far).
Unless you can think of something else...
Paul Orton
VB6
Visual Web Developer 2008 Express Edition
Microsoft Visual Basic 2012 Express
Look at the properties of a pdf file that does not work. There is a PDF tab and under it is the PDF Producer property. What does that say it was created with?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Try this example with pdf attached. I wrote it from my xp sp-2 system with Reader 7.0
Thanks for taking all the trouble to do this.
I've run your code and get the same error message as my own code - "File does not begin with...etc".
What's even stranger is that the AcroPdf control disappears from the form each time I close down VB and I have to put it back on (it does this with your project as well as mine).
I used AcroPDF.dll from the ActiveX folder of my Reader 7 installation - is this the file you use on your system?
Paul Orton
VB6
Visual Web Developer 2008 Express Edition
Microsoft Visual Basic 2012 Express
Hmm, the code works and Iknow others using it. I really think there it is something particular about your system setup that may be the issue. I tested it with XP SP-2 andAcrobat Reader 7. And also, Vista RTM with Acrobat Reader 8 and both run the code example fine.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Option Explicit
Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Public Sub KillProcess(NameProcess As String)
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const TH32CS_SNAPPROCESS As Long = 2&
Dim uProcess As PROCESSENTRY32
Dim RProcessFound As Long
Dim hSnapshot As Long
Dim SzExename As String
Dim ExitCode As Long
Dim MyProcess As Long
Dim AppKill As Boolean
Dim AppCount As Integer
Dim i As Integer
Dim WinDirEnv As String
If NameProcess <> "" Then
AppCount = 0
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
RProcessFound = ProcessFirst(hSnapshot, uProcess)
Do
i = InStr(1, uProcess.szexeFile, Chr(0))
SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
WinDirEnv = Environ("Windir") + "\"
WinDirEnv = LCase$(WinDirEnv)
If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
AppCount = AppCount + 1
MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(MyProcess, ExitCode)
Call CloseHandle(MyProcess)
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound
Call CloseHandle(hSnapshot)
End If
End Sub
In a form:
Code:
Option Explicit
Private Sub Command1_Click()
KillProcess ("AcroRd32.exe")
Unload Me
End Sub
Private Sub Form_Load()
AcroPDF1.src = App.Path & "\Test.pdf"
End Sub