2014-10-02 154 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