【问题标题】:VBA | Word <--> Table Column AlignmentVBA | Word <--> 表格列对齐
【发布时间】:2021-08-31 00:15:21
【问题描述】:

在 word 文档中的列中出现正确对齐问题。 我正在使用我们的一个系统中的表格创建一个 word 文档。第一次创建表格并选择要对齐的列将没有问题。如果用户现在创建一个新文档,覆盖旧文档,它将崩溃。如果创建了新的word文档,没有覆盖旧的,就不会出现错误。

因此,覆盖现有文档的组合,没有任何文字处理正在运行,并且选择右对齐的列将崩溃。这就是我尝试对齐列的方式。

objTable.Columns(4).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

objTable.Columns(5).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

objTable.Columns(6).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

有人知道如何解决这个问题吗?

谢谢

€dit: 我们有软件,用户可以在其中创建 Word 文档。 Word 文档加载一个 Word 模板,其中一个书签标记了创建表格的位置。在创建表之前,模板中的新文档将保存在网络路径上。如果该模板中已有文档,则应将其覆盖。第一次保存文档并给出正确的名称后,我的方法会创建表格并填充内容。如果之前创建了一个文档,那么一旦我的方法尝试对齐列(上面代码中的对齐部分),创建部分就会崩溃。我看了看任务管理器,第一次运行后没有运行 word 进程。如果创建了一个新的 word 文档,而不覆盖现有的文档,则对齐没有问题。所以我猜想覆盖现有文档和对齐的组合是导致错误的原因。

€dit2 - 我的代码(我删除了变量声明等不必要的代码行):

'That is kind of strange, because even though it should be nothing it  skipped that part - But if it tries to use the existing word instance - it crashes with the 462 - remote-server-computer is not available.   
If app is Nothing Then
        Set app = New Word.Application  
        Exit Function
End If
Set document = app.Documents.Add(Template:=Template, NewTemplate:=False, DocumentType:=0)
Dim settings As settings
settings = exportWord (document,...)


Private Function exportWord (oDoc As Word.Document,  ...) As settings
On Error GoTo Err_WordExport
Dim sets As settings
With sets
.export = False
End With
exportWord  = sets
Dim objRange As Word.Range
Dim objTable As Word.Table
With oDoc
    Set objRange = .Bookmarks("tbl").Range
    .Tables.Add objRange, positionen.Count + 1, 6
    Set objTable = .Bookmarks("tbl").Range.Tables(1)
End With
With objTable
    With .Rows(1)
        .Cells(1).Range.Text = ""
        .Cells(2).Range.Text = ""
        .Cells(3).Range.Text = ""
        .Cells(4).Range.Text = ""
        .Cells(5).Range.Text = ""
        .Cells(6).Range.Text = ""
        .Cells(1).Range.Font.Bold = True
        .Cells(2).Range.Font.Bold = True
        .Cells(3).Range.Font.Bold = True
        .Cells(4).Range.Font.Bold = True
        .Cells(5).Range.Font.Bold = True
        .Cells(6).Range.Font.Bold = True
    End With
End With
Dim i As Long
i = 2
For Each ItemPos In Positionen
'fill the content
Next ItemPos
With objTable.Rows(1).Borders(wdBorderBottom)
    .Visible = True
    .LineStyle = wdLineStyleDouble
End With
objTable.Columns(4).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
objTable.Columns(5).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
objTable.Columns(6).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
objTable.Columns.AutoFit
oDoc.SaveAs2 pathToSave
    
With sets
   .export = True
   .PathToFile = pathToSave
End With
    
exportWord = sets
Set objTable = Nothing
End Function

【问题讨论】:

  • 为什么文档会加载 Word 模板?如果它是一个实际的 Word 模板(.dotx、.dotm),则应该从一开始就从它创建文档。
  • “如果该模板中已经有一个文档,则应该覆盖它” - 如果存在同名的现有文件,无论原始文件是什么“模板”,它无疑都会被覆盖是从创建的。唯一的例外是存在文件锁定问题。你检查过吗?
  • 如果您的代码在尝试选择列时崩溃,最常见的原因是表格包含水平合并的单元格。你检查过吗?
  • @TimothyRylatt 感谢您的回答,不应该有任何合并的单元格。我添加了上面的代码。
  • 逐行遍历您的代码并确定错误发生在哪一行。这将使您更好地了解导致问题的原因。

标签: vba ms-word


【解决方案1】:

您可以通过循环遍历单元格来避免选择列。您还可以将代码简化如下:

Set objTable = oDoc.Tables.Add(oDoc.Bookmarks("tbl").Range, Positionen.Count + 1, 6)
With objTable
    Dim wdCell As Word.Cell
    With .Rows(1).Borders(wdBorderBottom)
        .Visible = True
        .LineStyle = wdLineStyleDouble
    End With
    For Each wdCell In .Rows(1).Cells
        With wdCell.Range
            .Text = ""
            .Font.Bold = True
        End With
    Next wdCell
    Dim colIndex As Long
    For colIndex = 4 To 6
        For Each wdCell In .Columns(colIndex).Cells
            wdCell.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
        Next wdCell
    Next colIndex
End With

您可以通过在模板中添加一个已应用格式的 2 行表格来进一步细化这一点。那么你只需要:

Set objTable = oDoc.Bookmarks("tbl").Range.Tables(1)
Dim i As Long
For i = 1 To positionen - 1
    objTable.Rows.Add
Next i

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2022-07-20
    • 2013-06-22
    相关资源
    最近更新 更多