如何提取表,其中表名包含给定的子字符串

时间:2022-09-13 11:37:19

I have a word document with many tables. I have used the below macro to export the tables to an excel file. Macro to export MS Word tables to Excel sheets

我有一个包含许多表的word文档。我使用下面的宏将表导出到excel文件。宏将MS Word表导出到Excel工作表

that was so helpful really. and I am very much thankful.

真的很有帮助。而且我非常感谢。

but the issue , I face is my job is not done completely.

但问题是,我面临的是我的工作没有完全完成。

As such I have just started learning the VBA , I am not able to know , how to do the below task.

因此我刚刚开始学习VBA,我无法知道,如何完成以下任务。

  • i have a word document which Appx contains , 20 tables. and I have to extract only one table , where , the table name contains a fixed sub string.
  • 我有一个Appx包含的word文档,20个表。而且我只需要提取一个表,其中,表名包含一个固定的子字符串。

Ex :

If the word document has 3 tables with names "Table 1 : table with gender and salary" "Table 2 : table with salary info" "Table 3 : table with name , age , gender and salary "

如果word文档有3个表,其名称为“表1:具有性别和工资的表”“表2:带有工资信息的表”“表3:具有名称,年龄,性别和工资的表”

if i run the above macro , i get all the tables to my Excel doc successfully. but my need is , only to get the Table with name.

如果我运行上面的宏,我成功地将所有表格都添加到我的Excel文档中。但我的需要是,只是为了得到名字的表。

"table with name , age , gender and salary"

“有姓名,年龄,性别和工资的表”

please suggest me.

请建议我。

Really thanks in advance.

非常感谢提前。

2 个解决方案

#1


0  

Are you entering the correct table number in the input box?

您是否在输入框中输入了正确的表格编号?

This line restricts the import to just that specific table:

此行将导入限制为该特定表:

TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")

#2


0  

I have changed the some lines as per your requirement ,please check it and let me know

我已根据您的要求更改了一些行,请检查并告知我

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As string 
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table name of table to import", "Import Word Table", "table with name , age , gender and salary")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub

#1


0  

Are you entering the correct table number in the input box?

您是否在输入框中输入了正确的表格编号?

This line restricts the import to just that specific table:

此行将导入限制为该特定表:

TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")

#2


0  

I have changed the some lines as per your requirement ,please check it and let me know

我已根据您的要求更改了一些行,请检查并告知我

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As string 
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table name of table to import", "Import Word Table", "table with name , age , gender and salary")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub