-
[RESOLVED] Run another program inside a form
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
End If
lngGetApp = FindWindow(vbNullString, "Beat Dyslexia/EV guide")
SetParent lngGetApp, Frame1.hwnd
GetClientRect Frame1.hwnd, RectCoord
MoveWindow lngGetApp, 0, 0, RectCoord.Right - RectCoord.Left, RectCoord.Bottom - RectCoord.Top, True
End Sub
-
Re: Run another program inside a form
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.
-
Re: Run another program inside a form
Wow, that sounds promising - I'll go and look for that now and report back if I get a result!
Thank you!
-
Re: Run another program inside a form
:confused: Looks like this ocx costs money - is that right or do you know of a free version?
-
Re: Run another program inside a form
I've downloaded the free trial but see it needs the full version of Acrobat to work - does that mean the end-user has to have it, too?
My requirements are very simple - I simply want to be able to open a pdf in a child form to be able to either view it or print it.
Is the Smart Print Control (currently version 4.1) worth buying? It claims to be able to print many types of file.
-
Re: Run another program inside a form
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...
-
1 Attachment(s)
Re: Run another program inside a form
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.
vb Code:
Option Explicit
Private Sub Form_Load()
AcroPDF1.src = "C:\Users\VB-Guru\Documents\VS2005_Walkthroughs.pdf"
End Sub
-
Re: Run another program inside a form
Thanks for that!
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!
-
Re: Run another program inside a form
Nope - same error message with "C:\GP1-1.pdf" as the source.
Anyone any idea what's wrong?
-
Re: Run another program inside a form
Is the pdf file generated via some other creator and not Acrobat? If so then the pdf file may not be 100% compatible?[/color]
-
Re: Run another program inside a form
Quote:
Originally Posted by RobDog888
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...
-
Re: Run another program inside a form
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?
-
Re: Run another program inside a form
Acrobat Distiller 5.0 (Windows)
BTW, I wanted to drop a screenshot of the properties window here but it seems you can only paste an image from a website - or is there another way?
-
Re: Run another program inside a form
Yes, save it as a file (picture formats) and then attach it to a post using the Manage Attachments button when replying/editing.
I want to make sure the d's you are testing are valid but looks like they are ok.
What version of Acrobat Reader do you actually have on your system?
-
Re: Run another program inside a form
-
1 Attachment(s)
Re: Run another program inside a form
Try this example with pdf attached. I wrote it from my xp sp-2 system with Reader 7.0
-
Re: Run another program inside a form
Quote:
Originally Posted by RobDog888
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?
-
Re: Run another program inside a form
Yes, and it sounds like its in the same location as mine. Hmm, I'm wondering if you have VS SP-6 installed and any Acrobat updates.
-
Re: Run another program inside a form
Quote:
Originally Posted by RobDog888
Yes, and it sounds like its in the same location as mine. Hmm, I'm wondering if you have VS SP-6 installed and any Acrobat updates.
Downloading SP6 as I write...
I haven't installed any Acrobat updates - I only installed version 7 a few days ago.
-
Re: Run another program inside a form
Service pack 6 has made no difference. :(
Acrobat Reader is version 7.0.0 - downloading 7.0.9 now...
-
Re: Run another program inside a form
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.
-
Re: Run another program inside a form
Just installed the Acrobat update and your code now works.
Thanks for continuing to listen!
-
Re: Run another program inside a form
Cool! Glad its working for you now. :)
So 7.0 initial install needs an update. Good to know what the issue was. Thanks.
-
Re: [RESOLVED] Run another program inside a form
It has a problem:
When closing the form, does not close file pdf. How closes the file?
-
Re: [RESOLVED] Run another program inside a form
solution:
In a module:
Code:
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
-
Re: [RESOLVED] Run another program inside a form
It killed the process automatically for me on my system.