.
Excel 의 VBA를 실행시키는 함수 입니다.
이 함수를 사용하기 위해서는 ExcelApp : TExcelApplication 를 적당한 곳에
올려 놓고 Connection을 맺어야 합니다.
procedure RunVBAScript( psProcName, psScript : String );
begin
try
with ExcelApp do
begin
with VBE.CodePanes.Item( 1 ).CodeModule do
begin
DeleteLines( 1, CountOfLines );
AddFromString( WideString( psScript ) );
end;
Run( psProcName );
with VBE.CodePanes.Item( 1 ).CodeModule do
DeleteLines( 1, CountOfLines );
end;
except
end;
end;
특정 Cell 영역의 색깔을 바꾸기 위해서는
procedure TForm1.ChangeCellColor;
var
lsVBA : String;
begin
lsVBA := 'Sub ChangeCellColor() ' +
' Range("A1:A4").Select ' +
' With Selection.Interior ' +
' .ColorIndex = 4 ' +
' .Pattern = xlSolid ' +
' .PatternColorIndex = xlAutomatic ' +
' End With ' +
' Range("B5").Select ' +
'End Sub ';
RunVBAScript( 'ChangeColor', lsVBA );
end;
와 같이 해주면 됩니다.
.
|