我们都知道,Office是基于COM组件技术开发的,属于非托管程序,然而C#使用的都是托管程序,那么如何使用非托管的COM组件就是我们操作WORD的第一个问题。所幸的是,.NET FRAMEWORK提供了一种不同种类类库的转换工具tlbimp,exe,通过这个转换工具,我们可以把COM组件转化为.NET FRAMEWORK可以直接调用的DLL文件。

接下来就是转化工作了,Office组件都可以在C:Program FilesMicrosoft Office目录下找到,当然安装的Office版本不同,子目录是不一样的。笔者使用的是Office 2007,可以在C:Program FilesMicrosoft OfficeOffice12目录下找到MSWORD.OLB,这个是WORD组件的类库文件,还可以找到MSACC.OLB操作ACCESS,MSPPT.OLB操作PPT,XL5CHS32.OLB操作EXCEL。载入不同的组件就可以完成对不同Office组件的操作。使用tlbimp,exe工具转化MSWORD.OLB文件后可以得到三个DLL文件,Office,dll,Visual BasicIDE.dll,Word.dll。最后在编译文件的时候,记得将这三个DLL文件载入,命令如下:

csc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:office.dll /r:vbide.dll /r:word.dll word.cs

笔者使用Visual Studio.NET 2005编译环境,通过IDE提供的功能可以大大简化我们对组件转化的工作,并且在编译时也不需要输入那么繁杂的语句,非常方便了。下面介绍一下IDE载入Office组件的方式。

在菜单栏选择“项目”-“添加引用”,弹出的窗口中我们可以选择“COM”选项卡,找到Microsoft Office 12.0 Object Library(Office 2003/2007需要使用12.0版的,如果你使用的是Office 2000或者更低的版本,只要载入10.0版的就可以了),确定后引入.也可以在“浏览”选项卡下找到我们上面提到的MSWORD.OLB文件,引入即可。引入后我们可以发现在解决方案中,引用目录下多了三个文件Microsoft.Office.Core,Microsoft.Office.Interop.Word,VSIDE。这说明引用文件成功,之后在编译程序的时候,在Debug目录下会生成两个DLL文件,Interop.Microsoft.Office.Core.dll,Interop.Microsoft.Office.Interop.Word.dll。完成组件的引入,下面就可以开始程序设计了。

先看一下Word对像模型

Application :用来表现WORD应用程序,包含其它所有对象。他的成员经常应用于整个WORD,你可以用它的属性和方法控制WORD环境。

Document :Document对象是WORD编程的核心。当你打开一个已有的文档或创建一个新的文档时,就创建了一个新的Document对象, 新创建的Document将会被添加到Word Documents Collection。

Selection :Selection对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。

Rang :是Document的连续部分,根据起始字符和结束字符定义位置。

Bookmark:类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存。

在编程中使用到的代码如下,注释比较详细,这里就不再具体的说明。

//Word程序对象

private Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

//Word文档对象

private Microsoft.Office.Interop.Word._Document aDoc;

private void openfile_Click(object sender, EventArgs e)

{//打开Word文件

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

//定义打开文件的16个参数

object fileName = openFileDialog.FileName; //文件名称

object ConfirmConversions = false; //允许转换

object ReadOnly = false; //只读方式打开

object AddToRecentFiles = false; //添加到最近打开的文档

object PasswordDocument = System.Type.Missing;

object PasswordTemplate = System.Type.Missing;

object Revert = System.Type.Missing;

object WritePasswordDocument = System.Type.Missing;

object WritePasswordTemplate = System.Type.Missing;

object Format = System.Type.Missing; //格式

object Encoding = System.Type.Missing; //编码

object Visible = System.Type.Missing;

object OpenAndRepair = System.Type.Missing;

object DocumentDirection = System.Type.Missing;

object NoEncodingDialog = System.Type.Missing;

object XMLTransform = System.Type.Missing;

WordApp.Visible = true;

try

{

//打开文档

aDoc = WordApp.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles,

ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,

ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);

//激活文档,使文档为当前处理

aDoc.Activate();

}

catch

{

MessageBox.Show("出现错误!");

}

}

}

private void closefile_Click(object sender, EventArgs e)

{//关闭Word文件

object SaveChanges = false; //保存更改

object OriginalFormat = System.Type.Missing;

object RouteDocument = System.Type.Missing;

//关闭文档

aDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

//退出程序

WordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

}

通过文档类对象aDoc还可以完成文件的保存,另存为等等操作,详细的可以参阅MSDN。

用C#动态生成Word文档并将数据填入Word表格中电脑知识 2009-07-09 16:37:03 阅读91 评论0 字号:大中小
 
刚刚实现了个功能:用C#实现动态生成Word文档,在Word文档中插入表格,并将读出的数据填入到表格中。
要使用C#操作word,首先要添加引用:

1、添加引用->COM->Microsoft Word 11.0 Object Library

2、在.cs文件中添加

using Word;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)

public string CreateWordFile(string CheckedInfo)
         ...{
             string message = "";
             try
             ...{
                 Object Nothing = System.Reflection.Missing.Value;
                 Directory.CreateDirectory("C:/CNSI");   //创建文件所在目录
                 string + DateTime.Now.ToShortString()+".doc";
                 object filename = "C://CNSI//" + name;   //文件保存路径
                 //创建Word文档
                 Word.Application WordApp = new Word.ApplicationClass();
                 Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//添加页眉
                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                 WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

//移动焦点并换行
                 object count = 14;
                 object WdLine = Word.WdUnits.wdLine;//换一行;
                  WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                  WordApp.Selection.TypeParagraph();//插入段落

//文档中创建表格
                  Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
                  //设置表格样式
                  newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                  newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                  newTable.Columns[1].Width = 100f;
                  newTable.Columns[2].Width = 220f;
                  newTable.Columns[3].Width = 105f;

//填充表格内容
                  newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                  newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                  //合并单元格
                  newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                  WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                  WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

//填充表格内容
                  newTable.Cell(2, 1).Range.Text = "产品基本信息";
                  newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                  //合并单元格
                  newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                  WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//填充表格内容
                   newTable.Cell(3, 1).Range.Text = "品牌名称:";
                   newTable.Cell(3, 2).Range.Text = BrandName;
                   //纵向合并单元格
                   newTable.Cell(3, 3).Select();//选中一行
                   object moveUnit = Word.WdUnits.wdLine;
                   object moveCount = 5;
                   object moveExtend = Word.WdMovementType.wdExtend;
                    WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                    WordApp.Selection.Cells.Merge();
                    //插入图片
                    string FileName = Picture;//图片所在路径
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    object Anchor = WordDoc.Application.Selection.Range;
                    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                     WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                     WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                     //将图片设置为四周环绕型
                     Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                     s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;

newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                     newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                      //在表格中增加行
                      WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                      WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

//文件保存
                     WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                     WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                     WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                     message=name+"文档生成成功,以保存到C:\CNSI\下";
             }
             catch
             ...{
                 message = "文件导出异常!";
             }
             return message;
         }

使用C#读取Word表格数据2009年12月29日 星期二 下午 07:21
  读取Word表格数据的方法

1//将读取Word表格封装与方法中。
2public string ReadWord(string fileName, int rowIndex, int colIndex)
3{
4  ApplicationClass cls = null;
5  Document doc = null;
6
7  Table table = null;
8  object missing = Missing.Value;
9
10  object path = fileName;
11  cls = new ApplicationClass();
12
13  try
14  {
15    doc = cls.Documents.Open
16      (ref path, ref missing, ref missing, ref missing,
17      ref missing, ref missing, ref missing, ref missing,
18      ref missing, ref missing, ref missing, ref missing,
19      ref missing, ref missing, ref missing, ref missing);
20    table = doc.Tables[1];
21    string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();
22    text = text.Substring(0, text.Length - 2);  //去除尾部的mark
23    return text;
24  }
25  catch (Exception ex)
26  {
27
28    return ex.Message;
29  }
30  finally
31  {
32    if (doc != null)
33      doc.Close(ref missing, ref missing, ref missing);
34    cls.Quit(ref missing, ref missing, ref missing);
35  }
36}
这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。

============================================

  由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方:

class WordTableRead
2{
3  private string fileName;
4  private ApplicationClass cls = null;
5  private Document doc = null;
6  private Table table = null;
7  private object missing = Missing.Value;
8  //Word是否处于打开状态
9  private bool openState;
10
11
12  /** <summary>
13  /// 自定义构造方法
14  /// </summary>
15  /// <param name="fileName">包含路径的文件名</param>
16  public WordTableRead(string fileName)
17  {
18    this.fileName = fileName;
19  }
20  
21  /** <summary>
22  /// 打开Word文档
23  /// </summary>
24  public void Open()
25  {
26    object path = fileName;
27    cls = new ApplicationClass();
28    try
29    {
30      doc = cls.Documents.Open
31        (ref path, ref missing, ref missing, ref missing,
32        ref missing, ref missing, ref missing, ref missing,
33        ref missing, ref missing, ref missing, ref missing,
34        ref missing, ref missing, ref missing, ref missing);
35      openState = true;
36    }
37    catch
38    {
39      openState = false;
40    }
41  }
42  
43  /** <summary>
44  /// 返回指定单元格中的数据
45  /// </summary>
46  /// <param name="tableIndex">表格号</param>
47  /// <param name="rowIndex">行号</param>
48  /// <param name="colIndex">列号</param>
49  /// <returns>单元格中的数据</returns>
50  public string ReadWord(int tableIndex, int rowIndex, int colIndex)
51  {
52    //Give the value to the tow Int32 params.
53
54    try
55    {
56      if (openState == true)
57      {
58        table = doc.Tables[tableIndex];
59        string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();
60        text = text.Substring(0, text.Length - 2);  //去除尾部的mark
61        return text;
62      }
63      else
64      {
65        return "";
66      }
67    }
68    catch
69    {
70      return "Error";
71    }
72  }
73
74  /** <summary>
75  /// 关闭Word文档
76  /// </summary>
77  public void Close()
78  {
79    if (openState == true)
80    {
81      if (doc != null)
82        doc.Close(ref missing, ref missing, ref missing);
83      cls.Quit(ref missing, ref missing, ref missing);
84    }
85  }
86}

using Word;
运行程序通过,搞定.

【摘自】  http://tmsoft.lsxy.com/trackback.php?tbID=334&extra=2aa67d  周老师科研站

oMissing);

object start = 0;
            object end = 0;
            Word.Range tableLocation = oDoc.Range(ref start, ref end);
            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));

object Rownum = 2;
            object Columnnum = 2;
            cell.Split(ref Rownum, ref  Columnnum);

前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

创建新Word
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
打开文档:
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            object fileName = @"E:CCCXCXXTestDoc.doc";
            oDoc = oWord.Documents.Open(ref fileName,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            object fileName = @"E:XXXCCXTest.doc";
            oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
                            ref oMissing, ref oMissing);

.添加新表
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

object start = 0;
            object end = 0;
            Word.Range tableLocation = oDoc.Range(ref start, ref end);
            oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
.表插入行
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

object start = 0;
            object end = 0;
            Word.Range tableLocation = oDoc.Range(ref start, ref end);
            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);
.单元格合并
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

object start = 0;
            object end = 0;
            Word.Range tableLocation = oDoc.Range(ref start, ref end);
            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));
.单元格分离
            object oMissing = System.Reflection.Missing.Value;
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref
通过段落控制插入
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */

//Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
            Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop;

namespace DealDoc
{
    public partial class Form1 : Form
    {
        string docContent = "";

public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {     
           
        }

private void btnOpen_Click(object sender, EventArgs e)
        {
            //if (folderBrowserDialog1.ShowDialog() == DialogResult.OK && folderBrowserDialog1.SelectedPath != "")
            if(openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
            {
                this.textBox1.Text = openFileDialog1.FileName;
                docContent = Doc2Text(this.textBox1.Text);
                this.richTextBox1.Text = docContent;
                this.richTextBox1.Enabled = false;
            }

}

//编辑
        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Enabled = true;
            this.richTextBox1.Focus();
        }
       
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
            saveFileDialog1.Filter = "Doc文件(*.doc)|*.doc|Txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog1.Title = "保存文件";
            //StreamWriter   myStream   =   new   StreamWriter(saveFileDialog1.FileName);  
            if ((saveFileDialog1.ShowDialog()) == DialogResult.OK)
            {
                if (saveFileDialog1.FileName != null)
                {
                    //myStream.Write(this.richTextBox1.Text);  
                    //myStream.Close();  
                    this.richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                }
            }
        }

//删除
        private void btnDel_Click(object sender, EventArgs e)
        {
           
        }

//关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

//获得word文本
        public string Doc2Text(string docFileName)
        {
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

//Normal.dot 模板使用
            wordApp.NormalTemplate.Saved = true;

object fileobj = docFileName;
            object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                );
         
            string outText = doc.Content.Text;

doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return outText;

///关闭Word时调用
            //object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            //object missing = System.Reflection.Missing.Value;
            //wordApp.Quit(ref saveOption, ref missing, ref missing);
        }

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //垃圾处理
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

********************************************************************************

详细的:将 Word 用作自动化服务器时提示保存 Normal.dot

http://support.microsoft.com/kb/285885/zh-cn

同时自动化多个 Microsoft Word 实例时,用户可能收到下面的一个或多个警告:
“Normal.dot was being edited by another Word session.If you save this document with the original name, you will overwrite any changes made in the other session.Do you want to save the document using the original name anyway?”
- 或 -
This file is in use by another application or user.(C:\Documents and Settings\...\Normal.dot)
如果对 Normal.dot 模板进行了更改,就可能会出现这些警告。
回到顶端
解决方案
要解决此问题,请执行下列操作之一: 在退出 Word 或将控制权移交给用户之前,应将 Normal.dot 模板的 Saved 属性设置为 True,如下所示:...
< type="text/javascript">

要解决此问题,请执行下列操作之一:
在退出 Word 或将控制权移交给用户之前,应将 Normal.dot 模板的 Saved 属性设置为 True,如下所示:
Application.NormalTemplate.Saved = True
     - 或 -
设置 Quit 方法的 SaveChanges 参数,如下所示:
Application.Quit SaveChanges:=wdDoNotSaveChanges
     回到顶端
更多信息
重现此问题的步骤 在 Visual Basic 中,新建一个标准 EXE 项目。默认情况下会创建 Form1。 在“项目”菜单上,单击“引用”,然后添加一个指向...
< type="text/javascript">

重现此问题的步骤
< type="text/javascript">
在 Visual Basic 中,新建一个标准 EXE 项目。默认情况下会创建 Form1。
在“项目”菜单上,单击“引用”,然后添加一个指向 Microsoft Word 对象库版本的引用。
向 Form1 中添加一个 CommandButton 控件。
向窗体中添加以下代码:
Private Sub Command1_Click()
    Dim wdApp1 As Word.Application
    Dim wdApp2 As Word.Application
   
    Set wdApp1 = CreateObject("Word.Application")
    wdApp1.Visible = True
    wdApp1.Documents.Add
   
    Set wdApp2 = CreateObject("Word.Application")
    wdApp2.Visible = True
    wdApp2.Documents.Add
               
    MsgBox "Change the default font of document 2."
   
    wdApp2.ActiveDocument.Close False
    wdApp2.Quit
    Set wdApp2 = Nothing

wdApp1.Quit
    Set wdApp1 = Nothing
End Sub
     运行该 Visual Basic 项目并单击命令按钮。
将出现一个消息框,指导您更改第二个文档的默认字体。在“格式”菜单上,单击“字体”,然后单击“默认”。当询问您是否更改默认字体时,单击“是”,然后单击“确定”取消消息框。
当关闭第二个 Word 实例时,将出现在“摘要”部分显示的警告之一。

要解决以上代码中的此问题,请执行以下操作之一:
在 wdApp2.Quit 方法的调用语句之前,添加以下行:
    wdApp2.NormalTemplate.Saved = True
     
- 或 -
使用 Quit 方法的 SaveChanges 参数,如下所示:
   object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;        wa.QuIT(ref saveOption, ref Missing, ref Missing);

三、数据自动输出到Word表格的关键代码
  Delphi编写的应用软件与数据库连接和操作MS Office软件都比较方便,因此在这里使用Delphi的Object Pascal语言来描述数据自动输出到Word表格的关键代码。
  1、定义操作数据输出到Word表格类TgsDataToWordTable
  TgsDataToWordTable = class(TObject)
  private
  FWordApp: Variant;                // Word软件
  FSrcWordDoc: Variant;              // 模板文档
  FDesWordDoc: Variant;             //目标文档
  public
  function  InitWordApp: Boolean;    // 启动 MS Word
  function  OpenSrcFile(AFileName: String): Boolean;  //打开模板文档
  procedure CopyTable;    //拷贝表格
  procedure TableInsertLine(Row: Integer; Count: Integer);//插入空行
  procedure SetTableCellText(CellY, CellX: Integer; Text: String);  //数据输出到单元格
  procedure MergeCells(CellY1, CellX1, CellY2, CellX2: Integer);  //合并单元格
  end;
  2、TgsDataToWordTable类关键方法的实现
  (1)拷贝表格
  procedure TgsDataToWordTable.CopyTable;
  begin
  FSrcWordDoc.Tables.Item(1).Select;
  FWordApp.Selection.Copy;
  FDesWordDoc.Activate;
  FWordApp.Selection.Paste;
  end;
  (2)插入空行
  procedure TgsDataToWordTable. TableInsertLine (Row: Integer; Count: Integer);
  begin
  if Count > 1 then
  begin
  FDesWordDoc.Tables.Item(1).Cell(Row, 1).Select;
  FWordApp.Selection.InsertRowsBelow(Count);
  end;
  end;
  (3)设置单元格数据
  procedure TgsDataToWordTable. SetTableCellText (CellY, CellX: Integer;
  Text: String);
  begin
  FDesWordDoc.Tables.Item(1).Cell(CellY, CellX).Select;
  FWordApp.Selection.Text := Text;
  end;
  (4)合并单元格
  procedure TgsDataToWordTable.MergeCells(CellY1, CellX1, CellY2, CellX2: Integer);
  begin
  FDesWordDoc.Tables.Item(1).Cell(CellY1, CellX1).Merge(FDesWordDoc.Tables.Item(1).Cell(CellY2, CellX2));
  end;   

C#_Word详细解析相关推荐

  1. OpenCL编程详细解析与实例

    OpenCL编程详细解析与实例 C语言与OpenCL的编程示例比较 参考链接: https://www.zhihu.com/people/wujianming_110117/posts 先以图像旋转的 ...

  2. 深度学习目标检测详细解析以及Mask R-CNN示例

    深度学习目标检测详细解析以及Mask R-CNN示例 本文详细介绍了R-CNN走到端到端模型的Faster R-CNN的进化流程,以及典型的示例算法Mask R-CNN模型.算法如何变得更快,更强! ...

  3. celery的使用(最新详细解析)

    celery的使用(最新详细解析) 一. Celery简介 Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的异步任务队列,同时也支持任务调度. Celery的架构由三部 ...

  4. 汇编语言 第3版 王爽 检测点习题部分—答案及详细解析

    第一章 基础知识 检测点1.1 (1)1个CPU的寻址能力为8KB,那么它的地址总线的宽度为()位. (2)1KB的存储器有() 个存储单元,存储单元的编号从()到() . (3)1KB的存储器可以存 ...

  5. Eclipse快捷键详细解析

    android开发中常用的Eclipse快捷键详细解析 1.查看快捷键定义的地方 Window->Preferences->General->Keys. 2.更改启动页 在Andro ...

  6. AlphaCode到底强在哪儿?清华博士后十分钟视频详细解析

    来源:机器之心 本文约2300字,建议阅读5分钟 AlphaCode 到底是怎么练成的? 春节期间,DeepMind 的编程版 AlphaGo--AlphaCode 一度火到刷屏.它可以编写与普通程序 ...

  7. mysql grant all详解_MySQL grant 语法的详细解析

    以下的文章是MySQL grant语法的详细解析,如果你对MySQL grant 语法的相关的实际操作有兴趣的话,你就可以对以下的文章点击观看了.我们大家都知道MySQL数据库赋予用户权限命令的简单格 ...

  8. 线程同步锁 java_java多线程同步之重入锁,详细解析

    上次已经为大家介绍过java多线程同步,Volatile详解的主要内容了.今天再来为大家介绍一些相关的内容,也就是java多线程同步之重入锁,一起来了解一下吧. 使用重入锁实现线程同步 在JavaSE ...

  9. 终端不能联网_详细解析物联网是什么?

    原标题:详细解析物联网是什么? 物联网的英文是Internet of Things,缩写为IoT.这里的"物"指的是我身边一切能与网络联通的物品.例如你带的手表.你骑的共享单车.马 ...

最新文章

  1. 客快物流大数据项目(六):Docker与虚拟机的形象比喻及组件介绍
  2. OSPF多区域配置与汇总
  3. Mysql:一条sql是如何执行的?
  4. 这才是老公的正确用法,不吃就往死里打......
  5. Mr.J--JS学习(闭包及IIFE)
  6. UE3 GPU性能和分析
  7. switchpreference 事件_Vue 3 自定义事件
  8. 淘宝和网易云怎样知道你喜欢什么?终于有人把推荐系统讲明白了
  9. GRE词汇统计大全(二)
  10. Exploit-exercises
  11. python空格转义字符_python空格转义
  12. 形容人的内核是什么意思_【得到】人人都需要的精准表达术-老光
  13. 向境外支付软件测试费,支付宝怎么打开境外支付?境外支付有手续费吗?
  14. Linux下使用crontab来执行定时任务计划----执行每晚12点多执行移动log日志文件操作
  15. DS18B20数字温度传感器及单总线协议规定
  16. 建模中常用的30各MATLAB程序和函数
  17. 蚂蚁金服开源地理可视化引擎 L7 2.0 正式版来了,让地图动起来
  18. 基于PHP+MySQL的在线投票系统设计与实现
  19. 导入技能要素三大类_教学能力综合训练(李平)-中国大学mooc-题库零氪
  20. Windows 已在tong.exe 中触发一个断点。

热门文章

  1. AIoT,构建更佳边缘AI能力
  2. 协力发展,共创未来丨热烈欢迎中电互联赴云恒调研指导
  3. ROS教程——1.6 如何使用TF变换
  4. Windows未能启动:0xc00000e9错误
  5. 单片机c语言除法运算,针对小容量单片机程序优化方式--乘除法篇
  6. visio画图旋转任意角度,不保留白边保存pdf格式
  7. u盘插入电脑后识别缓慢该怎么解决
  8. Outlook邮箱只显示一个邮箱账号内容怎么解决,outlook如何切换邮箱账号显示
  9. vue-router 路由创建、路由嵌套、二级路由
  10. 【VUE】二级路由 子路由跳转空白