PDA

View Full Version : Solved: Display Help File Extension



d4vem
10-05-2006, 06:58 AM
:hi: I am trying to display a help file with an extension of ".chm" a Compiled HTML Help File. I am using the following code without much success, I think it may be to do with the "Set hf = hfo.openTextFile" command line that needs to be changed any assistance would be much apprieciated.

Sub OpenHelpFile()
HelpFile = "C:\Test Help.chm"
Dim hfo As Object
Dim hf As Object
Set hfo = CreateObject("Scripting.FileSystemObject")
Set hf = hfo.openTextFile(HelpFile)

Set hfo = Nothing

End Sub

mvidas
10-05-2006, 07:12 AM
Hi,

The compiled help files are much more than text, thats why it won't work with just the file system object.

What you can do is use the ShellExecute API method to open the file with the default viewer. Make sure the Declare line is above all other subs/functions in the module:Public 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
Sub OpenHelpFile()
Dim HelpFile As String
HelpFile = "C:\Test Help.chm"
ShellExecute 0&, "", HelpFile, "", "", vbMaximizedFocus
End SubMatt

ALe
10-05-2006, 07:13 AM
Try this where NameOfYourChmFile is the full name of your file
Option Explicit

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
Sub OpenHelp()
Call ShellExecute(0, "Open", NameOfYourChmFile & vbNullString, _
vbNullString, vbNullString, 1)
End Sub

d4vem
10-06-2006, 05:19 AM
Thanks Matt and Ale they both worked fine.

The only other question I have is that when the file help is opened it is defaulting to the virtually the full size of the screen and not the size it is when I open the help file direct do you know anyway of using VBA to change the size of the help file on opening? I am trying to look at the HTML Help Workshop documentation to see if theres anything there but have been unable to find anything yet.

Again thanks to you both for the initial solution.

d4vem
10-06-2006, 05:24 AM
Apologies, Apologies.

If you resize the file once opened and close it then re open the file it opens at the size it was last closed. Please ignore my previous question.

Thanks

mvidas
10-06-2006, 05:36 AM
The size it is opening at is because of the last argument in our ShellExecute lines.. I used vbMaximizedFocus so it would be full screen, though ALe used "1" which is the constant value of vbNormalFocus. With the 1 or vbnormalfocus being used, it should open up at the last size it was at. I'm guessing his code opened it up full screen since you had just opened up mine, and I had forced it to full screen. He and I more-or-less used the same code, so whichever version you went with, make sure the last argument is a 1, or one of these:vbHide '0
vbNormalFocus '1
vbMinimizedFocus '2
vbMaximizedFocus '3
vbNormalNoFocus '4
vbMinimizedNoFocus '6You can see more details by looking up "Shell" in the VBA help system.
Matt

d4vem
10-10-2006, 06:53 AM
Thanks Matt it worked on both examples just need to decide what one to use!!!!

Ken Puls
10-10-2006, 02:29 PM
Hey guys,

I have an alternative for you as well. This uses a different method than the ShellExecute API, which is targetted specifically at Help Files:

Public Declare Function HTMLHELP Lib "hhctrl.ocx" _
Alias "HtmlHelpA" (ByVal hWnd As Long, _
ByVal lpHelpFile As String, _
ByVal wCommand As Long, _
ByVal dwData As Long) As Long

Const strHelpPath As String = _
"C:\Documents and Settings\My Help File.chm"


Function OpenCHM(lType As Long, Optional lContextID = 0) As Boolean
'Open a CHM help file

Dim lHH_Value As Long

'Check what point user wants file opened at
Select Case lType
Case Is = 0
'Display the help file at last selected item
lHH_Value = &H0
Case Is = 1
'Display the table of contents at last selected item
lHH_Value = &H1
Case Is = 2
'Display the index.
lHH_Value = &H2
'Ensure contextID is 0 to prevent crash
lContextID = 0
Case Is = 4
'Display table of contents at current contextID
lHH_Value = &HF
'Ensure on Table of Contents page
HTMLHELP 0, strHelpPath, &H1, 0
End Select

If HTMLHELP(0, strHelpPath, lHH_Value, lContextID) = 0 Then
'Was not succesful opening file
OpenCHM = False
Else
'Opened successfully
OpenCHM = True
End If

End Function

The main advantages are the you can open your help files at specific context ID's, and you don't need to open a new copy of the file every time.

You can call the functions from a routine, but the following shows its use in the debug window.
Debug.Print OpenCHM(1) 'Opens the help file at the last used point, or returns false
Debug.Print OpenCHM(2) 'Opens the file at the last used point in the Table of Contents, or returns false
Debug.Print OpenCHM(4,1000) 'Opens the file at the help topic with a contect ID of 1000, or returns false

HTH,