Results 1 to 18 of 18

Thread: [RESOLVED] Adding Microsoft Word Object Library

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Resolved [RESOLVED] Adding Microsoft Word Object Library

    I want to add Microsoft Word Object Library to my toolbox from the COM component list. However although I have MS word 2007 installed in my computer this component is not available or not visible.

    Help please!

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    You might try browsing to the OLB file manually:
    Code:
    C:\Program Files (x86)\Microsoft Office\OFFICE11\MSWORD.OLB
    You would place your office version for OFFICE11. Although, the issue suggests that the application is not installed correctly.

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

    Re: Adding Microsoft Word Object Library

    Rather than a component, check the references "Add Reference" You should find the library there.
    Attached Images Attached Images  

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    Quote Originally Posted by ForumAccount View Post
    You might try browsing to the OLB file manually:
    Code:
    C:\Program Files (x86)\Microsoft Office\OFFICE11\MSWORD.OLB
    You would place your office version for OFFICE11. Although, the issue suggests that the application is not installed correctly.
    I think you could be right. I shall re-install word.
    Now where did I put that disk!

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    Just to make sure we're all on the same page here (Hack got me thinking). Are you trying to add a reference to the Microsoft Word Object Library via the Toolbox > Choose Items... or the References > Add Reference > COM?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    Quote Originally Posted by ForumAccount View Post
    Just to make sure we're all on the same page here (Hack got me thinking). Are you trying to add a reference to the Microsoft Word Object Library via the Toolbox > Choose Items... or the References > Add Reference > COM?
    I want to to my toolbox from Toolbox > Choose Items ---- but the MS Word xx object library is not available or visible there. Although I have it installed on my computer.
    Last edited by ConfusedAgain; Aug 3rd, 2011 at 02:41 AM.

  7. #7
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: Adding Microsoft Word Object Library

    You should try adding it through references before reinstalling.
    Then if it works, you simply load the object at runtime instead of design time

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    You should do as Hack suggested. Try adding it as a reference because it isn't actually designed to show up in the Toolbox Item dialog. There are a couple ways to add a reference:
    1. From the Solution Explorer click "Show All Files", then right-click the References node and click Add Reference
    2. From the Menu > Project > Add Reference
    3. From the Solution Explorer double-click My Project > References tab > Add Reference

    Whichever method you choose, navigate to the COM tab and see if it is listed.

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

    Re: Adding Microsoft Word Object Library

    For future reference, the same is true for other office products as well (Excel, Powerpoint, Access, etc) - they are references as opposed to toolbox components.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    I have attached screen prints to maybe make this a little clearer.

    I have clicked on Add Reference but this still does not show up in my Choose Items For Toolbox option. How do I make it available from there so I can add it to my form?
    Attached Images Attached Images   

  11. #11
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    You aren't understanding. The Toolbox Items dialog chooses components/controls that can be added to a Form or designable Control. You cannot add Word to your Form. Hence, you cannot choose it from the Toolbox Items dialog. The behavior you are seeing is correct.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    OK - What I was trying to do was view a word document inside a form. Is that possible to do?

  13. #13
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    Unfortunately no, you cannot directly embed Word inside your Form. You might be able to simulate it with a bunch of APIs but it's probably not worth it.

    If the user saves the file as a .rtf then you could load the file into a RichTextBox. If that would be acceptable, another you could do is look into converting a .doc file into a .rtf file so that you could load it.

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

    Re: Adding Microsoft Word Object Library

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         System.Diagnostics.Process.Start("d:\worddocs\myDoc.doc")
    3. End Sub
    I needed no component or reference to do that. This doesn't open it in your form, but it does open it from your program.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    Thanks - I had a feeling this was not going to work. I need to open it in a form as it was meant to be a preview screen when they clicked on a file from a Treeview node.

    Thanks anyway.

  16. #16
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Microsoft Word Object Library

    The following code allows a user to select a .doc file, and it then loads that file into a RichTextBox. What it does is opens Word with the .doc file, then saves the file as an RTF file (temp file), then it streams that temp file into the RichTextBox. This is just one way that you could sorta do it. You will need a reference to the Microsoft Word Object Library for this code to work.
    Code:
    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Sub loadButton_Click(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) Handles loadButton.Click
    
            If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim file = Me.OpenFileDialog1.FileName
                Dim application As Word.Application = Nothing
                Dim document As Word.Document = Nothing
                Try
                    Dim tempFile = IO.Path.GetTempFileName()
    
                    '//create the application
                    application = New Word.Application()
                    application.Visible = False
    
                    Try
    
                        '//load the document
                        document = application.Documents.Open(CObj(file))
                        document.SaveAs(CObj(tempFile), Word.WdSaveFormat.wdFormatRTF)
                    Catch ex As COMException
                        MessageBox.Show("Unable to load the specified document.", "Error", _
                                        MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Finally
                        If document IsNot Nothing Then
                            document.Close(False)
                        End If
                        document = Nothing
                    End Try
    
                    Try
                        '//stream the temp file into the RichTextBox
                        Me.RichTextBox1.LoadFile(tempFile, RichTextBoxStreamType.RichText)
    
                        '//delete the temp file, we no longer need it
                        IO.File.Delete(tempFile)
                    Catch ex As IO.IOException
                        MessageBox.Show("Unable to delete or load temp file.", "Error", _
                                        MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
    
                Catch ex As COMException
                    MessageBox.Show("Unable to instantiate Word.", "Error", _
                                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    If application IsNot Nothing Then
                        application.Quit(False)
                    End If
                    application = Nothing
                End Try
    
            End If
    
        End Sub
    
    End Class
    As you can see from the pictures, I successfully loaded a doc file into a RichTextBox. You can also notice that the RichTextBox didn't properly form the table.
    Attached Images Attached Images   

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: Adding Microsoft Word Object Library

    Thank you ForumAccount that looks great but I think this is getting into to deep for me at the moment. I shall store this and look at it later - so a really big thank you.

    Thanks everyone else I will close this off as resolved as I think the original issue was cleared up.

  18. #18
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: [RESOLVED] Adding Microsoft Word Object Library

    Hi!
    I'll post this even though this thread is resolved.

    You actually CAN embed word into your form by using Process.Start("C:\document.doc") and then using P/Invoke to set the parent of the process to your own application like MDI. This actually creates a word window but it creates it inside your own form

Tags for this Thread

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