Embedded Word Document with VB6

Hi,

I would like to know how i can embed an MSWord Document object in a form (i.e. frame control), without using Word.Application (only Word.Document).  So far i used the entire Word interface in my forms (2), but it seems to be unstable, so i'dd rather use only Document object, and control it myself.

Thanks,

Freg
LVL 1
fregAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richie_SimonettiIT OperationsCommented:
Hi again freq,
you could use setparent api too here.
Just use with hwnd of word document window not word app and hide main word window.
0
Richie_SimonettiIT OperationsCommented:
Or just use webbrowser control to navigate to desired document..
0
fregAuthor Commented:
Hi Richie,

so far :

        ret = FindWindow("Opusapp", vbNullString)
        SetParent ret, Frame2.hwnd

to embed the entire Word.Application window.  What for the brand new document object ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Richie_SimonettiIT OperationsCommented:
it is a little more api involved since you have to loop child windows of opusapp
example:

'in a form
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Me.AutoRedraw = True
    EnumChildWindows  FindWindow("Opusapp", vbNullString)
, AddressOf EnumChildProc, ByVal 0&
End Sub
'in a module
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, 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
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim sSave As String
    'Get the windowtext length
    sSave = Space$(GetWindowTextLength(hwnd) + 1)
    'get the window text
    GetWindowText hwnd, sSave, Len(sSave)
    'remove the last Chr$(0)
    sSave = Left$(sSave, Len(sSave) - 1)
    If sSave <> "" Then Form1.Print sSave
    'continue enumeration
    EnumChildProc = 1
End Function


But i think webbrowser control would be better for your work, isn't it?
0
Richie_SimonettiIT OperationsCommented:
_WwC is the classname for document window.
0
fregAuthor Commented:
Concerning WebBrowser, i must be able to type character and edit the .doc.  Does that control permit edition ?
0
Richie_SimonettiIT OperationsCommented:
I think so but you have to manage the save event by yourself since webbrowser will not save the doc automatically.
0
fregAuthor Commented:
How can i user WebBrowser control ?  Is it included with visual studio 6 ?
0
Richie_SimonettiIT OperationsCommented:
Yes, do a right click in Toolbox window, choose Components...
Locate Microsoft nternet control (NOT Internet transfer control)
Do a click in the check box at left side an do a click in OK button
From here you could add to your form like any other control.
0
Richie_SimonettiIT OperationsCommented:
'When you have added the control do the following:
dim withevents wdoc as word.document

'where you want to open word document use this code:
webbrowser1.navigate "path_to document goes here"

'At Documentcomplete event of webbrowser
if (pDisp is webbrowser.object) then
     set wdoc = webbrowser1.document
end if

'done, automate wdoc as usually.
0
fregAuthor Commented:
That seems fine.  But tell me, why is it better to use WebBrowser control instead of embed Word.Document objet and use it directly ?
0
Richie_SimonettiIT OperationsCommented:
It is less complicate, besides, since you said that is unstable...
I think it is unstable due to the fact that we are not doing a good "cleaning" when close the app to restore parent window, object refences, etc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fregAuthor Commented:
Ok then !  Thanks a lot Ritchie !
0
Richie_SimonettiIT OperationsCommented:
Glad to help  you... (again ;)
Thanks for "A" grade.
0
fregAuthor Commented:
lol  i think it was you last time isn't it (concerning embedded word again) ?
0
Richie_SimonettiIT OperationsCommented:
If you need more help on this, don't post other Q and spend your points, we could continue here.
(i know, i know, i am a nice person... :))
0
fregAuthor Commented:
Great !  you ARE very nice !
0
fregAuthor Commented:
Ritchie,

One little thing : After i modified my document (.doc) and finished to work on it, i do wdoc.SaveAs, and then i change it by WebBrowser1.Navigate "anotherdoc.doc", so the WebBrowser control bother me with a messagebox asking if i want to save the changes... any idea to withdraw that annoying function ?
0
Richie_SimonettiIT OperationsCommented:
humm, it appears that:
1) You are saving your cached document
2) You are saving document at server side.

We could try two things:

1) before navigate to anotherdoc.doc, do:

wdoc.saved = true


2)
wdoc.Application.DisplayAlerts =0
0
fregAuthor Commented:
Well it seems that it doesn't work.  But i bypass the problem as it :

' Make a copy of the source template file :
FileCopy "D:\PVTemplate.dot", "D:\Temp.dot"

' Open the temp file :
WebBrowser1.Navigate "D:\Temp.dot"

' And then save as the real name :
wdoc.SaveAs "D:\MyDoc.doc"
' Save again to deactivate the message
' But here, the copy is made on Temp.dot instead of MyDoc.doc (bizarre)
wdoc.Save

WebBrowser1.Navigate "D:\Another.doc"

' Kill the temporary file
Kill "D:\Temp.dot"

Ritchie, if you get any more information about that problem, let me know !  ;-)
0
Richie_SimonettiIT OperationsCommented:
Not at this moment. I have to do test to see the problem.
0
fregAuthor Commented:
Hi again Ritchie !

One more problem with wdoc object : while i edit the wdoc in the webbrowser object and everything is fine, but as soon i start MSWord aside (with a new document for example), it becomes impossible for me to edit the wdoc anymore (in webbrowser).  As if MSWord application took the entire control of my wdoc object.

Any idea ??
0
fregAuthor Commented:
...and if there's no concrete solution, maybe could we "block" the execution of MSWord application.  Since i can detect (with findwindow) it and notify the user to close MSWord, that remains ok.  But if a user start MSWord app AFTER the execution of my program, all the edition feature is compromised (very bad).

(by the way, if you get any solution about that i'll be glad to allow you some more points)

;-)
0
Richie_SimonettiIT OperationsCommented:
Points are not the problem, iwhat i need is time. I am very busy at this moment.
0
fregAuthor Commented:
Ok then.  I'll wait...

Thanks !
0
Richie_SimonettiIT OperationsCommented:
Are you using Office XP?
0
fregAuthor Commented:
It's Office 2000
0
Richie_SimonettiIT OperationsCommented:
I cannot reproduce your error.

1) i Navigated to desired document
2) edit it, saved
3)modified again
4)open Word app
5)save it
6) Modify again, saved
7)Modify again, not saved
8)Close word
9) close browser, ask for save, saved
0
cnfeiCommented:
hihi,

can i prevent the popup of asking to download the word file after calling this?
WebBrowser1.Navigate "D:\Another.doc"
0
fregAuthor Commented:
hum... bizarre, i don't know.  Maybe Richie got an answer...?
0
Richie_SimonettiIT OperationsCommented:
That should due to you have not Word installed.
0
cnfeiCommented:
not really...
i have installed MSWord in my machines already.

but as now i am using the Microsoft Internet Control. where it open the word document. It will prompt a message to ask to open or save...

you can try out this by opening an Internet Explorer and then type the location of your word file...(eg. D:\Another.doc)

can i skip this popup when i use the control?
0
Richie_SimonettiIT OperationsCommented:
I do, but i load word document inside internet explorer. Maybe, there is an issue with security settings on IE.
0
cnfeiCommented:
so it means it is impossible to use the microsoft internet control in vb6 to view word document which bypass the file download dialog box to ask for open or save?

or any other method i can use in order to view word document(actually i only wanna view without editing) by using vb6 except OLE and MS internet control?
0
Richie_SimonettiIT OperationsCommented:
You could try the first approach (Setparent API stuff), see at the beginning of this Q.
0
cnfeiCommented:
could me give me some sample code on it?

why set parent can do it? it can by pass the file download window?

what is  opusapp?

0
fregAuthor Commented:
It embed a word app in a VB6 control.  Try the code above, as ritchie suggested.
0
cnfeiCommented:
Actually i would like to show a word document in the form of vb6 so that user can view the document. Editing is not neccessary.

findWindow and setparent can make the word document embed to the form?
how?
0
cnfeiCommented:
i can embed the whole word application in the frame now..
but can't embed the word document only...
even if i use _WwC

dim a as long
set a = FindWindow("_WwC", "ABC.doc")

how can i embed word document only?
0
fregAuthor Commented:
Try to, instead using a Word.Application object, use a Word.Document object.
0
Richie_SimonettiIT OperationsCommented:
No. You are doing wrong way. Use FindWindow to locate main word app but use enumchildwindow to locate word document window.
0
cnfeiCommented:
Thanks for your suggestions, and i can work it out now..

however...can i resize the word document so that the word document size will follow the size changes of the form?

thanks a lot.
0
cnfeiCommented:
i have solved the problem already ..

thanks a lot.
0
peanut1010Commented:
Richie_Simonetti,

I embedded word into my form, but how can I make it close the form when I close word
also
How can u make work maximize to the form size on opening, I tried wdvbmaximized and nothing happens



0
Richie_SimonettiIT OperationsCommented:
peanut1010
i don't know what wdvbmaximized means but if you want to maximize the word document to fill your form and to manage close event of word to do the same thing with your form, you need an object reference variable pointing to that word document, have you one already declared and setted?
0
peanut1010Commented:
I figured out what to do with that thanks.

But lets say the user close Word, is there a way that it could also close the Form that is the parent to it.


MEaning you have

Setparent, ret me.hwnd

with is my form1

now when the user closes word form1 is still open. Can you program that, also I want to remove a menu option, lets say "Print" which is under file is that possible.

If that is too detailed to show please let me know and I will open up a question. I ran into your name and seen that u knew a lot about this.....
0
Richie_SimonettiIT OperationsCommented:
"and seen that u knew a lot about this....."
that's not totally true but thanks.

Have you an object variable pointing to word document?, is it declared withevents?
if so, put this code under it:

unload me


If it is the only form, it should close your app too.
0
peanut1010Commented:
this is what I have



Set wdapp = New Word.Application
With wdapp
    'wdApp.Documents.Add
    'or
    Set wddoc = .Documents.Open(sdestination, , False)
    .WindowState = wdWindowStateNormal
    .DisplayAlerts = wdAlertsNone
    .Visible = True
End With
    Dim ret As Long
    ret = FindWindow("Opusapp", vbNullString)
    SetParent ret, form1.hwnd
    form1.Show
    form1.WindowState = vbMaximized
'this bottom line maximizes word on form1    
wdapp.WindowState = wdWindowStateMaximize



 
0
Richie_SimonettiIT OperationsCommented:
Hummm.
No.
dim withevents wdapp as word.application

'eventually dim withevents wddoc as word.document

'then, on:
Private Sub wdapp_Quit()
    unload me ' or myform
End Sub



0
peanut1010Commented:
it works though, what is the way you are want me to do it????
0
peanut1010Commented:
how would u eliminate menu buttons????
0
Richie_SimonettiIT OperationsCommented:
"it works though, what is the way you are want me to do it????"

Sorry, my english is not so good... what did you mean?

For menu, you could hide the entire menu bar.
0
Richie_SimonettiIT OperationsCommented:
Sorry for to be so general is the postings but i haven't VB installed anymore
:(
0
peanut1010Commented:
not the menu certain items in the menu, example = print, save, etc

if u don't have vb what do u use now then. just curoius
0
Richie_SimonettiIT OperationsCommented:
MS Excel to test some of th examples.
At this moment, i am not programming at all, just a little Lotus Notes/LotusScript stuff.

I didn't hide some of the menu items before... i must take a look on how to do it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.