C # Copy a table in a Word document and paste it into the next page

Source: Internet
Author: User

C # Copy a table in a Word document and paste it into the next page

object omissing = System.Reflection.Missing.Value;
Microsoft.office.interop.word._application oword;
Microsoft.office.interop.word._document odoc;
oword = new Microsoft.Office.Interop.Word.Application ();
Display Word Documents
oWord.Visible = true;
Get Word file Template
Object fileName = System.Windows.Forms.Application.StartupPath + "\word.doc";
Generate a new document from the template, equivalent to save as
odoc = OWORD.DOCUMENTS.ADD (ref fileName, ref omissing,
ref omissing, ref omissing);

Copy the first table
ODOC.TABLES[1]. Select ();
OWord.Selection.Copy ();

Manipulate the text in the table here
ODOC.TABLES[1]. Cell (1, 1). Range.Text = "This is the first table";

Next page
Object mymissing = System.Reflection.Missing.Value;
Object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
OWord.Selection.EndKey (ref myunit, ref mymissing);
Object pbreak = (int) Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
OWord.Selection.InsertBreak (ref pbreak);

Paste the first table
OWord.Selection.Paste ();

ODOC.TABLES[2]. Cell (1, 1). Range.Text = "This is the second table";

bool SaveChange = false;

C # manipulating Word table operations

object missing = System.Reflection.Missing.Value;

Object template = (object) Templatefilepath;

Object filename = (object) Savefilepath;

Object isVisible = missing;

Object readOnly = missing;

Object breaktype = Word.WdBreakType.wdSectionBreakNextPage;

Object Issavechange = (object) SaveChange;

Word.Document doc = null;

Define a Word.Application Object

Word.Application WordApp = new Word.applicationclass ();

C # Open document for manipulating Word table operations

doc = WordApp.Documents.Open (ref template,

Ref missing,ref Readonly,ref Missing,

Ref missing, ref missing, ref missing,

Ref missing, ref missing,ref missing,

Ref missing, ref isVisible, ref missing,

Ref missing, ref missing, ref missing);

C # manipulating Word Table Actions set Header text

WordApp.ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekCurrentPageHeader;

WordApp.Selection.WholeStory ();

WordApp.Selection.TypeText (This.m_titletext);

WordApp.ActiveWindow.ActivePane.View.SeekView =

Word.WdSeekView.wdSeekMainDocument;

Page Setup, set page to portrait layout, set paper type to A4 paper

Doc. Pagesetup.orientation = Word.WdOrientation.wdOrientLandscape;

Doc. Pagesetup.pagewidth = Wordapp.centimeterstopoints (29.7F);

Doc. Pagesetup.pageheight = Wordapp.centimeterstopoints (21F);

C # manipulating Word table operations create tables and set table and cell properties

Object autofitbehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;

Doc. CONTENT.TABLES.ADD (

WordApp.Selection.Range, TotalCount + 1,

Totalfield-keycount_1, ref missing,

Ref AutoFitBehavior);

C # to manipulate the merge cells of a Word table operation

Doc. CONTENT.TABLES[1]. Cell (I+1,J). Select ();

Object moveunit = Word.WdUnits.wdLine;

Object movecount = 1;

Object moveextend = Word.WdMovementType.wdExtend;

WordApp.Selection.MoveUp (ref moveunit,

Ref movecount, ref moveextend);

WordApp.Selection.Cells.Merge ();

WordApp.Selection.Cells.VerticalAlignment =

Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

Doc. CONTENT.TABLES[1]. Cell (i+1,1). Range.Text = "Cell content fill"

Add a table row

Doc. Content.tables[0]. Rows.Add (ref BeforeRow);

C # Adding table columns for manipulating Word table actions

Doc. Content.tables[0]. Columns.Add (ref BeforeColumn);

Center text

WordApp.Selection.ParagraphFormat.Alignment =

Word.WdParagraphAlignment.wdAlignParagraphCenter;

WordApp.Selection.Cells.VerticalAlignment =

Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

C # action Word table action selected cell text centered vertically

ODoc.Tables.Add (Tablelocation, 3, 4, ref omissing, ref omissing);

Word.table newtable = odoc.tables[1];

Object beforerow = Newtable.rows[1];

NEWTABLE.ROWS.ADD (ref BeforeRow);

Word.cell Cell = Newtable.cell (1, 1);

Cell. Merge (Newtable.cell (1, 2));

C # Copy a table in a Word document and paste it into the next page

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.