首页 文章

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

提问于
浏览
0

我有一个包含许多表的word文档 . 我使用下面的宏将表导出到excel文件 . Macro to export MS Word tables to Excel sheets

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

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

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

  • 我有一个Appx包含的word文档,20个表 . 而且我只需要提取一个表,其中,表名包含一个固定的子字符串 .

例如:

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

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

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

请建议我 .

非常感谢提前 .

2 回答

  • 0

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

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

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

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

    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
    

相关问题