簡體   English   中英

從Word提取標題到Excel

[英]Extract Heading from Word to Excel

我有一個包含注釋的Word文檔。 我寫了一個腳本提取到Excel:

  1. 評論編號
  2. 頁碼
  3. 評論者的首字母縮寫
  4. 評論者的姓氏
  5. 發表評論的日期
  6. 實際評論

我不知道的問題是我還需要提取標題編號和該標題的文本。 我需要注釋所在的標題的第7列。例如,假設我在標題“ 4.1這是標題”下的部分中有注釋。 我需要提取標題編號(4.1)和標題文本(這是一個標題)以及相關注釋。

為了創建標題,我利用了功能區“主頁”選項卡上“樣式”下“ Word”中的“標題”功能。

到目前為止,這是我寫的內容:

 Sub Export_Comments()

' Purpose: Search for comments in any text that's been pasted into
' this document, then export them into a new Excel spreadsheet.
' Requires reference to Microsoft Excel 15.0 Object Library in VBA,
' which should already be saved with as part of the structure of
' this .docm file.

Dim bResponse As Integer

' Exit routine if no comments have been found.
If ActiveDocument.Comments.Count = 0 Then
  MsgBox ("No comments found in this document")
  Exit Sub
Else
  bResponse = MsgBox("Do you want to export all comments to an Excel worksheet?", _
              vbYesNo, "Confirm Comment Export")
  If bResponse = 7 Then Exit Sub
End If

' Create a object to hold the contents of the
' current document and its text. (Shorthand
' for the ActiveDocument object.
Dim wDoc As Document
Set wDoc = ActiveDocument

' Create objects to help open Excel and create
' a new workbook behind the scenes.
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Dim i As Integer
Dim oComment As Comment         'Comment object

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

' Create a new Workbook. Shouldn't interfere with
' other Workbooks that are already open. Will have
' at least one worksheet by default.
Set xlWB = xlApp.Workbooks.Add

With xlWB.Worksheets(1).Range("A1")

  ' Create headers for the comment information
  .Offset(0, 0) = "Comment Number"
  .Offset(0, 1) = "Page Number"
  .Offset(0, 2) = "Reviewer Initials"
  .Offset(0, 3) = "Reviewer Name"
  .Offset(0, 4) = "Date Written"
  .Offset(0, 5) = "Comment Text"

  ' Export the actual comments information
  For i = 1 To wDoc.Comments.Count

    Set oComment = wDoc.Comments(i)
    .Offset(i, 0) = oComment.Index                                                'Comment Number
    .Offset(i, 1) = oComment.Reference.Information(wdActiveEndAdjustedPageNumber) 'Page Number
    .Offset(i, 2) = oComment.Initial                                              'Author Initials
    .Offset(i, 3) = oComment.Author                                               'Author Name
    .Offset(i, 4) = Format(oComment.Date, "mm/dd/yyyy")                           'Date of Comment
    .Offset(i, 5) = oComment.Range                                                'Actual Comment
  Next i

End With

' Make the Excel workbook visible
xlApp.Visible = True

' Clean up our objects
Set oComment = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

您可以使用名稱為\\HeadingLevel的內置書簽來獲取特定位置的Heading(通過應用九種可能的Heading樣式之一進行定義)。 為此,選擇必須在該范圍內。 這將返回標題下整個文本 ,因此需要將其折疊到起點,然后代碼與該段落一起使用以獲取ListString(編號)和文本。

文檔中注釋的范圍是Comment.Reference

在您的代碼上構建以下代碼,可以在我的測試環境(Word)中工作:

Dim rngComment As Word.Range, rngHeading As Word.Range

Set rngComment = oComment.Reference
rngComment.Select
Set rngHeading = ActiveDocument.Bookmarks("\HeadingLevel").Range
rngHeading.Collapse wdCollapseStart
Set rngHeading = rngHeading.Paragraphs(1).Range
Debug.Print rngHeading.ListFormat.ListString & " " & rngHeading.Text

我無法復制您的環境,但是以下方法應該有效

 For i = 1 To wDoc.Comments.Count
   Set oComment = wDoc.Comments(i)
   Set rngComment = oComment.Reference
   rngComment.Select
   Set rngHeading = wDoc.Bookmarks("\HeadingLevel").Range
   rngHeading.Collapse wdCollapseStart
   Set rngHeading = rngHeading.Paragraphs(1).Range
  .Offset(i, 0) = oComment.Index
  .Offset(i, 1) = oComment.Reference.Information(wdActiveEndAdjustedPageNumber)
  .Offset(i, 2) = oComment.Initial    
  .Offset(i, 3) = oComment.Author
  .Offset(i, 4) = Format(oComment.Date, "mm/dd/yyyy")
  .Offset(i, 5) = oComment.Range
  .Offset(i, 6) = rngHeading.ListFormat.ListString & " " & rngHeading.Text
Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM