views:

943

answers:

3

I have code that lets me select a single item in arange:

     COleVariant vItems = cstrAddr;
  hr = AutoWrap(
       DISPATCH_PROPERTYGET, 
       &vCell, 
       irange, 
       L"Item", 
       2,
       COleVariant((short)(1)), 
       COleVariant((short)(1)));
  if (FAILED(hr)) return hr;


  // Use the dispatch interface to select the cell
  COleVariant result;
  hr = AutoWrap(
      DISPATCH_METHOD, 
      &result, 
      vCell.pdispVal, 
      L"Select", 
      0);
  if (FAILED(hr)) return hr;

This works fine. However, I need to select all the cells in the range, but I haven't been able to find a way to specify this in the "get" call for the Item property. Tried using -1,-1... tried passing in a pair of bstr in the 2 variants, specifying a colon separated range of columns and a range of rows; also tried passing in a single parameter of a string of range specification. None worked.

Update: I have also tried

hr = iRange->Select(vResult);

This does return S_OK, but it does not select the range. Normally, I can't directly call the functions in the iRange struct; the result is a gpf or access violation -- so I have to use the autowrap function (to drive an Invoke call). I'm not surprised this call doesn't work. Hope I can get this working.... it's the last piece of this project.

+1  A: 

Not familiar with that kind of code (in VB it's much, much easier to do Automation) I think in your example you are selecting one cell from a range using the Item property and the Select method. Correct?

So in VB

Dim oRange as Range
Dim oCell as Range

 Set oRange = WorkSheet.Range("A1:A10") '<-- get range
 Set oCell = oRange.Item(1)             '<-- returns first cell in range
 oCell.Select                           '<-- selects first cell

The problem is Item property only returns one cell - you have to apply the Select method to the original range.

Dim oRange as Range

 Set oRange = WorkSheet.Range("A1:A10") '<-- get range
 oRange.Select                          '<-- Selects the range
DJ
That's what I was originally trying to do. I get a failure when I make that call, however. HRESULT 0x800a03ec. I have a separate question posted here to address that one.
Steve
Also, I saw this page: http://www.nika-soft.com/nativeexcelnet/doc/NativeExcel.IRange.Select.html which hints that from c++, you can specify a range on items. Due to restrictions in my code, I can't use the classes listed there. I have to use the autowrap/invoke style calls.
Steve
Oh well - I guess I can't help you...
DJ
+1  A: 

I found the answer to this question. This only appears to be a problem when used in the DSOFRAMER sample (Microsoft KB 311765). DSOFramer is a general purpose ActiveX control for embedding MS Office documents. The problem also only happens in a debug build; release builds are fine.

I also found a workaround that works on release or debug build: get any cell in the range (using get_Item), then call select on that item, then select again to de-select it. Once that's done, the select can be called on the range. Apparently, select cannot be called on the range if there is a cell already selected (or perhaps if the selection state is undefined).

Steve
A: 

yes do that shit son

mike