Error Using Office
Error Using Office
Error Using Office
Error Using Office
Error Using Office
Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office
Error Using Office Error Using Office
Error Using Office
Go Back  Xtreme Visual Basic Talk > > > Error Using Office


Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2004, 04:40 AM
ns2k ns2k is offline
Newcomer
 
Join Date: Jan 2004
Posts: 5
Default Error Using Office


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?
Reply With Quote
  #2  
Old 01-08-2004, 09:08 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

ns2K,

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:

System
System.Data
System.Drawing
System.Windows.Forms
System.XML

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:

Excel
Microsoft.Office.Core
VBIDE
stdole

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:
Code:
Public Class Form1 Inherits System.Windows.Forms.Form 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 End Sub Private Sub Form1_Disposed(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Disposed oExcel.Quit() NAR(oExcel) NAR(oBar) End Sub Private Sub NAR(ByVal o As Object) ' This sub cleanly releases the COM Automation Object. ' Source: http://support.microsoft.com/?kbid=317109 Try System.Runtime.InteropServices.Marshal.ReleaseComObject(o) Catch Finally o = Nothing End Try End Sub End Class
Note some issues in the above:

(1) Note the line that reads:
Code:
oCommandBars = oExcel.CommandBars ' <-- *** Critical! ***
Unfortunately, when using Automation in .Net one cannot directly call
Code:
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:
Code:
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.xtremevbtalk.com/t129690.html It's a bit long, but I think (hope!) you'll find it useful.

I hope this helps!

Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MsFlexGrid & VBA msl Word, PowerPoint, Outlook, and Other Office Products 2 09-17-2006 04:21 PM
Office Addin Error Handling viper2843 Word, PowerPoint, Outlook, and Other Office Products 4 11-28-2003 01:53 PM
save as with vba office 97 standar ngm Excel 1 11-24-2003 11:50 PM
How to install/reference Office 10.0 object library mysak Word, PowerPoint, Outlook, and Other Office Products 2 10-17-2003 01:57 PM
Handling all Office version... sethindeed General 5 10-09-2001 01:43 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Error Using Office
Error Using Office
Error Using Office Error Using Office
Error Using Office
Error Using Office
Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office Error Using Office
Error Using Office
Error Using Office
 
Error Using Office
Error Using Office
 
-->