【问题标题】:How to Capture Paragraph heading and Page number of Comment in MS Word如何在 MS Word 中捕获段落标题和注释页码
【发布时间】:2021-02-05 07:36:07
【问题描述】:

我有这个 VB 代码,它可以将评论和作者姓名从 MS Word 提取到 Excel,我如何捕获段落标题和评论页码?我希望我的 Excel 中有作者姓名、评论、段落标题和页码。

这是我的代码

导入 Microsoft.Office

导入 Microsoft.Office.Interop

导入 Microsoft.Office.Interop.Word

公开课表1 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

结束类

【问题讨论】:

    标签: excel vb.net ms-word


    【解决方案1】:

    例如,在 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
    

    我会留给你做 vb.net 等价物。

    【讨论】:

    • 它在段落上给出错误:System.Runtime.InteropServices.COMException: '所请求的集合成员不存在。'
    • 已修订。请注意,它是 VBA 代码 - 您需要实现 vb.net 等价物(我不熟悉语法)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2021-07-16
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多