Experience in developing Office programs in C++builder 6 (i)

Source: Internet
Author: User

First, with the control or with oleautomation?

This question should be said to be very common. I also stick to my proposition whenever possible: the server control group provided with BCB 6. If you are developing with Delphi version 6/7, then use Delphi to provide the server control group.

What's the benefit of doing that? I personally think there are at least two:

First, to maintain the structural +oo of the program design style. For example:

ExcelApplication1->set_DisplayAlerts(0,false);
ExcelApplication1->Quit();

Another example:

int SheetCount=ExcelWorkbook1->Worksheets->Count;

The code is still more intuitive. And it has the beauty of OO.

Second, strong type checking is better than weak type checking.

If you use the Olegetproperty or Olesetproperty function, there is no way to control the arguments passed to these two functions. For example, I can simply set a cell's property foo to 100. However, such a function call is bound to make an error during the run time. Using the server control does not have this problem--the compilation period cannot pass. Errors that can be found and corrected at compile time should not be left to the runtime to resolve. This is my proposition.

Ii. necessary means of assistance

It's tedious to write programs that control Excel using BCB. Because sometimes when the compiler passed, there will be difficult to ponder the exception! There is a very helpless feeling: BCB on the above help is simply BS. The speed of codeinsight is quite slow and unbearable!

However, we should not give up lightly. According to my experience, if you want to master Excel programming under BCB, you must master three ways to get help. These three ways to get help are (in descending order of my personal preference):

Macro command for Excel itself. My habit now is that if I want to implement a feature in BCB, but don't know the appropriate method and parameters, I'll open Excel, record what I want to do with the macro in any sheet, and then study the macro code. This will certainly get the correct method name, but it is not guaranteed for the number and type of parameters: This is because VBA can choose not to pass some default parameters. According to my personal experience, the effectiveness of this should be around 80%, the simplicity of 100%.

Office-brought VBA help. This help is typically not installed by default during Office Setup. For users in the Chinese version of Office 2003, you should check for similar vbaxxnn.chm files under C:\Program Files\Microsoft Office\office11\2052\. where xx should be one of the following strings: AC (Access), GR (Microsoft Graphics), of (office), OL (Outlook), OWS (Office Web Service), PB (Publisher), PP (PowerPoint), WD (Word), XL (Excel). And nn is the version number of the corresponding Office application. If you do not have these files, run Office Setup and install them in your custom installation. These files are useful for understanding the DOM model in Office applications. Its validity is 85%, the simplicity is about 80%.

The header file about the server in BCB6. The location of these header files should be in: C:\Program files\borland\cbuilder6\include\vcl\. I only cut a picture here. From this picture, we can also see that BCB6 support for Office only to Office 2000. Delphi 7 can support Office XP. But they all work well under Office 2003. Since Delphi 8, we should use interop to manipulate office. The effectiveness of this method is 100%--because you can certainly find BCB support in it, but simplicity only 10%--because these headers are quite large: the Excel_2k.h size is 8m+!

These three tools should be instrumental to each other in order to find the right method in a shorter time. And the methods and parameters found in these three ways should be corresponding and consistent. For example, a move method that moves one sheet to another sheet is defined in the above three help routes as follows:

In Excel's VBA macro: Sheets ("Sheet2"). Move after:=sheets (3)

VBA Help for Excel: see figure below

BCB Excel header file:

// *********************************************************************//
Interface:iworksheets
Flags: (4112) Hidden dispatchable
GUID: {000208b1-0001-0000-c000-000000000046}
// *********************************************************************//
Interface Iworksheets:public IDispatch
{
Public
... ...
HRESULT stdmethodcalltype Copy (VARIANT before/*[in,opt]*/= tnoparam (),
VARIANT after/*[in,opt]*/= Tnoparam (),
... ...
HRESULT stdmethodcalltype Move (VARIANT before/*[in,opt]*/= tnoparam (),
VARIANT after/*[in,opt]*/= Tnoparam (),
Long lcid/*[in]*/= tdeflcid ()); [637]
... ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.