Delphi Error correction for Excel copy and paste operation

Source: Internet
Author: User

I learn Delphi how to operate the Excel table, just to use the table copy and paste function, and then find blog on the Internet to learn, the results found after finding basically blog is a copy of a, this look I do not say anything, the problem is copied a lot

Was wrong and misled me for two days. Below, I am correct for these errors, I do not guarantee that I am right, full, anyway, I write the following code can be run.

Var  wb1:variant;  Wb2:variant;  Vsheet1, vsheet2:variant;  Range1, Range2:variant;

  

    1. Opening and displaying the table will not say much:
WB1: = Createoleobject (' Excel.Application '); WB2: = Createoleobject (' Excel.Application '); wb1. Workbooks.add;wb2. Workbooks.Open (' D:\Information.xlsx '); wb1. Visible: = true;wb2. Visible: = True;

  

2. Workbook Assignment:

Vsheet1: = wb1. ACTIVEWORKBOOK.SHEETS[1];  The workbook was previously set to the active table Vsheet2: = wb2. activeworkbook.sheets[1];//Vsheet2: = wb2. WORKBOOKS[1].SHEETS[1];  You can do it like this.

  

3. Copy operation:

I think the copy operation is the most important operation, because there are many copy operations on the Internet is wrong, I was just a variety of copy paste blog to mislead.

Vsheet2. Copy (Emptyparam, wb1. ACTIVEWORKBOOK.SHEETS[1]);//Copy to Sheet1 back Vsheet2. Copy (wb2. ACTIVEWORKBOOK.SHEETS[1]); Copy to Sheet1 front
vsheet2.rows[1].copy; Copy the first line, copy will have red error, but can run. vsheet2.range[' A1:a2 '. Copy; Copy a1a2 two vsheet2.UsedRange.Copy; Copy all

  

Note: This is how it is written on the Internet:

Vsheet1. Used.Range.Copy;

  

This is wrong, I was misled by this for a long time, and I search on the Internet, we all write (obviously a copy of a), really no language, write a blog when the trouble to run through a bit can it? Later on, I'm going to make a change to the bug on the Pit Daddy blog below.

vsheet2.range[' A2 '. PasteSpecial; This is paste, PasteSpecial has red error, can run.

  

The following sentence is not possible, to specify where to paste, but the blog on the Internet is written to do so, the pit father.

vsheet2.range.pastespecial;  Error

The above paste is pasted in the same table, if pasted into another table?

vsheet1.range[' A1 '. PasteSpecial;

  

Note that there is a problem with pasting here, copy and paste from table two to form one, copy and paste the past is a picture, but in the same table to do this operation paste is text, so pay attention.

The following is a copy between two tables:

Range2: = vsheet2.range[vsheet2.cells[1,1],vsheet2.cells[1, 2]];//Specifies the copy location range2.select;range2.copy;range1: = Vsheet1.range[vsheet1.cells[1,1],vsheet1.cells[1, 2]];range1.select;vsheet1.paste;                It's changed to Vsheet2 and copied to the original form.

  

This way copy paste past is the text, online those paste method I do not say much, anyway I was they pit for a long time, and incredibly not less than 10 people's blog is wrong.

In addition, these blogs are written in the Internet:

  

I can't execute the above sentence anyway, I don't know how they do it.

Finally: Attach my code. When you write a blog, do not mislead the novice, you attach the code trouble to carry out through the release can it? I was also a novice, so I was misled for a long time.

Unit Unit1; Interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, comobj;  Type TFORM1 = Class (Tform) procedure formcreate (sender:tobject); Private {Private declarations} public {public declarations} end; var Form1:tform1;  Implementation {$R *.DFM} procedure tform1.formcreate (sender:tobject); var wb1:variant;  Wb2:variant;  Vsheet1:variant;  Vsheet2:variant; Range1, Range2:variant;  Begin WB1: = Createoleobject (' Excel.Application ');   WB2: = Createoleobject (' Excel.Application '); Wb1.  Workbooks.Add; Wb2.   Workbooks.Open (' D:\Information.xlsx '); Wb1.  Visible: = True; Wb2.   Visible: = True; Vsheet1: = wb1.  ACTIVEWORKBOOK.SHEETS[1]; Vsheet2: = wb2. activeworkbook.sheets[1];//Vsheet2: = wb2.                        WORKBOOKS[1].SHEETS[1]; The two sentences the same effect {each of the following paragraphs can be run alone, as long as you just slightly modified it, I can not say that I said is all right, but the following code I have been executed in a sentence. }//Vsheet2. Copy (Emptyparam, wb2.      ACTIVEWORKBOOK.SHEETS[1]); Copy to Sheet1 back//Vsheet2. Copy (wb2. ActiveWORKBOOK.SHEETS[1]); Copy to Sheet1 front//wb2.worksheets.rows[1].                                 Copy;                                         This sentence is not enforceable//vsheet2.rows[1].copy; Copy the first line//vsheet2.range[' A1:A2 '].                                  Copy;                                     Copy specified area//vsheet2.usedrange.Copy;                             Copy all//vsheet2.range[' A2 '].pastespecial;  Paste to specified location//range2:= vsheet2.range[vsheet2.cells[1,1],vsheet2.cells[1, 2]];//range2.select;//range2.copy;//range1                    : = vsheet1.range[vsheet1.cells[1,1],vsheet1.cells[1, 2]];//range1.select;//vsheet1.paste;  Copy and paste from Vsheet2 to Vsheet1 in End;end.

  

Delphi Error correction for Excel copy and paste operation

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.