ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitWindow
    With Selection.Tables(1)
        If .Style <> "网格型" Then
            .Style = "网格型"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.TypeText Text:="1"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="2"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="3"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="4"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="5"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="6"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="7"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="8"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="9"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="10"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="11"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="12"
    Selection.MoveRight Unit:=wdCell
End Sub

解决方案 »

  1.   

    不好意思,忘了加说明了
    这段代码是在word中的宏录制下来的,
    目的是要添加一个3行4列的表格到word文档中环境使用vc,在word的基础上做自己想要的功能,
    目前为止只有表格不能添加上去,
    有劳各位了
    只要把以下代码的参数用vc描述就行了,剩下的我自己差不多可以
     ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= _
            4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitWindow 备注:上面的代码是vba中添加表格到文档的接口方法,关键是那个Range对象怎么表示
      

  2.   

    看看MSDN上的文章:
    INFO: Using Visual C++ to Automate Office
      

  3.   


    TablesPtr tables = ActiveDocument->Tables;
    tables->Add(Selection->Range,3,4,wdWord9TableBehavior,wdAutoFitWindow);
    TablePtr table = Selection->Tables(1);
    if (table->Style != _bstr_t("网格型"))
      table->Style = "网格型";
    table->ApplyStyleHeadingRows = VARIANT_TRUE;
    table->ApplyStyleLastRow = VARIANT_TRUE;
    table->ApplyStyleFirstColumn = VARIANT_TRUE;
    table->ApplyStyleLastColumn = VARIANT_TRUE;
    Selection->TypeText("1");
    Selection->MoveRight(wdCell);
    ...
      

  4.   

    Range:=Selection.Range 中的第一个Range仅仅表示一个命名的参数而已,仅仅在VB中有效,VC是用不到的
      

  5.   

    跟我一样,做插件的
    不过我不做office插件
    查一下相关文档吧,先看看word的结构
      

  6.   

    http://blog.csdn.net/scq2099yt/category/370448.aspx
      

  7.   

    首先感谢各位的回答这是我写的一个,发蒙了,现在的问题是:
    1.为什么在我先添加两个文本框之后加这个表格,文本框就没了,然后我就把表格先添加,然后再添加文本框,但是这样觉得不爽,是不是和下面代码的第一句相关,要改的话怎么改?
    2.在有文本框和表格的情况下,我要添加特定的内容到特定的地方,该怎么获得相应的selection,vba我在看,感觉很多,一时间弄不太好,有劳各位指点迷津,多谢。range=doc.GetContent();
    tables=doc.GetTables();
    CComVariant  DefaultTableBehavior(1),AutoFitBehavior(2);
    tables.Add(range,3,3,&DefaultTableBehavior,&AutoFitBehavior);
    文本框我是这么做的
    shapes.AddTextbox(1,   (float)100,   (float)100,   (float)400,   (float)200,   &Anchor);  
    shapes.AddTextbox(1,   (float)100,   (float)320,   (float)400,   (float)200,   &Anchor);看到过什么索引,研究中......