PDA

View Full Version : Need VB to open and close CHM doc



djl0525
06-13-2010, 02:44 PM
PART 1:
I have 2 documents. One is a .docx, the other is .chm. I need the .chm to open and close automatically with the .docx document.

C:\Users\DNDINC\Documents\Word xx.docx
C:\Users\DNDINC\Documents\CHM Files\Help xx.chm

The path, C:\Users\DNDINC\Documents, is fluid. It changes all the time. What is constant is that the Help files are always in the current path... \CHM Files folder.

Can Word VB open the CHM file after the user opens the .docx file? And close it when the user closes the .docx file?

PART 2:
The other thing I'd love to do is conceal the CHM files. I'd like the entire folder (CHM Files) to be invisible to the user. And of course, the CHM file has a corresponding .docx file so they would have to both be concealed. Maybe they can be embedded in the docx file??? I don't know... Does anyone have any suggestions on this?

Thank you in advance for your consideration.
--DJ

fumei
06-14-2010, 09:51 AM
"Can Word VB open the CHM file after the user opens the .docx file? And close it when the user closes the .docx file?"

Yes. Iin the docx file Document_Open, and Document_Close:
Sub Document_Open
Documents.Open Filename:=ActiveDocument.Path & "\CHM Files\Help xx.chm"
End Sub

Sub Document_Close()
Documents("Helpxx.chm").Close
End Sub


"The other thing I'd love to do is conceal the CHM files." Why?

"And of course, the CHM file has a corresponding .docx file so they would have to both be concealed. " Why would they both have to be concealed? That does not, in itself follow.

In any case, if you mean "invisible" by ANY means (say with Explorer) then you are dealing with a Permissions issue, not a VBA issue.

djl0525
06-14-2010, 02:10 PM
Thanks fumei, for the quick reply.

Can Word tell Windows Help to open the CHM file? I believe the executable is hh.exe.

And can the close cod check to see if the CHM file is open ...in case the user closes the CHM file first?

--DJ

fumei
06-15-2010, 09:22 AM
Well, if the first is done (Windows help opens the CHM), then the second is NOT possible. Why? Because if the first occurs then the CHM is not a Documents object.

As for the first, yes, I suppose it possible. This is not my forte at all (using CHM files), but if you can Shell out to the executable, then I suppose the answer is yes.

Check to see you can pass the filename to Shell. If so (and I assume you can), then use Shell in the Document_Open to open the CHM. However, you will have to come up with a different method to close it.

djl0525
06-15-2010, 02:31 PM
I appreciate your reply Gerry. Unfortunately, it's not my forte either. :-)

Everyone, I am open to suggestions.
--DJ

TonyJollans
06-16-2010, 06:54 AM
** Disclaimer ** I have never done this for real, so please treat as a basis for investigation rather than a full blown solution.

This ought to work, BUT .. I have had some abends playing with it (although not with the code below as is), and I'm not sure why at the moment.

Declare Function HtmlHelp _
Lib "hhctrl.ocx" _
Alias "HtmlHelpA" _
(ByVal hWndCaller As Long, _
ByVal pszFile As String, _
ByVal uCommand As Long, _
dwData As Any) _
As Long

Const HH_DISPLAY_TOPIC = &H0&
Const HH_DISPLAY_TOC = &H1&
Const HH_CLOSE_ALL = &H12&

Sub Document_Open()
HtmlHelp ByVal &H0&, ActiveDocument.Path & "\CHM Files\Help xx.chm" , HH_DISPLAY_TOC, ByVal &H0&
End Sub

Sub Document_Close()
HtmlHelp ByVal &H0&, ByVal &H0&, HH_CLOSE_ALL, ByVal &H0&
End Sub

fumei
06-16-2010, 11:42 AM
Well that seems to work. I put in the Doc_open and _Close and it opens the CHM file correctly. If the CHM is still open on Doc_Close, it is closed. If the CHM has been closed before Doc_Close, there is no error.

Tony: " I have had some abends playing with it "

Huh? "abends"????

TonyJollans
06-16-2010, 12:17 PM
Before I had the Document_Close in place, if I left the Help window open and closed Word (2007), after apparently closing, it would pop up an abend ("Word has encountered an error and needs to close" or whatever it says). On one occasion editing the code crashed it.

I usually find this kind of problem being due to incorrect API Declares and/or calls but, as far as I can tell, I have it correct. I am not wonderfully knowledgeable about the Windows hierarchy and I have tested using the handle for the desktop, and the Word app, but as it seems to work without one, I stuck to that for the post. Any or all of these things could explain my problems - I just mention them because I don't know what caused them, as a warning, should djl0525 - or anybody else - experience a problem with the code. I do, of course, hope it all works - and that the posted code does not cause abends.

fumei
06-16-2010, 01:37 PM
I have never heard of "abends".






oh. Cute.

In any case, I tried your solution in various ways (ver 2002) and have encountered no abends. Or errors.

One more raised finger to 2007.

djl0525
06-20-2010, 06:03 PM
Please pardon my lack of responsiveness. I didn't know I had more replies. Must check my profile settings.

I really appreciate all the work and testing and feedback. I'm going to try it now.

Thanks again! --DJ

djl0525
06-20-2010, 06:31 PM
I put the code on ThisDocument and I get this error.

http://i577.photobucket.com/albums/ss211/djl0525/6-20-2010Compileerror.jpg

Did I put it in the wrong place?

--DJ

Tinbendr
06-21-2010, 05:02 AM
Try adding Private to the beginning of the Declare.

Private Declare Function HtmlHelp _

fumei
06-21-2010, 09:16 AM
Or.............put your code in a standard module, rather than ThisDocument.

"Did I put it in the wrong place?"

Well, yes and no. The error is precise. You can not have Public Declare statements in a object module (which ThisDocument is. It is the code module for the Document object.).

So there are two possible solutions.

1. make the declare Private
2. put the code in a standard module (rather than an object module)