首页 > 解决方案 > 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 的单独子设置。

标签: vbams-word

解决方案


那将是一个非常糟糕的主意。使用 Selection 对象只会进一步减慢操作,并让用户诅咒你。

由于您似乎已经有了进度指示器,因此您正在尽一切努力让用户了解情况。

任何涉及滚动文档的操作都会使您的代码运行得更慢,因此您能做的最好的事情就是确保您的代码在尽可能短的时间内完成。这包括避免使用该Selection对象和关闭ScreenUpdating


推荐阅读