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!
Printable View
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!
You might try browsing to the OLB file manually:
You would place your office version for OFFICE11. Although, the issue suggests that the application is not installed correctly.Code:C:\Program Files (x86)\Microsoft Office\OFFICE11\MSWORD.OLB
Rather than a component, check the references "Add Reference" You should find the library there.
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?
You should try adding it through references before reinstalling.
Then if it works, you simply load the object at runtime instead of design time :)
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.
OK - What I was trying to do was view a word document inside a form. Is that possible to do?
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.
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.vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click System.Diagnostics.Process.Start("d:\worddocs\myDoc.doc") End Sub
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.
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.
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.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
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.
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 :)