展开单词表VBA

展开单词表VBA,vba,ms-word,formatting,Vba,Ms Word,Formatting,我正在使用VBA宏在word文档中插入行(两列)。问题是插入的行不能填充整个页面,所有列的宽度也不相同: 我的问题是:如何为所有列指定相同的宽度,并展开表格以填充页面宽度? 以下是我的功能: Private Function CreateWordDoc(ByVal wrdApp As Word.Application, ByRef Objects() As OwnClass, ByVal sFilename As String, ByVal sPath As String) Dim i

我正在使用VBA宏在word文档中插入行(两列)。问题是插入的行不能填充整个页面,所有列的宽度也不相同:

我的问题是:如何为所有列指定相同的宽度,并展开表格以填充页面宽度?

以下是我的功能:

Private Function CreateWordDoc(ByVal wrdApp As Word.Application, ByRef Objects() As OwnClass, ByVal sFilename As String, ByVal sPath As String)
    Dim i As Integer
    Dim wrdDoc As Word.Document
    Dim MyObj As OwnClass

    Dim wrdTppTable As Word.Table

    Set wrdDoc = wrdApp.Documents.Add(sFilename, Visible:=True)

    Set wrdTppTable = wrdDoc.Tables(2)

    For i = 0 To UBound(Objects) - 1
        Set MyObj = Objects(i)
        ' Add a row to the table and select it
        wrdTppTable.Rows.Add.Select
        ' Work with the selected row
        With wrdApp.Selection.Range
            ' Make sure the row is on two columns
            .Cells.Split 1, 2, True
            ' Set the text font parameters
            With .Font
                .ColorIndex = wdBlack
                .name = "Arial"
                .size = 11
                .Bold = False
            End With
            ' Write text in the cell
            .Text = MyObj.GetKey & ": " & MyObj.GetValue
            ' Then select the next cell in the row
            .Next.Select
        End With
        ' Work with the second column of the row
        wrdApp.Selection.Cells.SetWidth 54, RulerStyle:=wdAdjustFirstColumn
        With wrdApp.Selection.Range
            With .Font
                .ColorIndex = wdBlack
                .name = "Arial"
                .size = 11
                .Bold = False
            End With
            ' Write the cell
            .Text = MyObj.GetId
        End With
    Next
End Function

要完成所要完成的任务,您可以做的最好的事情是在开始时设置柱的尺寸,然后再执行任何修改。示例代码:

Dim availableWidth As Integer
availableWidth = wrdDoc.PageSetup.PageWidth - wrdDoc.PageSetup.LeftMargin - wrdDoc.PageSetup.RightMargin

With wrdTppTable
   .Columns.Add 'Adding the required column
    'Resizing both columns on account of the available space
   .Columns(1).Width = availableWidth / 2
   .Columns(2).Width = availableWidth / 2
   .Cell(1, 1).Merge .Cell(1, 2)
End With

在这段代码之后,只需添加行,就可以开始遍历单元格并执行所需的操作。使用
单元格。仅在确实需要时分割
;例如:在第三行中,您希望有三列,其中两列适合第二主列。

您可以通过直接影响此属性来设置列的宽度(.columns(x).width)。另一方面,我没有太清楚的代码的各个部分;例如:为什么要一次又一次地拆分,而不是创建所需的列数(实际上,拆分为特定的行)?我对使用Word的VBA非常陌生。我不确定拆分是最好的方式。我愿意接受任何代码提案。如果你有,我很乐意看。请随意回答并发布一些代码;)作为参考,我将行添加到word模板中,该模板已经包含一个表,其中有一行(标题),但只有一列。所有其他行必须有两列。这很有意义:)我已经用所需的代码编写了一个答案,以解决您的大小调整问题;从您的代码中删除所有拆分和设置宽度,只需将其写在顶部即可。PS:您的代码在其他部分也非常混乱(什么是对象?!);但是我想它可以按照您的意愿工作,您的整个问题是列大小调整部分。是的
Objects
是我在代码中定义的对象数组。这与此无关。您的代码生成了一个包含两列和x行的表,但我希望表头仅位于一列上。我应该合并函数末尾的第一行吗?@Maxbester我告诉过你:删除所有涉及拆分和设置宽度的代码位。如您所见,我的代码输出两个相同的列,如果您以后不修改它,它将保持不变。在新代码中,您仍然有“wrdApp.Selection.Cells.SetWidth 54,RulerStyle:=wdAdjustFirstColumn”哦,是的,对不起。我忘了这句话。它现在工作得很好。谢谢@没问题。