garretth

I just starting to experiment with VS2005 and the VSTO 2005 SE Beta and I've run into a problem I don't seem to be finding the solution to. I'm working in Windows Vista and Office 2007 and have applied the service pack and the Vista update to my VS2005 Professional installation.

What I'm trying to do is determine whether an Excel cell that is double clicked is in a named range. My first experiment was to display the local address in the cell when the cell was double clicked and this worked perfectly. When I try to access the name property, however, I recieve the following error

Exception rethrown at [0]:

at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

at Microsoft.Office.Interop.Excel.Range.get_Name()

at AccountManager.ThisAddIn.Cell_DoubleClick(Range Target, Boolean& Cancel) in C:\Users\Garrett Heil\Documents\Visual Studio 2005\Projects\AccountManager\AccountManager\ThisAddIn.vb:line 46

Private Sub Cell_DoubleClick(ByVal Target As Excel.Range, ByRef Cancel As Boolean) Handles ExcelActiveSheet.BeforeDoubleClick

I obviously read about the localization issues and tried wrapping the returned range but this made no difference. My event code is below and the exception occures at If _tempRange.Name Is Nothing Then

<code>

Private Sub Cell_DoubleClick(ByVal Target As Excel.Range, ByRef Cancel As Boolean) Handles ExcelActiveSheet.BeforeDoubleClick

Dim _tempRange As Excel.Range

_tempRange = CType(Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(GetType(Excel.Range), Target), Excel.Range)

Try

If _tempRange.Name Is Nothing Then

_tempRange.Value = "No Name"

Else

_tempRange.Value = Target.Name

End If

Catch ex As Exception

_tempRange.Value = ex.Message

End Try

<end code>

If anyone has a suggestion, I'd love to hear it; I'm stumped.

Thanx, garretth



Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

garretth

Update

This is evidently something stupid that I'm doing or, at least, it is not a fuction of Windows Vista or Office 2007. I tried the same code on my work PC which is running Windows XP Pro and Office 2003 and I receive exactly the same result.

Anyone

Thanx - garretth





Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

Dennis Wallentin

Garreth,

When double clicking on a cell or when hiting the F2-button we put Excel into Edit Mode. In this mode we can't track anything.






Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

garretth

Thanks,

Not being able to detect or cancel edit mode seems a rather major flaw in the excel class model but it explains the source of my error.

garrett h





Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

Dennis Wallentin

We can't run code in edit mode. If I understand it correctly it's IDispatch that holds all calls until Excel leaves the edit mode. So when You call into Excel's IDispatch interfaces while it's in edit mode then the code simple stops responding.

Excel is per se an end user's tool and as such it serves it purpose well








Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

garretth

I guess I'm still missing something. I changed my event from "BeforeDoubleClick" to "SelectionChanged" and I still recieve the same error. I also tried "BeforeRightClick" with the same effect. Surely simply moving from one cell to another doesn't put Excel into Edit Mode. If so, what events can I respond too.

I also tried an empty project with a bit of debug code

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

' Start of VSTO generated code

Me.Application = CType(Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(GetType(Excel.Application), Me.Application), Excel.Application)

' End of VSTO generated code

Try

Debug.Print(Application.ActiveCell.Name)

Catch ex As Exception

Debug.Print(ex.Message)

End Try

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub

and recieve the same error.

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Exception from HRESULT: 0x800A03EC





Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

Dennis Wallentin

Hi,

Me.Application.ActiveCell.Address is the correct syntax.






Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

garretth

Thanx, Dennis, you are, of course, correct; I got lazy in my frustration. This is not, however, the source of the exceptions.

garrett h





Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

Dennis Wallentin

Hi,

The Name property is read-only and therefore cause the exception.
What is it that You want to achieve






Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

garretth

I'm trying to determine whether a selected cell is in a named range. At least that's what I was trying to do. Now I'm just trying to figure out why I can't read the Name property without throwing exceptions.

Note that I am reading the Name property, not trying to write it. The problem is I've found several other range properties that cause the same behavior and I'm perplexed that others haven't noticed this. I have to believe that I'm doing something fundementally wrong but we're talking about 7 lines of code. How much could I have done Also, I've tried this on two completely different development machines, one with Vista and Office 2007, one with XP Pro and Office 2003 and I get exactly the same behavior.

Note also that I have not installed the original VSTO 2005. I can't on the Vista machine because it does not have Office 2003. Could this be the source of the problem

garrett h





Re: Visual Studio Tools for Office Exception from HRESULT: 0x800A03EC when trying to access Range properties

Dennis Wallentin

Hi,

The name property is only available on worksheet and workbook level but not on cell level.

Anyway, since You want to check if the active cell is in a range or not the following snipped shows how to approach it:

'Using celladdresses.
'Dim TargetRange As Excel.Range = Application.Range("A1:A10")
'Using a predefined range name
Dim TargetRange As Excel.Range = Application.Range("Name_of_the_Range")

If Application.Intersect(Application.ActiveCell, TargetRange) Is Nothing Then
'The active cell is not part of the range
Else
MsgBox(Application.ActiveCell.Address & " is within the target range.")
End If

In order to trig the above code it needs to be placed in an event procedure (either on an application level via for instance the Application_SheetSelectionChange or a workbook level via for instance Sheet1_SelectionChange but we need more info from You to take it a bit closer to the solution.