Steve
Steve

Reputation: 995

Excel automation. Need to select multiple items from a Range

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.

Upvotes: 2

Views: 1845

Answers (2)

Steve
Steve

Reputation: 995

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).

Upvotes: 1

DJ.
DJ.

Reputation: 16247

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

Upvotes: 1

Related Questions