当文件路径位于变量中时,如何获取表的总行数?
我需要计算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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能希望修改您的代码以使其更具防御性,以便在遇到您隐藏的假设之一时它不会崩溃。 (即文件存在,并且至少有 1 个表)。
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).