我在使用Excel 2010 的Early绑定从Delphi XE2设置条件格式时遇到问题
我试图复制的宏如下:
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=6"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
尽管我可能会尝试,但我似乎无法访问等效的Selction.FormatConditions(1)
来工作
我最接近的是以下代码:
XR := Xlapp.Range(...)
XR.FormatConditions.Delete;
XR.FormatConditions.Add(xlCellValue, xlGreater, '=6', EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
这是有效的。当我尝试定义颜色时,我遇到了问题
FC := XR.FormatConditions[1];
FC.SetFirstPriority;
with FC.Interior do
begin
PatternColorIndex := xlAutomatic;
ThemeColor := xlThemeColorAccent6;
end;
然而,这一直告诉我XR.FormatConditions(1)是和IDispatch,因此与FormatCondition分配不兼容
我做错了什么?
您需要将Selection
用作ExcelRange
。ExcelXP还要求第二个和第三个参数为OleVariant
,所以这应该可以工作(无论如何,它都会编译):
var
Sel: ExcelRange;
Op, Formula: OleVariant;
Condition: FormatCondition;
begin
Sel := ExcelApplication1.Selection[1] as ExcelRange;
Op := xlGreater;
Formula := '=6';
Sel.FormatConditions.Add(xlCellValue, Op, Formula, EmptyParam);
Condition := Sel.FormatConditions[1] as FormatCondition;
Condition.Interior.PatternColorIndex := xlAutomatic;
// Do whatever else
end;