Results 1 to 20 of 20

Thread: How to display WORD document in form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    How to display WORD document in form?

    Hi guys...I have a WORD document with text and pictures inside, say for example 5 pages...

    Is it possible to display the WORD document in a form if so how do I do that? Would be great if some simple sample code can be provided for me to build on.......

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    There are two ways to go here.

    You can either open the document directly by opening a session of Word, or you could put an OLE control on your form and link it to a document.

    The latter is less flexible.

    If you just want to open it, set a reference to the Microsoft Word Object Library, and do something like:
    VB Code:
    1. Private Sub Command1_Click()
    2.    Dim oWord As Word.Application
    3.     Dim oDoc As Word.Document
    4.    
    5.  
    6.     Set oWord = GetObject(, "Word.Application")
    7.     If oWord Is Nothing Then
    8.         Set oWord = CreateObject("Word.Application")
    9.     End If
    10.    
    11.     oWord.Documents.Open FileName:="d:\worddocs\gone.doc"
    12.     oWord.Documents("gone.doc").Activate
    13.    
    14.     Set oDoc = oWord.ActiveDocument
    15.     oWord.Visible = True
    16. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    Yes Hack my current program has something liek what you mentioedn which opens and activates WORD seperately.....but what I would liek for this otehr portion is to display the WORD document inside a form.......you know.....like soemtimes when you click on HELP of a program and it pops out with text and pictures inside...

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    Have you tried embedding it as an OLE object?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    Erm...I have no idea how to do that.....can u try to guide me along?

    Not using ADO right?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    No, no. On your VB toolbar, you will see a control that has OLE on it.

    Place one of those controls on your form.

    A window will open up and display a variety of things you can connect to such as a Visio drawing, an Excel Spreadsheet, a Word document, a Powerpoint presentation, just a whole bunch of stuff.

    Select the Word document selection, and it should allow you to "connect" to the document of your choice.

    I really haven't had a whole lot of actual experience with this control as I normally just use the applications object library and connect to whatever I'm connecting to that way, however, a lot of people prefer this method.

    Give it a shot and see if you like it.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    Ok thanks...will get back...

  8. #8
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: How to display WORD document in form?

    I placed this on PSC a while back [My demo] it shows how you can place the word application within a form like a child.

    Hope this helps!!!

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    Bombdrop, is there a way to do it without creating modules?

    I just tried it on OLE...yes the idea is there but the formatting is frustrating...the left part of the OLE box cant seem to be aligned with the left of my WORD document such that part of the text keep getting blocked on the left....and if my WORD os more than 1 page its like the form can only display one page due to the limited maximum size of the form...
    Last edited by boroangel; Oct 19th, 2005 at 08:46 AM.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: How to display WORD document in form?

    Look at using the SetParent API.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    Quote Originally Posted by RobDog888
    Look at using the SetParent API.
    Along with FindWindow.

    I elected to put the Word document inside a Frame on my form. If you want to make it a part of the Form itself, please do so. Here is an example.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
    4. ByVal hWndNewParent As Long) As Long
    5.  
    6. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    7. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    8.  
    9. Private Sub Command1_Click()
    10. Dim lngGetDocHandle As Long
    11. Dim oWord As Word.Application
    12. Dim oDoc As Word.Document
    13.    
    14.     If oWord Is Nothing Then
    15.         Set oWord = CreateObject("Word.Application")
    16.     End If
    17.    
    18.     oWord.Documents.Open FileName:="d:\worddocs\mydoc.doc"
    19.     oWord.Documents("mydoc.doc").Activate
    20.    
    21.     Set oDoc = oWord.ActiveDocument
    22.     oWord.Visible = True
    23.  
    24. lngGetDocHandle = FindWindow(vbNullString, "MyDoc.doc - Microsoft Word")
    25.  
    26. SetParent lngGetDocHandle, Frame1.hwnd
    27.  
    28. End Sub

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    SetParent API? Er..........

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    119

    Re: How to display WORD document in form?

    Quote Originally Posted by Hack
    Along with FindWindow.

    I elected to put the Word document inside a Frame on my form. If you want to make it a part of the Form itself, please do so. Here is an example.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
    4. ByVal hWndNewParent As Long) As Long
    5.  
    6. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    7. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    8.  
    9. Private Sub Command1_Click()
    10. Dim lngGetDocHandle As Long
    11. Dim oWord As Word.Application
    12. Dim oDoc As Word.Document
    13.    
    14.     If oWord Is Nothing Then
    15.         Set oWord = CreateObject("Word.Application")
    16.     End If
    17.    
    18.     oWord.Documents.Open FileName:="d:\worddocs\mydoc.doc"
    19.     oWord.Documents("mydoc.doc").Activate
    20.    
    21.     Set oDoc = oWord.ActiveDocument
    22.     oWord.Visible = True
    23.  
    24. lngGetDocHandle = FindWindow(vbNullString, "MyDoc.doc - Microsoft Word")
    25.  
    26. SetParent lngGetDocHandle, Frame1.hwnd
    27.  
    28. End Sub
    Hack, for this code, instead of using an OLE.....what control is being used? A Findwindow?

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: How to display WORD document in form?

    FindWindow is an API call and not a control. No OLE but the Word Object Model.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    Quote Originally Posted by boroangel
    Hack, for this code, instead of using an OLE.....what control is being used? A Findwindow?
    You have a variety of options now.

    You can just plain open it as my first code example does; you can use the OLE control to embed your document; your can programmatically open it, and place it within a container on your form.

    I'd suggest trying all three options to see which one you like the best.

  16. #16
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: How to display WORD document in form?

    The easiest way is to use the WebBrowser control. Instead of pointing it to a URL, specify the path of the Word doc you want to display.
    "It's cold gin time again ..."

    Check out my website here.

  17. #17
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to display WORD document in form?

    Quote Originally Posted by BruceG
    The easiest way is to use the WebBrowser control. Instead of pointing it to a URL, specify the path of the Word doc you want to display.
    Where did the web come into this?

  18. #18
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: How to display WORD document in form?

    The easiest way is to use the WebBrowser control.
    This is a very nice way to view and edit word docs from within a VB form.

  19. #19
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: How to display WORD document in form?

    Wouldnt it be easier just to use a rich text box? Then all the formatting can be seen and used.

    You could just use something like below like I use in my programs.
    VB Code:
    1. With rtbWork
    2.         .LoadFile App.Path & "/Files/HowItWorks.rtf"
    3.         .SelStart = 0
    4.         .SelLength = Len(.Text)
    5.         .SelColor = vbYellow
    6.         .SelLength = 0
    7.     End With
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  20. #20
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: How to display WORD document in form?

    Hack -
    The WB control can be used not only to navigate to websites, but also to display local Office docs (works for Word, Excel, PowerPoint).
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width