所请求的集合成员不存在 - MS Access DBA

问题描述 投票:0回答:1

我从某人那里继承了 MS Access 数据库。它从数据库中提取数据并填充 Word 模板。我想改进他们格式化表格的方式。我的目标是为每列设置宽度和对齐方式。下面是我想到的,但是当我运行它时,我收到一条错误消息,并且突出显示“With .Columns(2)”。错误消息是:“请求的集合成员不存在”。它在第 2 列上出错,而不是在第 1 列上出错。我已经尝试了一堆不同的代码,使用我在 google 上搜索到的示例,但没有成功。

Private Sub formatTable()

With m_objWord.Selection
    .SelectCell
    .Font.Bold = True
  
    With .Tables(1)
        .PreferredWidthType = wdPreferredWidthPercent
        .PreferredWidth = 97
        .Style = "Table Grid"
    End With
    
    With .Columns(1)
         .PreferredWidth = InchesToPoints(2.5)
         .Select
         With Selection.ParagraphFormat
           .Alignment = wdAlignParagraphLeft
         End With
    End With
        
    With .Columns(2)
         .PreferredWidth = InchesToPoints(2.25)
         .Select
         With Selection.ParagraphFormat
           .Alignment = wdAlignParagraphLeft
         End With
    End With
    
    With .Columns(3)
         .PreferredWidth = InchesToPoints(0.9)
         .Select
         With Selection.ParagraphFormat
           .Alignment = wdAlignParagraphCenter
         End With
    End With
         
    With .Columns(4)
         .PreferredWidth = InchesToPoints(1)
         .Select
         With Selection.ParagraphFormat
           .Alignment = wdAlignParagraphCenter
         End With
    End With
         
    
End With

End Sub
vba ms-access
1个回答
0
投票
  • 如果
    Selection
    在 Word 表格内,则
    Selection.Columns(1)
    表示插入点所在的列。但是,
    Selection.Columns(2)
    无效,导致运行时错误
    5941
    ,如原帖中所述。
Sub formatTable()
    Dim oTab As Table
    'Set m_objWord = Word.Application ' for testing
    With m_objWord.Selection
        If .Information(wdWithInTable) Then
            .SelectCell
            .Font.Bold = True
            Set oTab = .Tables(1)
        Else
            MsgBox "Click any cell in table before running the cod."
            Exit Sub
        End If
    End With
    With oTab
        .PreferredWidthType = wdPreferredWidthPercent
        .PreferredWidth = 97
        .Style = "Table Grid"
        Dim i As Long, aInch
        aInch = Split("2.5 2.25 0.9 1")
        For i = 1 To 4
            With .Columns(i)
                 .PreferredWidth = InchesToPoints(aInch(i - 1))
                 .Select
                Selection.ParagraphFormat.Alignment = IIf(i < 3, wdAlignParagraphLeft, wdAlignParagraphCenter)
            End With
        Next
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.