2

我有代码可以让我在一个范围内选择一个项目:

        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;

这工作正常。但是,我需要选择范围内的所有单元格,但我无法找到一种方法来在 Item 属性的“get”调用中指定它。尝试使用 -1,-1... 尝试在 2 个变体中传入一对 bstr,指定以冒号分隔的列范围和行范围;还尝试传入范围规范字符串的单个参数。没有工作。

更新:我也试过

hr = iRange->Select(vResult);

这确实返回 S_OK,但它不选择范围。通常,我不能直接调用 iRange 结构中的函数;结果是 gpf 或访问冲突——所以我必须使用 autowrap 函数(来驱动 Invoke 调用)。我对这个电话不起作用并不感到惊讶。希望我能得到这个工作......这是这个项目的最后一部分。

4

2 回答 2

1

不熟悉那种代码(在 VB 中,自动化要容易得多) 我认为在您的示例中,您正在使用 Item 属性和 Select 方法从范围中选择一个单元格。正确的?

所以在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

问题是 Item 属性只返回一个单元格 - 您必须将 Select 方法应用于原始范围。

Dim oRange as Range

 Set oRange = WorkSheet.Range("A1:A10") '<-- get range
 oRange.Select                          '<-- Selects the range
于 2008-11-07T20:25:01.843 回答
1

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

于 2008-11-14T19:20:43.693 回答