如何获取表所属的部分和子部分?

时间:2017-10-03 15:54:41

标签: excel vba excel-vba word-vba

我有以下代码循环遍历word文档中的所有表并提取所有行和单元格信息。我还需要知道该表所属的部分和子部分,并将其记录在Excel的单元格中。我可以访问这些信息吗?

 'On Error Resume Next
  Application.ScreenUpdating = False
  Application.EnableEvents = False
 'Application.Calculation = xlCalculationManual
  Application.DisplayAlerts = False
  Dim intChoice As Integer
  Dim strPath As String
  Dim objWord As Object
  Dim objdoc As Object
  Dim ChartObj As ChartObject
 'Dim objShape As InlineShape
  Dim tTable As Word.Table
  Dim wb As Worksheet
  Dim wb1 As Worksheet
  Set wb = Worksheets("Pull Images")
  Set wb1 = Worksheets("Results")
  wb1.Pictures.Delete
  Set objWord = CreateObject("Word.Application")
  objWord.Visible = True
  Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
  intChoice = Application.FileDialog(msoFileDialogOpen).Show
  'On Error Resume Next
  'if the user selects a file
  If intChoice <> 0 Then
  'get the path selected
  strPath = Application.FileDialog( _
    msoFileDialogOpen).SelectedItems(1)
  'opens the document
  Set objdoc = objWord.Documents.Open(strPath)
  With objWord.Documents(objdoc)
  Set Rng = wb.Range("A1")
  N = 3
  For Each tTable In objdoc.Tables
    tTable.Range.Copy
    Rng.Select
    Rng.Parent.PasteSpecial Format:="Text", Link:=False, _
                DisplayAsIcon:=False
    With Rng.Resize(tTable.Rows.Count, tTable.Columns.Count)
        .Cells.UnMerge
        .Cells.ColumnWidth = 14
        .Cells.RowHeight = 14
        .Cells.Font.Size = 10
    End With

    Set Rng = Rng.Offset(tTable.Rows.Count + 2, 0)

 Next tTable
 End With
 End If
 'objWord.ActiveDocument.SaveAs ThisWorkbook.Path & "\" & 
  ActiveSheet.Range("E3").Value & "_MVR"
 'objWord.ActiveDocument.Close
  objWord.Quit
  Set objdoc = Nothing
  Set objWord = Nothing
  Set myrange = Nothing
  Set myrange1 = Nothing
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  'Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  end sub

1 个答案:

答案 0 :(得分:0)

此代码将返回文档中每个表的节号。

Dim Tbl As Table

With ActiveDocument
    For Each Tbl In .Tables
        Debug.Print Tbl.Range.Sections(1).Index
    Next Tbl
End With