Title: DDE Between Delphi and Excel
Question: Dynamic Data Exchange(DDE) between Delphi and Excel
Answer:
procedure TForm2.BtnSendClick(Sender: TObject);
var
SendData:Pchar;
RowNo,ColNo:Integer;
ExcelCell,CellText:String;
ExlHandle:THandle;
begin
ExlHandle := FindWindow('XLMAIN','Microsoft Excel - ExcelDDE.xls');
If ExlHandle = 0 Then
ShellExecute(ExlHandle,nil,Pchar('D:\ExcelDDE.xls'),nil,nil,SW_SHOWNORMAL)
Else
Begin
ShowWindow(ExlHandle,SW_MAXIMIZE);
BringWindowToTop(ExlHandle);
DdeClientConv1.ExecuteMacro('[Open("D:\ExcelDDE.xls")]',False);
End;
DdeClientConv1.SetLink('Excel','[ExcelDDE.xls]Sheet1');
DdeClientConv1.OpenLink;
GetMem(SendData,100);
DdeClientConv1.ExecuteMacro('[Run("AddMenu")]',False);
For ColNo := 1 to prfData.ColCount - 1 do
Begin
For RowNo := 1 to prfData.RowCount - 1 do
Begin
CellText := prfData.Cells[ColNo-1,RowNo].Text;
StrPCopy(SendData,CellText);
ExcelCell := 'R' + IntToStr(RowNo) + 'C' + IntToStr(ColNo);
DdeClientConv1.PokeData(ExcelCell,SendData);
End;
End;
DdeClientConv1.CloseLink;