|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub AddCommentsToWordALL()
Dim wbApp As Object ' Word Application
Dim wbDoc As Word.Document ' Word Document
Dim shp As Object ' Word Shape
Dim fDialog As Object ' File Dialog
Dim strFileName As Variant
Dim i As Integer
Dim ct As Integer
Dim obj As Word.InlineShape
Dim par As Word.Paragraph
Dim inobj As Object
' 创建Word应用程序对象
Set wbApp = CreateObject("Word.Application")
wbApp.Visible = True ' 可视化Word应用,以便调试或查看进度
' 设置文件对话框
Set fDialog = wbApp.Application.fileDialog(msoFileDialogFilePicker)
' 设置过滤器只显示Word文档
With fDialog
.Title = "Select Word Documents"
.Filters.Clear
.Filters.Add "Word Documents", "*.doc;*.docx;*.docm"
.AllowMultiSelect = True
End With
' 显示文件对话框
If fDialog.Show Then
For Each strFileName In fDialog.SelectedItems
' 打开选定的文档
Set wbDoc = wbApp.Documents.Open(strFileName)
ct = wbDoc.InlineShapes.count
' 在处理InlineShapes时,找出一行多个图片的情况
For i = 1 To ct
If i <= ct Then ' 防止越界
Set inobj = wbDoc.InlineShapes(i)
If inobj.Type = wdInlineShapePicture Then
If i + 1 <= wbDoc.InlineShapes.count Then ' 检查下一个元素是否存在
If wbDoc.InlineShapes(i + 1).Type = wdInlineShapePicture Then
If inobj.Range.Information(wdVerticalPositionRelativeToPage) = _
wbDoc.InlineShapes(i + 1).Range.Information(wdVerticalPositionRelativeToPage) Then
'On Error GoTo ErrorHandler
Debug.Print "i = " & i; " start = " & inobj.Range.Start; " End = " & inobj.Range.End
wbDoc.Comments.Add Range:=inobj.Range, Text:="aaaa"
End If
End If
End If
End If
End If
'ErrorHandler:
'Debug.Print "文件 " & strFileName & " 发生错误: " & Err.Description; " i =" & i
'Err.Clear
'Resume Next
Next i
' 关闭文档,保存更改
wbDoc.Close True
Next strFileName
End If
' 关闭Word应用程序
wbApp.Quit
' 清理对象引用
Set wbDoc = Nothing
Set wbApp = Nothing
Set fDialog = Nothing
End Sub
我想再Excel中运行,判断word文档中的图片,如果是相邻两张图片在同一行上,则增加一个批注,但是运行到 wbDoc.Comments.Add Range:=inobj.Range, Text:="aaaa",总是报自动化错误,请大神给指导一下
|
|