How work with Word and Excel in Delphi?

喜夏-厌秋 提交于 2019-12-24 19:17:58

问题


I use Delphi X10 and Word 2016 64-bit on Windows 10 64-bit. I always used ComObj. for example:

uses ComObj;


procedure TForm1.RzBitBtn1Click(Sender: TObject);
var Excel: variant;  i, j: word;
begin
  Excel := CreateOleObject('Excel.Application');
  Excel.Workbooks.Open('file.xls');
  for i := 1 to 5 do
  for j := 1 to 5 do
  StringGrid1.Cells[j, i] := Excel.Sheets[1].Cells[i, j].Text;
end;

But, i want to use autocompleate for Excel methods. I read what i should import type library, but i cant find type library file in my system.


回答1:


This is quite easy to do, as long as the type libraries for Excel anmd Word are correctly registered.

Go to the OCX\Servers folder under your Delphi install and find Word2010.Pas. Add it to a new VCL project, and USE it in the main form's unit. Declare a form variable A: WordApplication on your main form.

With that done, you should be able to go to Form1's Form.Create, type

A.

and autocomplete should offer you the possibilities for WordApplication's methods and properties.

Now, open Word2010.Pas and find the section

// File generated on 2/20/2013 5:56:45 PM from Type Library described below.

// ************************************************************************  //
// Type Lib: c:\Program Files\Microsoft Office\Office14\msword.olb (1)
// LIBID: {00020905-0000-0000-C000-000000000046}

That tells you the name and location of the type library which was used to generate Word2010.Pas.

Now, unless you need to use some method/property that has been added to Word/Excel since the 2010 version, Word2010.Pas and Excel2010.Pas may be all you need. However, you can import the type library of a more recent version using Delphi and have it generate an import unit for it. Exactly how to do that depends on your Delphi version, but for Delphi Seattle. you simply go to Component | Import Component from the IDE main menu, then select Import type library in the pop-up and follow the wizard's prompts.

Once you've generated the import unit, obviously you simply USE it in your project instead of MSWord2010.

Btw, strictly speaking, there is no "Delphi X10", recent versions (since XE8) have been Seattle, Berlin and Tokyo.



来源:https://stackoverflow.com/questions/44216993/how-work-with-word-and-excel-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!