C#调用WORD时出错如下:               object missingValue = Type.Missing;                object location = strInfo.Length; //如果location超过已有字符的长度将会出错。一定要比"明细表"串多一个字符                  Word.Range rng2 = wordDoc.Range(ref location, ref location);                                wordDoc.Tables.Add(rng2, 14, 6, ref missingValue, ref missingValue);                wordDoc.Tables.Item(1).Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;//错误在这里 “Word.Tables”并不包含“Item”的定义                wordDoc.Tables.Item(1).Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));//错误在这里“Word.Tables”并不包含“Item”的定义                wordDoc.Tables.Item(1).Range.Font.Size = 10;//错误在这里“Word.Tables”并不包含“Item”的定义                wordDoc.Tables.Item(1).Range.Font.Name = "宋体";//错误在这里“Word.Tables”并不包含“Item”的定义
解决办法:1、填加引用:Microsoft Office 11.0 Object Library ;2、把Tables.Item(1)改成Tables[1]。
 

解决方案 »

  1.   

    object oFileName = @"C:\Documents and Settings\liush\My Documents\TestDoc.doc";
    object oReadOnly = true;
    object oMissing = System.Reflection.Missing.Value;Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = true;//只是为了方便观察
    oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);//MessageBox.Show(oDoc.Tables.Count.ToString());
    for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
    {
        Word.Table nowTable = oDoc.Tables.Item(tablePos);
        string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);    for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
        {
    for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
    {
    tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
    tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
    tableMessage += "\t";
    }tableMessage += "\n";
        }    MessageBox.Show(tableMessage);
    }
    http://edu.itbulo.com/200608/104129.htm