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.
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?
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.
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:
From the Solution Explorer click "Show All Files", then right-click the References node and click Add Reference
From the Menu > Project > Add Reference
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.
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.
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?
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.
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.
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.
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.
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.
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