excel - 从 Word 中提取作者和评论到 Excel

标签 excel vba ms-word

我有以下代码,它从 Word 文档中提取文本字符串并将其导出到 Excel 电子表格中。 A 列读取注释,B 列读取它所引用的原始文本。我还想提取在电子表格中发表评论但不知道如何提取的作者。谢谢。

    Option Explicit




   Public Sub FindWordComments()
'Requires reference to Microsoft Word v14.0 Object Library

Dim objExcelApp As Object
Dim wb As Object
Set objExcelApp = CreateObject("Excel.Application")
Set wb = objExcelApp.Workbooks.Open("C:\Users\cetraig\Documents\Projects\_HRBT\Book1.xlsx")

Dim myWord              As Word.Application
Dim myDoc               As Word.Document
Dim thisComment         As Word.Comment

Dim fDialog             As Office.FileDialog
Dim varFile             As Variant

Dim destSheet           As Worksheet
Dim rowToUse            As Integer
Dim colToUse            As Long

Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Set destSheet = wb.Sheets("Sheet1")
colToUse = 1

With fDialog
    .AllowMultiSelect = True
    .Title = "Import Files"
    .Filters.Clear
    .Filters.Add "Word Documents", "*.docx"
    .Filters.Add "Word Macro Documents", "*.docm"
    .Filters.Add "All Files", "*.*"
End With

If fDialog.Show Then

    For Each varFile In fDialog.SelectedItems

        rowToUse = 2

        Set myWord = New Word.Application
        Set myDoc = myWord.Documents.Open(varFile)

        For Each thisComment In myDoc.Comments

            With thisComment
                destSheet.Cells(rowToUse, colToUse).Value = .Range.Text
                destSheet.Cells(rowToUse, colToUse + 1).Value = .Scope.Text
                destSheet.Columns(2).AutoFit
            End With

            rowToUse = rowToUse + 1

        Next thisComment

        destSheet.Cells(1, colToUse).Value = Left(myDoc.Name, 4)
        'Put name of interview object in cell A1

        destSheet.Cells(1, colToUse + 1).Value = ActiveDocument.Words.Count
        'Put the number of words in cell B1

        Set myDoc = Nothing
        myWord.Quit

        colToUse = colToUse + 2

    Next varFile

    End If

    End Sub

最佳答案

添加一行:

destSheet.Cells(rowToUse, colToUse + 2).Value = .Author

应该做的伎俩

(假设您希望它在下一列结束)

关于excel - 从 Word 中提取作者和评论到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803103/

相关文章:

python - Openpyxl如何按索引从工作表中获取行

python - 如何使用 Python 写入 Excel 电子表格?

vba - 如何在 Excel VBA 中组合 FormulaArray 和 FormulaLocal 选项?

powershell - 如何通过Powershell永久保存Word选项/自动更正设置?

java - 无法使用 HttpServletResponse 下载文件

vba - 如何在VBA代码中通过名称引用Word字段?

vba - vba复制粘贴失败

excel - 找不到 OleDB 数据提供程序 VBA/Excel

vba - 在 Word 中更改自定义文档属性

java - 使用 xlsx 在 R 中打开 Excel 文件时出错