vba - Microsoft Word:选择并转到 InlineShape
问题描述
我的 Word 文档中的宏遍历所有图形(内联形状)并更新链接和值。目前,文档在“请稍候”表单对话框后面的整个过程中冻结。理想情况下,我会让代码在整个文档中移动,以向用户展示实际正在发生的事情。
如何在下面的循环中选择并转到当前的内联形状?
Private Sub UpdateFields()
PleaseWait.bar.Width = 0
PleaseWait.Show
' This routine sets the new path for external links, pointing them to the current folder.
Dim Rng As Range, Fld As Field, Shp As Shape, iShp As InlineShape, i As Long
Dim no_of_steps As Integer
Dim single_step_width As Integer
no_of_steps = 0
With ThisDocument
' Create progress bar
' a) Count total number of steps
For Each Rng In .StoryRanges
For Each iShp In Rng.InlineShapes
no_of_steps = no_of_steps + 1
Next iShp
Next Rng
' b) Divide full width of progress frame by number of steps
single_step_width = PleaseWait.frame.Width \ no_of_steps
' Go through all story ranges in the document.
For Each Rng In .StoryRanges
' Go through the inlineshapes in the story range.
For Each iShp In Rng.InlineShapes
With iShp
' Skip over inlineshapes that don't have links to external files.
If Not .LinkFormat Is Nothing Then
With .LinkFormat
' Skip links already set to current folder
If Not .SourceFullName = ThisDocument.Path & "\datagrunnlag.xlsm" Then
' Replace the link to the external file
.SourceFullName = ThisDocument.Path & "\datagrunnlag.xlsm"
On Error Resume Next
.AutoUpdate = False
.Update
On Error GoTo 0
End If
End With
End If
' Update progress bar with completed step
PleaseWait.bar.Width = PleaseWait.bar.Width + single_step_width
DoEvents
End With
Next iShp
Next Rng
End With
End Sub
编辑 05.12.2020:在子目录中添加了所有代码。ScreenUpdating = False 和 True 由 MacroEntry 和 MacroExit 的单独子设置。
解决方案
那将是一个非常糟糕的主意。使用 Selection 对象只会进一步减慢操作,并让用户诅咒你。
由于您似乎已经有了进度指示器,因此您正在尽一切努力让用户了解情况。
任何涉及滚动文档的操作都会使您的代码运行得更慢,因此您能做的最好的事情就是确保您的代码在尽可能短的时间内完成。这包括避免使用该Selection
对象和关闭ScreenUpdating
。
推荐阅读
- c# - 如何在 razor 视图引擎中获取集合中项目的元数据?
- html - 除非子文本下降到两行,否则 DIV 上方的额外空间?
- python-3.x - Python 从 FTP 服务器下载最新的 CSV
- python - 使用 Django 用户模型
- c# - 构建错误 TFS 2015“没有适当的映射存在...”到映射之外的代码
- python - 测试准确度为 98% 的模型的混淆矩阵不准确
- reactjs - ReactJS 工作流程以及如何捕获按钮点击
- javascript - 检测chrome是否正在加载缓存页面?
- java - 有没有办法从 URL 字节流的特定位置开始读取?
- postgresql - 为什么按月“分组”不起作用?