2017-10-03 1 views
-1

J'ai le code suivant qui boucle dans toutes les tables dans un document Word et tire toutes les informations de ligne et de cellule. J'ai aussi besoin de savoir à quelle section et sous-section appartient cette table aussi et de l'enregistrer dans une cellule dans Excel. Puis-je accéder à cette information?Comment obtenir la section et la sous-section à laquelle appartient une table?

'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 
+0

[Oui, vous pouvez] (http://vba.relief.jp/word-vba-get-current-section-number/). Qu'avez-vous essayé? – BruceWayne

+0

J'ai essayé selection.Information (wdActiveEndSectionNumber), mais cela ne fonctionne pas. –

+0

'ça ne marche pas' n'est pas une description d'un problème – jsotola

Répondre

0

Ce code renverra le numéro de section de chaque table de votre document.

Dim Tbl As Table 

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