当文件路径位于变量中时,如何获取表的总行数?

发布于 2025-01-10 01:47:01 字数 658 浏览 0 评论 0原文

我需要计算Word文档中表1的总行数。 文档路径位于变量中,因为它会根据用户输入而更改。包含路径的变量是fpath

当我使用变量而不是文件路径时,如何获取表 1 的总行数?

由于 Documents(fpath).Tables(1).Rows.Count,我的代码似乎失败了。

Sub CountTableRows()
    Dim fpath as String  'contains file path                                                             
    Dim fdoc as Document
    Dim trows as Integer 'contains total number of rows in table
    Set fpath = "c:\folder\document.docx"
    Set fDoc = Documents.Open(fpath)
    fDoc.Activate
    trows = Documents(fpath).Tables(1).Rows.Count
    MsgBox "total rows= " & trows
End Sub

I need to count the total number of rows in table 1 in a Word document.
The document path is in a variable because it will change based on user input. The variable that contains the path is fpath.

How do I get the total row count of table 1 when I use the variable instead of the file path?

My code seems to fail due to Documents(fpath).Tables(1).Rows.Count.

Sub CountTableRows()
    Dim fpath as String  'contains file path                                                             
    Dim fdoc as Document
    Dim trows as Integer 'contains total number of rows in table
    Set fpath = "c:\folder\document.docx"
    Set fDoc = Documents.Open(fpath)
    fDoc.Activate
    trows = Documents(fpath).Tables(1).Rows.Count
    MsgBox "total rows= " & trows
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-01-17 01:47:01

您可能希望修改您的代码以使其更具防御性,以便在遇到您隐藏的假设之一时它不会崩溃。 (即文件存在,并且至少有 1 个表)。

Public Function CountTableRows(ByVal ipPath As String, ByVal ipTableNo As Long) As Long

    CountTableRows = -1
    
    Dim myDoc As Word.Document
    If Not TryOpenDoc(ipPath, myDoc) Then
    
        Debug.Print "There was a problem opening '" & ipPath & "'"
        Exit Function
        
        
    End If
    
    Dim myRowCount As Long
    If Not TryCountRows(myDoc, ipTableNo, myRowCount) Then
    
        Debug.Print "Document '" & ipPath & "' does not have Table " & ipTableNo
        Exit Function
        
        
    End If
    
    myDoc.Close
    CountTableRows = myRowCount
    ' MsgBox "Total rows =" & myRowCount
        
End Function


Public Function TryOpenDoc(ByVal ipPath As String, ByRef opDoc As Word.Document) As Boolean

    TryOpenDoc = False
    
    On Error Resume Next
    Set opDoc = Application.Documents.Open(ipPath)
    If Err.Number <> 0 Then Exit Function
    
    Err.Clear
    TryOpenDoc = True
    
End Function


Public Function TryCountRows(ByRef ipDoc As Word.Document, ByVal ipTableNo As Long, ByRef opRowCount As Long) As Boolean

    TryCountRows = False
    If ipDoc.Tables.Count < ipTableNo Then Exit Function
   
    opRowCount = ipDoc.Tables(ipTableNo).Rows.Count
    
    TryCountRows = True
    
End Function

You may wish to revise your code to be more defensive, so that it does not fall over if it hits one of your hidden assumptions. (i.e. the file exists, and has at least 1 table).

Public Function CountTableRows(ByVal ipPath As String, ByVal ipTableNo As Long) As Long

    CountTableRows = -1
    
    Dim myDoc As Word.Document
    If Not TryOpenDoc(ipPath, myDoc) Then
    
        Debug.Print "There was a problem opening '" & ipPath & "'"
        Exit Function
        
        
    End If
    
    Dim myRowCount As Long
    If Not TryCountRows(myDoc, ipTableNo, myRowCount) Then
    
        Debug.Print "Document '" & ipPath & "' does not have Table " & ipTableNo
        Exit Function
        
        
    End If
    
    myDoc.Close
    CountTableRows = myRowCount
    ' MsgBox "Total rows =" & myRowCount
        
End Function


Public Function TryOpenDoc(ByVal ipPath As String, ByRef opDoc As Word.Document) As Boolean

    TryOpenDoc = False
    
    On Error Resume Next
    Set opDoc = Application.Documents.Open(ipPath)
    If Err.Number <> 0 Then Exit Function
    
    Err.Clear
    TryOpenDoc = True
    
End Function


Public Function TryCountRows(ByRef ipDoc As Word.Document, ByVal ipTableNo As Long, ByRef opRowCount As Long) As Boolean

    TryCountRows = False
    If ipDoc.Tables.Count < ipTableNo Then Exit Function
   
    opRowCount = ipDoc.Tables(ipTableNo).Rows.Count
    
    TryCountRows = True
    
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文