Embedded Word Document with VB6


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.


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.
Richie_SimonettiIT OperationsCommented:
Or just use webbrowser control to navigate to desired document..
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 ?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

'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?
Richie_SimonettiIT OperationsCommented:
_WwC is the classname for document window.
fregAuthor Commented:
Concerning WebBrowser, i must be able to type character and edit the .doc.  Does that control permit edition ?
Richie_SimonettiIT OperationsCommented:
I think so but you have to manage the save event by yourself since webbrowser will not save the doc automatically.
fregAuthor Commented:
How can i user WebBrowser control ?  Is it included with visual studio 6 ?
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.
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.
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 ?
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.

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 !
Richie_SimonettiIT OperationsCommented:
Glad to help  you... (again ;)
Thanks for "A" grade.
fregAuthor Commented:
lol  i think it was you last time isn't it (concerning embedded word again) ?
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... :))
fregAuthor Commented:
Great !  you ARE very nice !
fregAuthor Commented:

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 ?
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

wdoc.Application.DisplayAlerts =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)

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 !  ;-)
Richie_SimonettiIT OperationsCommented:
Not at this moment. I have to do test to see the problem.
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 ??
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)

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

Thanks !
Richie_SimonettiIT OperationsCommented:
Are you using Office XP?
fregAuthor Commented:
It's Office 2000
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

can i prevent the popup of asking to download the word file after calling this?
WebBrowser1.Navigate "D:\Another.doc"
fregAuthor Commented:
hum... bizarre, i don't know.  Maybe Richie got an answer...?
Richie_SimonettiIT OperationsCommented:
That should due to you have not Word installed.
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?
Richie_SimonettiIT OperationsCommented:
I do, but i load word document inside internet explorer. Maybe, there is an issue with security settings on IE.
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?
Richie_SimonettiIT OperationsCommented:
You could try the first approach (Setparent API stuff), see at the beginning of this Q.
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?

fregAuthor Commented:
It embed a word app in a VB6 control.  Try the code above, as ritchie suggested.
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?
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?
fregAuthor Commented:
Try to, instead using a Word.Application object, use a Word.Document object.
Richie_SimonettiIT OperationsCommented:
No. You are doing wrong way. Use FindWindow to locate main word app but use enumchildwindow to locate word document window.
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.
i have solved the problem already ..

thanks a lot.

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

Richie_SimonettiIT OperationsCommented:
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?
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.....
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.
this is what I have

Set wdapp = New Word.Application
With wdapp
    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.WindowState = vbMaximized
'this bottom line maximizes word on form1    
wdapp.WindowState = wdWindowStateMaximize

Richie_SimonettiIT OperationsCommented:
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

it works though, what is the way you are want me to do it????
how would u eliminate menu buttons????
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.
Richie_SimonettiIT OperationsCommented:
Sorry for to be so general is the postings but i haven't VB installed anymore
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
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.
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.