Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Word Automation in .Net

3 views
Skip to first unread message

Dan Thomas

unread,
Feb 20, 2002, 9:32:01 AM2/20/02
to
I have built a word application in .Net. However, I am unable to build the
project as the compiler complains that the document.Close and
application.Quit commands are ambiguous:

'Close' is ambiguous across the inherited interfaces 'Word._Document' and
'Word.DocumentEvents_Event'.
'Quit' is ambiguous across the inherited interfaces 'Word._Application' and
'Word.ApplicationEvents3_Event'.

What do I need to do to clarify the reference to methods and not the events?

Any help/guidance is appreciated.

Dan Thomas

Joel Alley

unread,
Feb 20, 2002, 12:04:00 PM2/20/02
to
This is a known issue with the runtime callable wrapper (RCW) generated for
the Word type library. A knowledge base article is in process regarding
this problem. Since it hasn't been made public yet, I've included the text
of the article here.

Q315981 - BUG: VB .NET "'[Method]' is ambiguous" Err Msg When Automating
======================================================================
----------------------------------------------------------------------------
---
The information in this article applies to:

- Microsoft Visual Basic.NET (2002)
- Microsoft Word 2002
- Microsoft Excel 2002
- Microsoft Word 2000
- Microsoft Excel 2000
- Microsoft PowerPoint 2002
- Microsoft PowerPoint 2000
----------------------------------------------------------------------------
---

SYMPTOMS
========

When you use Visual Basic .NET to automate a Microsoft Office application,
and
an Automation object has both a method and an event with the same name, you
may
receive the following compile time error message when you build the project:

BC30685: '[Method]' is ambiguous across the inherited interfaces
'[Interface1]' and '[Interface2]'.

For example, Word 2000 and Word 2002 expose both a Quit method and a Quit
event
on the Application object. If your code calls the Quit method, you receive
the
following compile error message:

'Quit' is ambiguous across the inherited interfaces 'Word._Application'
and
'Word.ApplicationEvents3_Event'.

CAUSE
=====

When you import a COM reference into your .NET project, Visual Basic .NET
automatically generates a runtime callable wrapper (RCW) to wrap the
component
and expose it to the Common Language Runtime as a native .NET object. This
object implements the default interface for the COM object, and exposes
functionality for sinking the default events. If the object that is wrapped
has
both a method and an event with the same name, the default RCW sets up
conflicting names, and Visual Basic .NET is unsure which interface member
your
code is attempting to access.

RESOLUTION
==========

To resolve this problem, you can use one of the following methods:

- Explicitly cast the object to the correct interface before you call the
Quit
method. For example, you can use the following:

CType(oWordApp, Word._Application).Quit()

-or-

- Change the variable declaration to use the extended Class wrapper object
for
the object. For example, instead of using Word.Application, change the
declaration to resemble the following:

Dim oWordApp As New Word.ApplicationClass()

STATUS
======

Microsoft is researching this problem and will post more information here
in the
Microsoft Knowledge Base when it becomes available.

MORE INFORMATION
================

Office applications are not the only COM servers that may exhibit this
problem
when they are automated in Visual Basic .NET. The problem is not with the
COM
server or Visual Basic .NET itself, but with the RCW that the .NET Framework
generates. By default, the type library importer for .NET does not resolve
conflicting names.

The following Office Automation objects and methods may generate this
compile
error message if a standard RCW is generated for your Visual Basic .NET
project:

+-----------------+-------------------------+-------------------------------
+
| Method | Object Interface | RWC Event Interface
|
+-----------------+-------------------------+-------------------------------
+
| NewWorkbook | Excel._Application | Excel.AppEvents_Event
|
| Activate | Excel._Workbook | Excel.WorkbookEvents_Event
|
| Activate | Excel._Worksheet | Excel.DocEvents_Event
|
| Calculate | Excel._Worksheet | Excel.DocEvents_Event
|
| Activate | Excel._Chart | Excel.ChartEvents_Event
|
| Select | Excel._Chart | Excel.ChartEvents_Event
|
| NewDocument | Word._Application | Word.ApplicationEvents3_Event
|
| Quit | Word._Application | Word.ApplicationEvents3_Event
|
| Close | Word._Document | Word.DocumentEvents_Event
|
| NewPresentation | PowerPoint._Application | PowerPoint.EApplication_Event
|
+-----------------+-------------------------+-------------------------------
+

Steps to Reproduce Behavior
---------------------------

1. Start Visual Studio .NET and create a new Visual Basic Windows
Application
project named WordQuitTest. Form1 is created by default.

2. On the Project menu, click Add Reference. In the Reference dialog box,
click
the COM tab, double-click Microsoft Word 10.0 Object Library in the
list, and
then click OK to have Visual Basic .NET create a wrapper and add it to
your
project.

3. Use the Toolbox to add a button to Form1, and then double-click the
button to
open the code window for Form1.

4. Paste the following code in the button click method in Form1.vb:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim oWordApp As New Word.Application()
oWordApp.Visible = True
MsgBox("App Name = " & oWordApp.Name)
oWordApp.Quit()

End Sub

5. On the Build menu, click Build Solution to build the project. You
receive a
compile error message.
============================================================================
====

Thanks,
Joel Alley

Of course, this posting wouldn’t be complete without a nice, juicy
disclaimer from our lawyers: This posting is provided “AS IS” with no
warranties, and confers no rights. You assume all risk for your use. © 2001
Microsoft Corporation. All rights reserved.

Dan Thomas

unread,
Feb 21, 2002, 7:49:05 AM2/21/02
to
Thanks Joel. Dan


"Joel Alley" <NO_joel...@microsoft.com> wrote in message
news:bLH4nCjuBHA.1752@cpmsftngxa07...

0 new messages