Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default close a pdf file opened via shellexecute

I am using the following code to open a pdf file.

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

strFile = 'assume strFile is opening without issue

ShellExecute 0, "open", strFile, vbNullString, vbNullString, 0



how can I close the PDF file with vba script? Thank you in advance!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default close a pdf file opened via shellexecute

I am using the following code to open a pdf file.

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

strFile = 'assume strFile is opening without issue

ShellExecute 0, "open", strFile, vbNullString, vbNullString, 0



how can I close the PDF file with vba script? Thank you in advance!


You need to know the name of the app that opens PDFs, and determine
what its window 'title' is so you can grab its 'handle'. Once done you
can send it a close message. You could simple query open windows for
determining which has the filename in its 'title'; -obviates needing to
know which app opens PDFs on another machine if your project runs
there.

I have code that runs an out-of-process EXE as though it was
in-process. It locates the window and grabs its handle so that EXE ends
when my project shuts down. I can dig for the exact code and post
back...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default close a pdf file opened via shellexecute

I wrote this code so I could use HTML.exe files as userguides, as
though they were run 'in-process' even though they are stand-alone EXE
apps. You'll have to modify it to work with a PDF viewer (something I
was meaning to do before I bought Adobe RoboHTML!)

'
' Description: This module handles code for using HE.exe publications
(HePub) as the application User Guide.
' The component assumes using a single HePub.
' Syntax is provided for use with VB and VBA;
' Assumes HePubs are stored in the same folder as the
application.
'
' Two methods are shown:
' Method1: Uses separate procedures for PageName
and TopicId.
' Method2: Uses a single procedure for both
PageName and TopicId.
'
' Uses the following global variables as
defined/initialized in the mOpenClose module:
' gszAPP_NAME Global string constant
that holds the application name
' gszAppPath Global string variable
that holds the path to the app folder
' gszPATH_SEP Global string constant
that holds the OS path separator character
'
' Date Developer Action
' -------------------------------------
' 04/03/2008 Garry Sansom Created
'

Option Explicit
Option Private Module

Private Const mszModule As String = "mHEHelp"

'//[HEAPI.DLL Constants]//
Private Const HEC_EXITPUB As Integer = 0 'Exit HePub
Private Const HEC_LOADPAGE As Integer = 1 'Load page using
name
Private Const HEC_BRINGTOFRONT As Integer = 2 'Bring to front
Private Const HEC_LOADID As Integer = 3 'Load page using ID
Private Const HEC_ISHEPUB As Integer = 4 'Internal use

'//[WinAPI Declarations]//
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As
Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias
"GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal
nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal
cch As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long,
ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd
As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As
Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As
Long) 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

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As
Long)

'[WinAPI Constants]
Private Const SW_MINIMIZE = 6
Private Const SW_RESTORE = 9
Private Const WM_CLOSE = &H10
Private Const WM_SHOWWINDOW = &H18

'//[Variable Definitions]//
' This constant is appended to the global constant gszAPP_NAME, to form
the complete filename of our HePub.
' We use this with application name so we have some way to associate it
as our help file.
Public Const gsHELP_FILE_DEF As String = "-UserGuide.exe"
'?Alternatives?: "-HePub.exe", "-Help.exe"

Private mszAppTitle As String
Public glHEPubHwnd As Long 'The handle of the
running instance of our HePub
Private mlReturn As Long
Private mlHwnd As Long
Private mbMatchCase As Boolean
Private mbVisible As Boolean
Private m_MatchCriteria As FindWindowCriteria

' Constants used by FindHEPubWindow
Public Enum FindWindowCriteria
fwcStartsWith = 0
fwcContains = 1
fwcMatches = 2
End Enum

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''
'[Implementation]
Function bHEPubIsRunning() As Boolean
' Checks if our HePub is running, starts it if not.

Const sSource As String = "bHEPubIsRunning()"

'Make sure our path variable is initialized
If gszAppPath = "" Then gszAppPath = ThisWorkbook.Path & gszPATH_SEP
'vba
' If gszAppPath = "" Then gszAppPath = App.Path & gszPATH_SEP 'vb6


'Check our variable to see if there's a running instance.
'This value is 0 if not running, otherwise it's our HePub window
handle.
glHEPubHwnd = FindHEPubWindow(gszAPP_NAME & mszAPP_TITLE_DEF, , ,
True)

If glHEPubHwnd < 0 Then
If IsIconic(glHEPubHwnd) Then
Call ShowWindow(glHEPubHwnd, SW_RESTORE)
End If
Call SetForegroundWindow(glHEPubHwnd)
GoTo NormalExit
Else
'Start our HePub
'This will return an error if it fails
On Error Resume Next
mlReturn = Shell(gszAppPath & gszAPP_NAME & mszHELP_FILE_DEF,
vbNormalFocus) = 0
On Error GoTo 0
If mlReturn = 0 Then GoTo NormalExit
'Get its handle
glHEPubHwnd = FindHEPubWindow(gszAPP_NAME & mszAPP_TITLE_DEF, , ,
True)
End If


NormalExit:
bHEPubIsRunning = (glHEPubHwnd < 0)

End Function '//bHEPubIsRunning()


Public Sub ExitHePub()
' Closes the HePub on shutdown

Const sSource As String = "ExitHePub()"

'Check our variable to see if there's a running instance.
'This value is 0 if not running, otherwise it's our HePub window
handle.
glHEPubHwnd = FindHEPubWindow(gszAPP_NAME & mszAPP_TITLE_DEF, , ,
True)

If glHEPubHwnd < 0 Then
mlReturn = SendMessage(glHEPubHwnd, WM_CLOSE, 0&, 0&)
glHEPubHwnd = 0
End If

End Sub '//ExitHePub()


''''''''''
'[Method1]
''''''''''
Public Sub ShowHePubPage(ByVal PageName As String)
' Displays the specified page from the HePub file.
'
' Arguments: PageName [in] The full path and file name to the
HePub file to display.
'

Const sSource As String = "ShowHePubPage()"

If bHEPubIsRunning Then
mlReturn = SendMessage(glHEPubHwnd, HEC_LOADPAGE, PageName, 0&)
mlReturn = ShowWindow(glHEPubHwnd, SW_RESTORE)
End If

End Sub '//ShowHePubPage()


Public Sub ShowHePubId(ByVal PageId As Integer)
' Displays the specified PageId from the HePub file.
'
' Arguments: PageId [in] The numeric PageId to display.
This is defined by HE for each page when compiling.
'

Const sSource As String = "ShowHePubId()"

If bHEPubIsRunning Then
mlReturn = SendMessage(glHEPubHwnd, HEC_LOADID, PageId, 0&)
mlReturn = ShowWindow(glHEPubHwnd, SW_RESTORE)
End If

End Sub '//ShowHePubId()


'''''''''
' Method2
'''''''''
Public Sub ShowHePubTopic(ByVal Topic As String, Optional IsId As
Boolean = False)
' Displays the specified help topic as specified using the PageName or
PageId
' (Used in place of the two previous procedures)
'
' Arguments: Topic [In] The topic to be displayed once the HePub
file has been opened.
' If using PageName, it's already a string.
' If using PageId, convert to string before
passing to here.
' IsId [In} Optional. True if passing PageId. Default
is False (passing PageName)
'

Const sSource As String = "ShowHePubTopic()"

Dim iLoadAction As Integer

If IsId Then iLoadAction = HEC_LOADID Else iLoadAction = HEC_LOADPAGE

If bHEPubIsRunning Then
mlReturn = SendMessage(glHEPubHwnd, iLoadAction, Topic, 0&)
mlReturn = ShowWindow(glHEPubHwnd, SW_RESTORE)
End If

End Sub '//ShowHePubTopic()

Public Function FindHEPubWindow(AppTitle As String, Optional
MatchCriteria = fwcStartsWith, Optional MatchCase As Boolean = False,
Optional MustBeVisible As Boolean = False) As Long
' Finds if there's a running instance of our HePub using its
Publication Title.
' Requires the EnumOpenWindows() function
'
' Arguments: AppTitle The window caption text we are
looking for. This is our Publication Title.
' MatchCriteria Optional. The type of comparison we
want to make.
' Must be one of the
FindWindowCriteria constants. Default is fwcStartsWith if omitted.
' **Do not include any text
appended to the default Publication Title.
' MatchCase Optional Boolean value to determine
if search is case sensitive.
' If omitted the default is
False, and all text is converted to UCase for comparison.
' MustBeVisible Optional Boolean value to determine
if our HePub window must be visible (not hidden).
' If omitted the default is
False.
'
' Returns: On success, the window handle of the running instance
of our HePub. On fail, 0.
'

Const sSource As String = "FindHEPubWindow()"

'Initialize search parameters.
mlHwnd = 0
m_MatchCriteria = MatchCriteria
mbMatchCase = MatchCase
mszAppTitle = AppTitle

'If case-insensitive.
If Not mbMatchCase Then mszAppTitle = UCase$(mszAppTitle)

'Find our HePub window handle.
'This will return 0 if it's not running.
Call EnumWindows(AddressOf EnumOpenWindows, MustBeVisible)
FindHEPubWindow = mlHwnd

End Function '//FindHEPubWindow()

Private Function EnumOpenWindows(ByVal hWnd As Long, ByVal lParam As
Long) As Long
' This enumerates the open windows to find our HePub, and if so returns
its handle
'
' Arguments: hWnd [In} Uses AddressOf so we can recursively look
at each running window process
' lParam [In] Whether the window process must be
visible. Exits if False
'
' Returns: The handle of our HePub window
'

Const sSource As String = "EnumOpenWindows()"

Static WindowText As String
Static lReturn As Long

'Our HePub window must be visible
If lParam Then
If IsWindowVisible(hWnd) = False Then EnumOpenWindows = True: Exit
Function
End If

'Get the window caption
WindowText = Space$(256)
lReturn = GetWindowText(hWnd, WindowText, Len(WindowText))

If lReturn Then
'Clean up window text.
WindowText = Left$(WindowText, lReturn)
If mbMatchCase = False Then
WindowText = UCase$(WindowText)
End If

'Compare our MatchCriteria to the window caption to find our HePub.
Select Case m_MatchCriteria
Case fwcStartsWith: If InStr(WindowText, mszAppTitle) = 1 Then
mlHwnd = hWnd
Case fwcContains: If InStr(WindowText, mszAppTitle) < 0 Then
mlHwnd = hWnd
Case fwcMatches: If WindowText = mszAppTitle Then mlHwnd = hWnd
End Select
End If

'Continue enumeration if we haven't found our HePub yet
EnumOpenWindows = (mlHwnd = 0)

End Function '//EnumOpenWindows()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default close a pdf file opened via shellexecute

Well.. it'll take a bit of time to peruse through all of that, but as always you come through well and thurough. Thanks a ton Garry!
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default close a pdf file opened via shellexecute

Well.. it'll take a bit of time to peruse through all of that, but as
always you come through well and thurough. Thanks a ton Garry!


You're most welcome!
Let me know if you need assistance, but I think you just need to
replace my HEHelp refs with vars that work for your needs...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XL file opened using automation - how close? xp Excel Programming 2 May 27th 10 11:02 PM
XL file opened using automation - how close? Jacob Skaria Excel Programming 2 May 27th 10 10:16 PM
XL file opened using automation - how close? Jacob Skaria Excel Programming 0 May 27th 10 09:32 PM
XL file opened using automation - how close? xp Excel Programming 0 May 27th 10 09:11 PM
Closing a program which was opened with shellexecute JonT Excel Programming 1 February 27th 08 01:46 PM


All times are GMT +1. The time now is 10:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"