Solved

Embedded Word Document with VB6

Posted on 2003-11-28
55
2,596 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:freg
  • 25
  • 16
  • 8
  • +1
55 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839319
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839323
Or just use webbrowser control to navigate to desired document..
0
 
LVL 1

Author Comment

by:freg
ID: 9839335
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839351
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839359
_WwC is the classname for document window.
0
 
LVL 1

Author Comment

by:freg
ID: 9839371
Concerning WebBrowser, i must be able to type character and edit the .doc.  Does that control permit edition ?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839385
I think so but you have to manage the save event by yourself since webbrowser will not save the doc automatically.
0
 
LVL 1

Author Comment

by:freg
ID: 9839403
How can i user WebBrowser control ?  Is it included with visual studio 6 ?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839431
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839442
'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
 
LVL 1

Author Comment

by:freg
ID: 9839468
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 250 total points
ID: 9839510
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
 
LVL 1

Author Comment

by:freg
ID: 9839541
Ok then !  Thanks a lot Ritchie !
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839563
Glad to help  you... (again ;)
Thanks for "A" grade.
0
 
LVL 1

Author Comment

by:freg
ID: 9839577
lol  i think it was you last time isn't it (concerning embedded word again) ?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839618
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
 
LVL 1

Author Comment

by:freg
ID: 9839633
Great !  you ARE very nice !
0
 
LVL 1

Author Comment

by:freg
ID: 9839801
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9850447
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
 
LVL 1

Author Comment

by:freg
ID: 9850552
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9850865
Not at this moment. I have to do test to see the problem.
0
 
LVL 1

Author Comment

by:freg
ID: 9866600
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
 
LVL 1

Author Comment

by:freg
ID: 9866638
...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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9866889
Points are not the problem, iwhat i need is time. I am very busy at this moment.
0
 
LVL 1

Author Comment

by:freg
ID: 9867609
Ok then.  I'll wait...

Thanks !
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9950183
Are you using Office XP?
0
 
LVL 1

Author Comment

by:freg
ID: 9950401
It's Office 2000
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9952362
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
 

Expert Comment

by:cnfei
ID: 10094048
hihi,

can i prevent the popup of asking to download the word file after calling this?
WebBrowser1.Navigate "D:\Another.doc"
0
 
LVL 1

Author Comment

by:freg
ID: 10096541
hum... bizarre, i don't know.  Maybe Richie got an answer...?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 10096549
That should due to you have not Word installed.
0
 

Expert Comment

by:cnfei
ID: 10100723
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 10103577
I do, but i load word document inside internet explorer. Maybe, there is an issue with security settings on IE.
0
 

Expert Comment

by:cnfei
ID: 10104903
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 10105061
You could try the first approach (Setparent API stuff), see at the beginning of this Q.
0
 

Expert Comment

by:cnfei
ID: 10105263
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
 
LVL 1

Author Comment

by:freg
ID: 10105483
It embed a word app in a VB6 control.  Try the code above, as ritchie suggested.
0
 

Expert Comment

by:cnfei
ID: 10110243
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
 

Expert Comment

by:cnfei
ID: 10110634
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
 
LVL 1

Author Comment

by:freg
ID: 10112932
Try to, instead using a Word.Application object, use a Word.Document object.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 10112933
No. You are doing wrong way. Use FindWindow to locate main word app but use enumchildwindow to locate word document window.
0
 

Expert Comment

by:cnfei
ID: 10120907
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
 

Expert Comment

by:cnfei
ID: 10127645
i have solved the problem already ..

thanks a lot.
0
 

Expert Comment

by:peanut1010
ID: 11280571
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11280844
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
 

Expert Comment

by:peanut1010
ID: 11281934
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11282155
"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
 

Expert Comment

by:peanut1010
ID: 11282249
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11282299
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
 

Expert Comment

by:peanut1010
ID: 11282360
it works though, what is the way you are want me to do it????
0
 

Expert Comment

by:peanut1010
ID: 11282363
how would u eliminate menu buttons????
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11282537
"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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11282555
Sorry for to be so general is the postings but i haven't VB installed anymore
:(
0
 

Expert Comment

by:peanut1010
ID: 11282702
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 11283360
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now