Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

excel - How to Capture Paragraph heading and Page number of Comment in MS Word

I have this VB code that extracts comment and Author name from MS Word to Excel, How do I capture Paragraph heading and Page number of comment? I want my excel to have Author name, Comment, paragraph heading and page number.

Here's my code

Imports Microsoft.Office

Imports Microsoft.Office.Interop

Imports Microsoft.Office.Interop.Word

Public Class Form1 Dim fileName As String = "None"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If fileName = "None" Then
        MsgBox("Please select a file", vbExclamation, "Info")
        Return
    End If

    Dim oWord As Object ' Word.Application
    Dim oDoc As Object ' Word.Document

    oWord = CreateObject("Word.Application")
    oWord.Visible = False


    oWord.Documents.Open(fileName)
    oWord.Visible = False

    Dim ExcelApp = CreateObject("Excel.Application")
    Dim WorkBooks = ExcelApp.Workbooks
    Dim Book = WorkBooks.Add
    Dim Sheets = Book.Sheets
    Dim Sheet = Sheets(1)

    Sheet.Range("A1").Value = "Comment"
    Sheet.Range("B1").Value = "Author"
    Sheet.Range("C1").Value = "Date"

    Sheet.Columns("A").ColumnWidth = 50
    Sheet.Columns("B").ColumnWidth = 30
    Sheet.Columns("C").ColumnWidth = 30

    Dim I As Integer = 1
    For Each oDoc In oWord.Documents
        For Each cmt In oDoc.Comments
            I += 1
            Sheet.Range("A" & I).Value = cmt.Range.Text
            Sheet.Range("B" & I).Value = cmt.Author
            Sheet.Range("C" & I).Value = cmt.Comments().Date
        Next
    Next

    ExcelApp.Visible = True
    Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub

Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
    Dim OFD As New OpenFileDialog()
    OFD.Title = "Select your Word file"
    ' OFD.Filter = "Word Files (*.txt)|*.txt"

    If OFD.ShowDialog() <> DialogResult.Cancel Then
        fileName = OFD.FileName
        If Len(fileName) > 27 Then
            lblSelectedFile.Text = Strings.Left(fileName, 7) & " ... " & Strings.Right(fileName, 13)
        Else
            lblSelectedFile.Text = fileName
        End If
    Else
        Return
    End If

End Sub

Private Sub lblSelectedFile_Click(sender As Object, e As EventArgs) Handles lblSelectedFile.Click

End Sub

End Class

question from:https://stackoverflow.com/questions/66059312/how-to-capture-paragraph-heading-and-page-number-of-comment-in-ms-word

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For example, in VBA:

        Sheet.Range("D" & I).Value = cmt.Reference.Information(1) 'wdActiveEndAdjustedPageNumber
        Sheet.Range("E" & I).Value = cmt.Reference.Bookmarks("Headinglevel").Range.Paragraphs.First.Range.Text

I'll leave it to you to do the vb.net equivalent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...