我用网上的例子操作word,但是总是出现问题,我已经添加了 word 11.0 的引用了
源码:错误如下:
错误 1 “System.Windows.Forms.Button”并不包含“WdParagraphAlignment”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 72 68 窗体间数据交互
错误 2 “System.Windows.Forms.Button”并不包含“WdUnits”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 79 36 窗体间数据交互
错误 3 “System.Windows.Forms.Button”并不包含“newTable”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 87 18 窗体间数据交互
错误 4 “System.Windows.Forms.Button”并不包含“WdLineStyle”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 87 59 窗体间数据交互
错误 5 “System.Windows.Forms.Button”并不包含“WdLineStyle”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 88 57 窗体间数据交互
错误 6 “System.Windows.Forms.Button”并不包含“WdCellVerticalAlignment”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 98 67 窗体间数据交互
错误 7 “System.Windows.Forms.Button”并不包含“WdParagraphAlignment”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 99 69 窗体间数据交互
错误 8 “System.Windows.Forms.Button”并不包含“WdColor”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 103 62 窗体间数据交互
错误 9 “System.Windows.Forms.Button”并不包含“WdCellVerticalAlignment”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 106 67 窗体间数据交互
错误 10 “System.Windows.Forms.Button”并不包含“WdUnits”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 113 42 窗体间数据交互
错误 11 “System.Windows.Forms.Button”并不包含“WdMovementType”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 115 44 窗体间数据交互
错误 12 “System.Windows.Forms.Button”并不包含“WdWrapType”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 128 46 窗体间数据交互
错误 13 “System.Windows.Forms.Button”并不包含“WdParagraphAlignment”的定义 F:\李丽共享\源码\C#源码\自己编写\窗体间数据交互\窗体间数据交互\窗体间数据交互\Form1.cs 136 62 窗体间数据交互问题:
请问这个问题怎么解决?或者还有别的操作word的好方法,要能画表格,能插入图片

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Configuration;
    using System.IO;
    using Word;
     
    namespace 窗体间数据交互
    {
        public partial class Form1 : Form
        {
            public ArrayList listData1;
            public Form1()
            {
                InitializeComponent();
                this.listData1 = new ArrayList();
                this.listData1.Add("DotNet");
                this.listData1.Add("C#");
                this.listData1.Add("Asp.net");
                this.listData1.Add("WebService");
                this.listData1.Add("XML");
                this.listBoxFrm1.DataSource = this.listData1;
            }        private void buttonEdit_Click(object sender, EventArgs e)
            {
                Form2 formChild = new Form2(this.listData1);//构造函数在实例化中调用
                formChild.Owner = this;            formChild.ShowDialog();
                this.listBoxFrm1.DataSource = null;
                this.listBoxFrm1.DataSource = this.listData1;
               // Process.Start("IEXPLORE.EXE");   
            }
            private void Word_Click(object sender, EventArgs e)
            {
                CreateWordFile("");
            }
             private string CreateWordFile(string CheckedInfo)
            {
                string message = "";
                try
                {
                    Object Nothing = System.Reflection.Missing.Value;
                    Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
                    string name = "CNSI_" + DateTime.Now.ToString()+".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.
                     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;
            }      
        }
    }
      

  2.   

    红色字体是出错的几个地方
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Configuration;
    using System.IO;
    using Word;
     
    namespace 窗体间数据交互
    {
        public partial class Form1 : Form
        {
            public ArrayList listData1;
            public Form1()
            {
                InitializeComponent();
                this.listData1 = new ArrayList();
                this.listData1.Add("DotNet");
                this.listData1.Add("C#");
                this.listData1.Add("Asp.net");
                this.listData1.Add("WebService");
                this.listData1.Add("XML");
                this.listBoxFrm1.DataSource = this.listData1;
            }        private void buttonEdit_Click(object sender, EventArgs e)
            {
                Form2 formChild = new Form2(this.listData1);//构造函数在实例化中调用
                formChild.Owner = this;            formChild.ShowDialog();
                this.listBoxFrm1.DataSource = null;
                this.listBoxFrm1.DataSource = this.listData1;
               // Process.Start("IEXPLORE.EXE");   
            }
            private void Word_Click(object sender, EventArgs e)
            {
                CreateWordFile("");
            }
             private string CreateWordFile(string CheckedInfo)
            {
                string message = "";
                try
                {
                    Object Nothing = System.Reflection.Missing.Value;
                    Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
                    string name = "CNSI_" + DateTime.Now.ToString()+".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.
                     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;
            }      
        }
    }
      

  3.   

    我的是office2003 应该是11.0吧
    我的com里面只有 office 10.0 没有word 10.0
      

  4.   

    我使用vs2005,讲你的代码复制进去,然后又引用了word11.0,发现运行一切正常,唯独就是在saveas的位置有个异常,报的是文件名可能有问题,你说的问题不存在,可能是不是你的引用有问题,或者引用的组件有问题
      

  5.   

    Microsoft word 11.0 object library
    C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll,这个不同机器环境不一样
      

  6.   

    to hack8是不是我的office 2003有问题?我用的是盗版的
      

  7.   

    换了一下代码,问题已经解决了,现在贴出来源代码.button1 新建文档,button2 打开指定文档,buttons导入模版
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Configuration;
    using System.IO;
    using Word;
     
    namespace 窗体间数据交互
    {
        public partial class Form1 : Form
        {
            public ArrayList listData1;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                CreateWordFile("");
            }
             private string CreateWordFile(string CheckedInfo)
            {
                string message = "";
                try
                {
               //    创建新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 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));                //
                    //填充表格内容
                    newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                    newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                  //  通过段落控制插入
                //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();            }
                catch
               {
                    message = "文件导出异常!";
                }
                return message;
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button2_Click(object sender, EventArgs e)
            {
               // 打开指定文档:在指定标签处添加文字
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oWord.Visible = true;
                object fileName = @"E:/TestDoc.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 j = null;
                int i = 1;
                j = (object)i;
                   
                object lbl = oDoc.Books.get_Item(ref j);
                oDoc.Books.get_Item(ref lbl).Select();
                oDoc.Application.Selection.TypeText("123");        }
            private void button3_Click(object sender, EventArgs e)
            {
                // 导入模版:在指定标签处添加文字
                object oMissing = System.Reflection.Missing.Value;
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oWord.Visible = true;
                object fileName = @"E:/TestDoc.doc";
                oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
                object j = null;
                int i = 1;
                j = (object)i;            object lbl = oDoc.Books.get_Item(ref j);
                oDoc.Books.get_Item(ref lbl).Select();
                oDoc.Application.Selection.TypeText("123");
            }   
       
        }
    }