excel - Excel VBA:从 Word 文档导入注释
问题描述
我有一个使用“运行时错误 4160,文件名错误”进行调试的代码。
我桌面上的同一文件夹中有 2 个文件:sample.docx 和 Import Comments.xlsm。
我的 Excel 文件中有两个活动工作表,i)带按钮的输入(Word 文档的文件路径)和 ii)评论(有 4 列;ID;日期;作者;评论)。
我从http://pharma-sas.com/how-to-retrieve-comments-from-word-document-using-vba/借用了代码并以 1:1 的比例使用它 -> 除了引入相对路径(原始代码也给出了相同的错误)。
我正在使用 Excel 2013。
Sub Import_Comments_Click()
' Create Object
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' Open word file with comments
Dim WordNam As String
WordNam = ThisWorkbook.Path & "sample.docx"
objWord.Documents.Open WordNam
ThisWorkbook.Worksheets(2).Activate
' Clear comments extracted previously
For i = 2 To ActiveSheet.UsedRange.Rows.Count
For j = 1 To ActiveSheet.UsedRange.Columns.Count
Cells(i, j) = ""
Next j
Next i
' Start row
a = 2
' Start For loop --> This debugs and gives the Runtime error 4160 - Bad File name
**For i = 1 To objWord.Documents(WordNam).Comments.Count**
' Sequence Number
ActiveSheet.Cells(a, 1) = a - 1
ActiveSheet.Cells(a, 1).HorizontalAlignment = xlCenter
' Create date of comment
ActiveSheet.Cells(a, 2) = Format(objWord.Documents(WordNam).Comments(i).Date, "Short Date")
ActiveSheet.Cells(a, 2).HorizontalAlignment = xlLeft
' Author of Comment
ActiveSheet.Cells(a, 3) = objWord.Documents(WordNam).Comments(i).Author
ActiveSheet.Cells(a, 3).WrapText = True
' Comment
ActiveSheet.Cells(a, 4) = objWord.Documents(WordNam).Comments(i).Range.Text
ActiveSheet.Cells(a, 4).WrapText = True
a = a + 1
Next i
Columns("A").ColumnWidth = 5
Columns("C").ColumnWidth = 18
Columns("D").ColumnWidth = 70
' Close word file with comments
objWord.Documents.Close
objWord.Quit
End Sub
我有人可以帮助我调查此运行时错误的原因。
一切顺利,安德烈亚斯
解决方案
推荐阅读
- r - 从文件链接中提取目录链接
- node.js - when to disconnect and when to end a pg client or pool
- split - 如何在Stata中拆分观察值
- javascript - Plotly.js 中单个图的多个 X 轴
- c# - 如何使用 ML.NET 预测整数值?
- c - 字符串操作(具有相同输入的变量输出)
- image - 输入图像大小 CNN Tensorflow
- javascript - React 中动态渲染组件的大小写错误
- string - 使用 PowerShell 计算 log.file 中字符串的出现次数
- objective-c - 如何检查 NSUserDefaults 中的 NSArray 是否包含特定的 NSString