![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
I'm developing a program to create PDF's on the fly, but I'm not able to close the Acrobat Window that gets opened (Not by me, but by the process itself)... I couldn't find any API call to do that, is this possible ?
|
|
|
|
|
|
#2 |
|
Legend
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
|
Wonder how the process opens it. Perhaps with createobject one can create and terminate the instance as in the following link:
http://forum.planetebook.com/archive/3164.htm Using the class name (with the reader at least) is not too much fun, it's dynamic, changing with every instance. |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
This is what's happening... you create a PDF basically by printing a document. But then, since i'm using Adobe Distiller, it opens Adobe Writer, and it creates the document just fine... but it creates it in this particular location that I don't want it ! so, I copy it where I want, but then, I want to kill the old (original) file... but I can't ! why ? Adobe is STILL open, and using it ! sucks, right ?
That code from PlanetPDF (I got the other part that I'm using from there too !) looks great, thanks. I do need to join them all after creating them ! |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
How have you created the App ?? You could either use the SendMessage function which sends the specified message (control constants) to the window, this calls the window procedure for the window and doesn't return until the window procedure has processed the message so it is in effect synchronous, or the PostMessage function which posts a message to a thread’s message queue and returns immediately - asyncronous - try using this one. Nate is correct in that getting the Class name for that you get a dynamic name everytime eg Afx:400000:8:148e:0:408f, Afx:400000:8:148e:0:4757, Afx:400000:8:148e:0:4727 You can however look for just the partial name Str... I beleive there has being a thread on this to which I posted. There were a couple of solutions to this was supplied, I have amended it here. You can change it as required.....the sTitle or Caption of the PDF file is the key. But Postmsg should do it. eg. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long '// Close window Constant Const WM_CLOSE = &H10 All you then need is the handle to the window so call PostMessage PDF_WindowHandle, WM_CLOSE,0,0 Actually, now that I think of it the Html addin has a routine to get the windows...in this case it maximizes it, all you need to do is change it to send the WM_CLOSE const instead. Ay way here is the routine to get the Class name, window handle to that App and then Close it. Option Explicit Option Compare Text Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Const WM_CLOSE = &H10 Private psAppNameContains As String Private pbFound As Boolean Private sTitle As String Public Function AppTitleByStringPart(StringPart As String) As Boolean Dim lRet As Long psAppNameContains = StringPart lRet = EnumWindows(AddressOf CheckForInstance, 0) AppTitleByStringPart = pbFound pbFound = False End Function Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _ lParam As Long) As Long Dim lRet As Long Dim iNew As Integer If Trim(psAppNameContains = "") Then CheckForInstance = False Exit Function End If sTitle = Space(255) lRet = GetWindowText(lhWnd, sTitle, 255) sTitle = StripNull(sTitle) If InStr(sTitle, psAppNameContains) > 0 Then CheckForInstance = False pbFound = True Else CheckForInstance = True End If End Function Private Function StripNull(ByVal InString As String) As String Dim iNull As Integer If Len(InString) > 0 Then iNull = InStr(InString, vbNullChar) Select Case iNull Case 0 StripNull = InString Case 1 StripNull = "" Case Else StripNull = Left$(InString, iNull - 1) End Select End If End Function Sub GetClass() Dim WndHdl As Long, sWndTitle As String, RetVal As Long, lpClassName As String sWndTitle = InputBox("Enter at least one word from the Window Title:") If sWndTitle <> "" Then AppTitleByStringPart (sWndTitle) sWndTitle = sTitle WndHdl = FindWindow(vbNullString, sWndTitle) lpClassName = Space(256) RetVal = GetClassName(WndHdl, lpClassName, 256) MsgBox "Classname for Window Caption" & "[" & sWndTitle & "]" & vbLf & vbLf & _ " = " & Left$(lpClassName, RetVal) & vbCr & _ "WindowHandle:=" & WndHdl Else MsgBox "No match found for " & sTitle & "" End If PostMessage WndHdl, WM_CLOSE, 0, 0 End Sub _________________ Kind Regards, Ivan F Moala From the City of Sails ![]() [ This Message was edited by: Ivan F Moala on 2002-05-23 22:20 ] |
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Thanks Ivan, see ? told you, you know your API !
I'll give it a shot right away... I did find the SendMessage function, but didn't know where to look for the message constant to close the window, how do you know that ? APIGuide doesn't have them, and (obviously) neither AllApi.net (Because they're the same !) |
|
|
|
|
|
#6 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
|
|
|
|
|
|
#7 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
All window msg constants start with WM. In fact it wasn't till I really had a good look, while playing around that I realised that that was how the conventioning went eg. Public Const WM_CLOSE = &H10 Public Const WM_ACTIVATE = &H6 Public Const WM_ACTIVATEAPP = &H1C Public Const WM_PAINT = &HF Public Const WM_PASTE = &H302 Public Const WM_SETFONT = &H30 Public Const WM_SETFOCUS = &H7 Public Const WM_SETCURSOR = &H20 Winapi.txt...forgot where I got it, But I thought you had VB6, I think it's gets installed with it, although I believe it misses on a lot of other API calls and consts ?? Most Sendmessage API examples post this const as it's the most common one ie to close a window. Good luck....wouldn't mind see the code for the PDF file.....sounds handy to have. |
|
|
|
|
|
|
#8 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
I think WinAPI.txt is the one that you need to download from microsoft.
If I recall correctly, the Dan Appleman book has this on the CD and has his own modified API file. |
|
|
|
|
|
#9 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Sure ! (About the PDF code)... and it's really cool ! you (will) define which files (Word and Excel) to convert, in which order and where to put it.
|
|
|
|
|
|
#10 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|