2017-06-02 12 views
0

我目前正在編寫一個vba宏,它應該抓住文檔中的所有註釋並將它們返回到新創建的excel文件中。我差不多完成了,但遇到了段落指示問題。我想把段落相應的標題放在excel中。爲了做到這一點,我必須直接得到段落標題,或者在段落上方找到第一個與標題相關的格式。至少這些是我能想到的選項。任何想法如何最好地解決這個問題?如何使用vba返回單詞上面第一個標題的值?

Sub exportComments() 

Dim xlApp As Object 
Dim xlWB As Object 
Dim i As Integer, HeadingRow As Integer 
Dim objPara As Paragraph 
Dim objComment As Comment 
Dim strSection As String 
Dim strTemp 
Dim myRange As Range 
Set xlApp = CreateObject("Excel.Application") 
xlApp.Visible = True 
Set xlWB = xlApp.Workbooks.Add 'create a new workbook 
With xlWB.Worksheets(1) 
' Create Heading 
    HeadingRow = 1 
    .Cells(HeadingRow, 1).Formula = "Comment" 
    .Cells(HeadingRow, 2).Formula = "Page" 
    .Cells(HeadingRow, 3).Formula = "Paragraph" 
    .Cells(HeadingRow, 4).Formula = "Commented part" 
    .Cells(HeadingRow, 5).Formula = "Comment" 
    .Cells(HeadingRow, 6).Formula = "Reviewer" 
    .Cells(HeadingRow, 7).Formula = "Date" 
    strSection = "preamble" 'all sections before "1." will be labeled as "preamble" 
    strTemp = "preamble" 
    If ActiveDocument.Comments.Count = 0 Then 
     MsgBox ("No comments") 
     Exit Sub 
    End If 
    For i = 1 To ActiveDocument.Comments.Count 
     Set myRange = ActiveDocument.Comments(i).Scope 
     strSection = ParentLevel(myRange.Paragraphs(1)) 
     'MsgBox strSection 
     'Comment line 
     .Cells(i + HeadingRow, 1).Formula = ActiveDocument.Comments(i).Index 
     'Page number line 
     .Cells(i + HeadingRow, 2).Formula = ActiveDocument.Comments(i).Reference.Information(wdActiveEndAdjustedPageNumber) 
     'Paragraph indicator line 
     .Cells(i + HeadingRow, 3).Formula = ActiveDocument.Comments(i).Scope.Paragraphs(1) 
     'Commented part line 
     .Cells(i + HeadingRow, 4).Formula = ActiveDocument.Comments(i).Scope.FormattedText 
     'Comment value line 
     .Cells(i + HeadingRow, 5).Formula = ActiveDocument.Comments(i).Range 
     'Comment reviewer line 
     .Cells(i + HeadingRow, 6).Formula = ActiveDocument.Comments(i).Author 
     'Comment date line 
     .Cells(i + HeadingRow, 7).Formula = Format(ActiveDocument.Comments(i).Date, "dd/MM/yyyy") 
    Next i 
End With 
Set xlWB = Nothing 
Set xlApp = Nothing 
End Sub 

Function ParentLevel(Para As Word.Paragraph) As String 
    ' Finds the first paragraph of the current section 
    Dim oSection As Section 
    Dim iSection As Integer 
    Dim lngPara As Long 
    Dim oRng As Range, oPara As Range 
     iSection = Para.Range.Information(wdActiveEndSectionNumber) 
     Set oSection = ActiveDocument.Sections(iSection) 
     Set oRng = oSection.Range 
     For lngPara = 1 To oRng.Paragraphs.Count 
      Set oPara = oRng.Paragraphs(lngPara).Range 
      If Len(oPara) > 1 Then 
       Exit For 
      End If 
     Next lngPara 
     oPara.End = oPara.End - 1 
     ParentLevel = oPara.Text 
    End Function 

這樣的想法是把段落標題在Headingrow 3.該解決方案必須適應不同的頭格式,我經常有自制的頭格式工作的文件。我唯一可以信賴的就是在樣式名稱中包含單詞標題的標題。任何幫助將不勝感激,當然我可以添加更多的信息可能會失蹤。

+0

如果需要此信息,我使用word 2016 –

回答

1

你在正確的軌道上,看起來相當有能力編寫VBA,所以這個答案比確定性更具顧問性。

識別樣式名稱中的「標題」可能是一個選項,但前提是您可以依賴樣式進行正確命名以適應此情況。在變量易變的情況下(可能發生不可預知的變化),有一種解決方案通常不需要太多的開發:提示用戶在運行宏時提供這些信息!

在你的情況中,你提到頭文件通常具有自定義格式,你可以獲取使用的格式,並用UserForm提示用戶標識哪些格式用於段落標題。通過使用文件Styles,這些更容易在VBA訪問:

Sub getStyles() 
    Dim UsedStyles As New Collection 
    Dim pgf As Paragraph 

    For Each pgf In ActiveDocument.Paragraphs 
     UsedStyles.Add pgf.Style.NameLocal 
    Next pgf 
End Sub 

在文檔中的所有段落這將循環,並創建包含的範圍內使用的所有樣式的名稱唯一列表(Collection)該文件。然後,您可以將其傳遞給具有MultiSelect ListBox的UserForm,指示用戶選擇哪些樣式用於標題。將用戶選擇返回到您的宏,並將其用作比較來查找標題。

+0

這確實是一個非常好的選擇,甚至可能具有更多的可用性,正如我自己所想的那樣。這對於向程序添加所需的功能肯定有幫助。我想感謝你這個明確的答案,並對我的反應深表歉意。 –

+0

@AlwinG沒問題,總是值得等待幾天,看看是否有其他答案!用戶提示當然是提高可用性的好方法。正確的做法有點額外的工作,但它可以讓你遠離更多的工作。沒有比運行一個你認爲可以依靠快速完成工作的宏更糟糕的感覺了,而是發現你必須調試並重寫它的一部分! – Carrosive

相關問題