Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.excel.programming > #109747

Re: close a pdf file opened via shellexecute

From GS <gs@v.invalid>
Newsgroups microsoft.public.excel.programming
Subject Re: close a pdf file opened via shellexecute
Date 2016-12-09 21:11 -0500
Organization A noiseless patient Spider
Message-ID <o2fo68$3f0$1@dont-email.me> (permalink)
References <96a5abdc-d536-4116-bf7f-ce65654cea98@googlegroups.com> <o2fmg1$unr$1@dont-email.me>

Show all headers | View raw


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

Back to microsoft.public.excel.programming | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

close a pdf file opened via shellexecute Matthew Dyer <matthew.e.dyer@gmail.com> - 2016-12-09 13:58 -0800
  Re: close a pdf file opened via shellexecute GS <gs@v.invalid> - 2016-12-09 20:42 -0500
    Re: close a pdf file opened via shellexecute GS <gs@v.invalid> - 2016-12-09 21:11 -0500
      Re: close a pdf file opened via shellexecute Matthew Dyer <matthew.e.dyer@gmail.com> - 2016-12-09 22:01 -0800
        Re: close a pdf file opened via shellexecute GS <gs@v.invalid> - 2016-12-10 01:07 -0500

csiph-web