2014-10-02 153 views
-2

我有一個包含頭文件(H1,H2,H3等)的單詞文檔。在一些各章節的,也有這樣的要求:獲取單詞出現在Word VBA文檔中的章節

enter image description here

對於每一個要求,我有一個表像上面的。我使用正則表達式來查找我的需求。

首先需要

我想提取一個Excel文件的所有要求引用,並知道它們是章(用「/」這樣的H1/H2/H3每個級別之間)。

我將在輸出這種Excel文件:

enter image description here

我成功提取所有要求的引用而不是它們的路徑章。

這是我寫的,代碼其中工程提取的要求只引用來自一個名爲「簡介」書籤(爲了不考慮這些至極是在內容表):

Sub Extract_Requirements() 

    Set oExcel = CreateObject("excel.application") 
    oExcel.Visible = True 
    Set oWk = oExcel.Workbooks.Add 

    'Headers of the Excel file 
    oWk.Sheets(1).Range("A1") = "PATH" 
    oWk.Sheets(1).Range("B1") = "ID" 
    oWk.Sheets(1).Range("C1") = "VERSION" 
    oWk.Sheets(1).Range("D1") = "REF" 
    oWk.Sheets(1).Range("E1") = "LABEL" 
    oWk.Sheets(1).Range("F1") = "DESCRIPTION" 
    oWk.Sheets(1).Range("G1") = "CRITICALITY" 
    oWk.Sheets(1).Range("H1") = "CATEGORY" 
    oWk.Sheets(1).Range("I1") = "STATE" 
    oWk.Sheets(1).Range("J1") = "CREATED_ON" 
    oWk.Sheets(1).Range("K1") = "CREATED_BY" 

    'Start inserting data in Excel file 
    i = 2 

    Set RegEx = New RegExp 
    RegEx.Pattern = "([A-Za-z0-9]+_)+\d{3}" 
    RegEx.IgnoreCase = True 
    RegEx.Global = True 

    'Move the cursor to the Introduction bookmark (useful not to get the requirements within the table of content) 
    Selection.GoTo What:=wdGoToBookmark, Name:="Introduction" 
    Selection.End = ActiveDocument.Content.End 

    Dim matchCol As MatchCollection 
    Set matchCol = RegEx.Execute(Selection.Range) 

    For Each Match In matchCol 
     'PATH de l'exigence 
     'TODO 

     'VERSION de l'exigence 
     'TODO 

     'LABEL de l'exigence 
     oWk.Sheets(1).Range("E" & i) = Match.Value 

     'DESCRIPTION de l'exigence 
     'TODO 

     'STATE 
     oWk.Sheets(1).Range("I" & i) = "APPROVED" 
     i = i + 1 

    Next Match 
End Sub 

第二需要

獲取下面的參考和說明的版本。

在此先感謝您的幫助

回答

0

我終於找到了另一種方法來做到這一點。我循環段落:

For Each objPara In Selection.Paragraphs 
    With objPara.Range 
     sText = .Text 
     sStyle = .ParagraphStyle 

     'On détermine le style de l'élément courant s'il en a un 
     If sStyle = "Titre 1;H1" Then 
      If sH1 <> sText Then 
       sH2 = "" 
      End If 
      sH1 = sText 
     ElseIf sStyle = "Titre 2;H2" Then 
      If sH2 <> sText Then 
       sH3 = "" 
      End If 
      sH2 = sText 
     ElseIf sStyle = "Titre 3;H3" Then 
      sH3 = sText 
     End If 
     Set regMatch = RegEx.Execute(sText) 
     IsAMatch = (regMatch.Count > 0) 
     If IsAMatch Then 
      'PATH de l'exigence 
      sPath = sH1 
      If sH2 <> "" Then 
       sPath = sPath & "/" & sH2 
      End If 
      If sH3 <> "" Then 
       sPath = sPath & "/" & sH3 
      End If 
     End If 
    End With 
Next