I add reference office.dll and Microsoft Office Excel Component to my project and use :
Dim App As Excel.Application, BarName as String
Dim Bar As Microsoft.Office.Core.CommandBar = App.CommandBars(BarName)
Than recieve this error in my task board for the second code line:
+ 'Reference required to assembly 'Interop.Microsoft.Office.Core' containing the type 'Microsoft.Office.Core.CommandBars'. Add one to your project.'
What can I do in order to solve this problem?
01-08-2004, 08:08 AM
Hmmm I'm not 100% sure what is gonig on. You might want to start with a new project, just to get a sense of how to get these references right, and then once we've figured it out, apply those changes to your *real* project.
You should not have to explicitly add a reference to the 'Microsoft.Office.Core', for adding a reference to 'Excel' should take care of that step for you... so I am guessing that your adding the refrerence to the 'office.dll' is interfering?
Here is how I loaded the references in my project and it ran fine. Starting with a brand-new Project, I show the following references in my Solution Explorer:
I then look to add the Microsoft Excel Object Library, choosing Alt|Project|Add Reference... then clicking the COM Tab, and then scrolling down until I find "Microsoft Excel 10.0 Object Library" (which could be 9.0 or some other # for you).
I then click <Select> and then <Ok>.
The following references are added for me:
So you can see that 'Microsoft.Office.Core' is added automatically for you. (Well, it was for me!)
I then ran the following code, without any trouble:Public Class Form1
Dim oExcel As Excel.Application
Dim oCommandBars As Microsoft.Office.Core.CommandBars
Dim oBar As Microsoft.Office.Core.CommandBar
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim sBarName As String
oExcel = New Excel.Application
oCommandBars = oExcel.CommandBars ' <-- *** Critical! ***
sBarName = "Drawing" ' <-- (Change as Needed)
oBar = oCommandBars(sBarName)
oExcel.Visible = True
oBar.Visible = True
Private Sub Form1_Disposed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Disposed
Private Sub NAR(ByVal o As Object)
' This sub cleanly releases the COM Automation Object.
' Source: http://support.microsoft.com/?kbid=317109
o = Nothing
End Class Note some issues in the above:
(1) Note the line that reads: oCommandBars = oExcel.CommandBars ' <-- *** Critical! *** Unfortunately, when using Automation in .Net one cannot directly call oBar = oExcel.CommandBars(sBarName) in one line as you could in VB 6.0 (and, frankly, in virtually every other circumstance in .Net as well). Well, you could, and it will work, but your Excel.Application will never close when you are done (if you check your Task Manager, you'll see it hanging around even after your .Net Application is closed).
You must break up the line as follows:
oCommandBars = oExcel.CommandBars
oBar = oCammandBars(sBarName) And lastly, you must terminate your COM Interop Objects by making use of the NAR() Sub, shown above.
For more info on these complexities, I would see the FAQ on .Net Automation, above: http://www.visualbasicforum.com/t129690.html It's a bit long, but I think (hope!) you'll find it useful.
I hope this helps!