使用VBA在MS Word中创建表 [英] creating tables in MS Word using VBA

查看:93
本文介绍了使用VBA在MS Word中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MS Word中创建表格的代码是什么?

What are the codes to create a table in MS Word?

我已经使用了宏的记录功能来查看它是如何编写的,我也不知道该如何解释.

I've used the record function for macro to see how it is written and i have no idea how to interpret this.

,并且在录制宏时不能绘制表格吗?它使绘制表"功能灰显.

and can you not draw tables while recording a macro? It greys out the "draw table" function.

我需要制作一个在表中具有一些合并单元格的桌子-如果我可以绘制表并使用宏进行记录会更容易,但似乎我做不到...

I need to make a talbe that has some merged cells within the table - it would be easier if i can draw the table and record using macro but it seems like i can't do that...

我已经使用了draw函数来绘制下表,但我无法记录它.

I've used the draw function to draw the follow table but i can't record it.

帮助?!

推荐答案

这应该可以帮助您入门.

This should get you started.

Sub Tester()


    Dim x, w, c

    ThisDocument.Tables(1).Delete

    ThisDocument.Tables.Add Range:=Selection.Range, NumRows:=7, NumColumns:=1, _
                          DefaultTableBehavior:=wdWord9TableBehavior, _
                          AutoFitBehavior:=wdAutoFitFixed

    With ThisDocument.Tables(1)

        .Rows.Height = 70
        w = .Rows(1).Cells(1).Width

        .Rows(1).Cells(1).Split 1, 7
        .Rows(1).Cells(1).Width = w / 2
        For x = 2 To 7
            .Rows(1).Cells(x).Width = (w / 2) / 6
        Next x

        .Rows(5).Height = 15
        .Rows(7).Height = 15

        .Rows(7).Cells(1).Split 1, 7

        .Rows(6).Cells(1).Split 1, 4
        .Rows(6).Cells(2).Split 2, 1

        'Once you merge cells it gets difficult to use .Rows, but
        '  you can still address individual cells. Use the loop below to
        '  find out which one you need to operate on...
        x = 1
        For Each c In .Range.Cells
            c.Range.Text = x
            x = x + 1
        Next c

        .Range.Cells(16).Split 1, 4
        'you can figure out setting the exact required widths...
    End With
End Sub

这篇关于使用VBA在MS Word中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆