2016-12-16 32 views
1

Delphi Seattle、Excel 2013。セルの背景をグラデーションに設定する必要があります。単色の場合は背景色を設定できますが、グラデーションの構文を正しく取得することはできません。課題の一部は、セルの勾配がIDispatchであることです。次のコードは、単一の背景色を設定します。Delphi - Excelセルの背景色グラデーションを設定する

procedure TForm1.GradientTestClick(Sender: TObject); 
var 
    oExcel : ExcelApplication; 
    RawDataSheet :_Worksheet; 
    ThisCell : ExcelRange; 
begin 
    oExcel := CreateOleObject('Excel.Application') as ExcelApplication; 
    oExcel.Visible[LOCALE_USER_DEFAULT] := True; 

    // Add a New Workbook, with a single sheet 
    oExcel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT); 
    // Get the handle to the active Sheet, and insert some dummy data 
    RawDataSheet := oExcel.ActiveSheet as _Worksheet; 
    ThisCell := RawDataSheet.Range['A1', EmptyParam]; 
    ThisCell.Value2 := 10; 


    // To set ONE Color 
    ThisCell.Interior.Pattern := xlSolid; 
    ThisCell.Interior.ColorIndex := 3; 

    // To Set Gradient... 


end; 

私は(リニア、2色、黄色に緑)したいグラジエント型を設定するExcelマクロを記録すると、マクロは、私はDelphiである行うことができるはず何

Sub Macro1() 
' 
' Macro1 Macro 
' 

' 
    With Selection.Interior 
     .Pattern = xlPatternLinearGradient 
     .Gradient.Degree = 0 
     .Gradient.ColorStops.Clear 
    End With 
    With Selection.Interior.Gradient.ColorStops.Add(0) 
     .Color = 5296274 
     .TintAndShade = 0 
    End With 
    With Selection.Interior.Gradient.ColorStops.Add(1) 
     .Color = 65535 
     .TintAndShade = 0 
    End With 
End Sub 

です。 ..

ThisCell.Interior.Pattern := xlPatternLinearGradient; 
    ThisCell.Interior.Gradient.Degree := 0; 
    ThisCell.Interior.Gradient.ColorStops.Clear; 
    ThisCell.Interior.Gradient.ColorStops.Add[0].Color := 5296274; 
    ThisCell.Interior.Gradient.ColorStops.Add[1].Color := 65535; 

私の挑戦は、ThisCell.Interior.GradientがIDispatchであることです。 DegreeやColorstopsなどの他の「サブプロパティ」を設定するにはどうすればよいですか?

ありがとう

+1

OleVariantに.Gradientを割り当てて、それに遅延呼び出しを使用してみましたか?例えば。 'vGradient.Degree:= 0' – MartynA

答えて

2

遅延バインディングを使用して、IDispatchインターフェイスのメソッド/プロパティにアクセスします。

... 
    Gradient: OleVariant; 
begin 
    .... 

    // To Set Gradient... 

    ThisCell.Interior.Pattern := xlPatternLinearGradient; 
    Gradient := ThisCell.Interior.Gradient; 
    Gradient.Degree := 45; 
    Gradient.ColorStops.Clear; 
    Gradient.ColorStops.Add(0).Color := 5296274; 
    Gradient.ColorStops.Add(1).Color := 65535; 
+0

それはそれです。私は後半のバインディングを使わなければならなかった。 – user1009073

関連する問題