【问题标题】:On keyword return control to calling program在关键字返回控制到调用程序
【发布时间】:2016-01-23 14:21:12
【问题描述】:

在幻灯片中第一次出现关键字后,我希望被调用程序结束并将控制权返回给调用程序,以便它移动到下一张幻灯片。

这里的 Exit Sub 不起作用,幻灯片中所有出现的关键字都会显示 MsgBox。

Option Explicit
Global sldmissed As Slide
Global c As Long

Sub Highlightkeywords()
 Dim Pres As Presentation
 Dim shp As Shape
 c = 0
 For Each Pres In Application.Presentations
      For Each sldmissed In Pres.Slides
         For Each shp In sldmissed.Shapes
             Call Keywords(shp)
         Next shp
     Next sldmissed
 Next Pres
 MsgBox c

End Sub

Sub Keywords(shp As Object)

    Dim txtRng As TextRange
    Dim rngFound As TextRange
    Dim I, K, X, n As Long
    Dim iRows As Integer
    Dim iCols As Integer
    Dim TargetList

    TargetList = Array("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st", "etc", ":00", ".00", "a.m.", "p.m.", "number", "US", "USA", "$")

    With shp

        If shp.HasTable Then

            For iRows = 1 To shp.Table.Rows.Count
                For iCols = 1 To shp.Table.Rows(iRows).Cells.Count
                    Set txtRng = shp.Table.Rows(iRows).Cells(iCols).Shape.TextFrame.TextRange
                    For I = LBound(TargetList) To UBound(TargetList)
                        Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                        Do While Not rngFound Is Nothing
                            n = rngFound.Start + 1
                            With rngFound
                                If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                                    sldmissed.Select
                                    c = c + 1
                                    MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                                    Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)

                                    **GoTo Normalexit**
                                Else
                                    **GoTo Normalexit**

                                End If
                            End With
                        Loop
                    Next
                Next
            Next

        End If

    End With


    Select Case shp.Type
     Case msoTable

     Case msoGroup
        For X = 1 To shp.GroupItems.Count
            Call Keywords(shp.GroupItems(X))
        Next X

     Case 21
         For X = 1 To shp.Diagram.Nodes.Count
           Call Keywords(shp.GroupItems(X))
         Next X

     Case Else

        If shp.HasTextFrame Then
            Set txtRng = shp.TextFrame.TextRange
            For I = LBound(TargetList) To UBound(TargetList)
                Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                Do While Not rngFound Is Nothing
                    n = rngFound.Start + 1
                    With rngFound
                        If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                            sldmissed.Select
                            c = c + 1
                            MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                            Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)
                            **GoTo Normalexit**

                        Else
                            **GoTo Normalexit**

                        End If
                    End With
                Loop
            Next
        End If

    End Select

Normalexit:
Exit Sub

End Sub

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:
    Option Explicit
    Global sldmissed As Slide
    Global c As Long
    Sub Highlightkeywords()
     Dim Pres As Presentation
     Dim shp As Shape
     c = 0
     For Each Pres In Application.Presentations
          For Each sldmissed In Pres.Slides
             For Each shp In sldmissed.Shapes
                If keywords(shp) Then
                Exit Sub
             Next shp
         Next sldmissed
     Next Pres
    
    End Sub
    
    Function keywords(shp As Object) As Boolean
    
        Dim txtRng As TextRange
        Dim rngFound As TextRange
        Dim I, K, X, n As Long
        Dim iRows As Integer
        Dim iCols As Integer
        Dim TargetList
    
        keywords = True
    
    
        TargetList = Array("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st", "etc", ":00", ".00", "a.m.", "p.m.", "number", "US", "USA", "$")
    
        With shp
    
        If shp.HasTable Then
    
                    For iRows = 1 To shp.Table.Rows.Count
                        For iCols = 1 To shp.Table.Rows(iRows).Cells.Count
                            Set txtRng = shp.Table.Rows(iRows).Cells(iCols).Shape.TextFrame.TextRange
                                     For I = LBound(TargetList) To UBound(TargetList)
                                        Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                                        Do While Not rngFound Is Nothing
                                        n = rngFound.Start + 1
                                        With rngFound
                                            If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                                                sldmissed.Select
                                                c = c + 1
                                                MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                                                Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)
    
                                                keywords = False
                                                GoTo Normalexit
    
    
                                            Else
                                                 keywords = False
                                                GoTo Normalexit
    
    
    
                                        End If
                                 End With
                                Loop
                            Next
                        Next
                    Next
    
                End If
    
                End With
    
    
               Select Case shp.Type
                Case msoTable
    
    
                Case msoGroup
                    For X = 1 To shp.GroupItems.Count
                        Call keywords(shp.GroupItems(X))
                    Next X
    
                Case 21
                    For X = 1 To shp.Diagram.Nodes.Count
                        Call keywords(shp.GroupItems(X))
                    Next X
    
                Case Else
    
                    If shp.HasTextFrame Then
                    Set txtRng = shp.TextFrame.TextRange
                        For I = LBound(TargetList) To UBound(TargetList)
                           Set rngFound = txtRng.Find(FindWhat:=TargetList(I), MatchCase:=True, wholewords:=True)
                           Do While Not rngFound Is Nothing
                                 n = rngFound.Start + 1
                                 With rngFound
                                   If rngFound.Font.Color.RGB = RGB(255, 0, 0) Then
                                            sldmissed.Select
                                            c = c + 1
                                            MsgBox "Slide: " & sldmissed.SlideNumber, vbInformation
                                            Set rngFound = txtRng.Find(TargetList(I), n, MatchCase:=True, wholewords:=True)
                                            keywords = False
                                            GoTo Normalexit
    
    
                                    Else
                                            keywords = False
                                            GoTo Normalexit
    
    
    
    
                                        End If
                                 End With
                            Loop
                        Next
                      End If
    
        End Select
    
    Normalexit:
    End Function
    

    这是你说的吗?

    【讨论】:

    • 是的 - 这看起来像我的建议。
    • 您能否详细说明为什么它不起作用?没有运行或没有给出预期的结果?
    【解决方案2】:

    exit sub 将退出Sub Keywords 并将控制权返回给Sub Highlightkeywords(),这将继续循环。

    你可能想把Sub Keywords 变成Function Keywords As Boolean 然后在Keywords函数的开头设置Keywords = true,在GoTo Normalexit之前设置keyworkds = false

    还有,这个:

    Normalexit:
    Exit Sub
    
    End Sub
    

    可以改成这样:

    Normalexit:
    End Function
    

    在您的代码中,Exit SubEnd Sub 没有任何不同,因为 End Sub 会在没有退出的情况下直接调用并仍然退出。

    您正在处理Function Keywords 的结果吗?

    修改

    Sub Highlightkeywords()中,更改对关键字的调用以处理结果。

    For Each Pres In Application.Presentations
          For Each sldmissed In Pres.Slides
             For Each shp In sldmissed.Shapes
                 if Keywords(shp) then
                   exit sub
             Next shp
         Next sldmissed
     Next Pres
    

    修改后的 2

    只需重新阅读您想要的内容。也许这就是你要找的东西?回答最初认为调用程序是调用这个程序的程序 - 但也许你的意思是一旦找到关键字就让它移动到演示文稿中的下一张幻灯片?

    For Each Pres In Application.Presentations
          For Each sldmissed In Pres.Slides
             For Each shp In sldmissed.Shapes
                 if Keywords(shp) then break 'This will go to next slide
             Next shp
         Next sldmissed
     Next Pres
    

    【讨论】:

    • 感谢您的帮助伙伴。但这也行不通。我将 Sub 关键字更改为函数,也更改了 normalexit。但它不起作用。你能帮我修改一下代码吗?
    • 你在处理Function Keywords的结果吗? (没有意识到这将很难阅读 - 改为修改答案)对于每个 Pres 在 Application.Presentations 对于每个 sldmissed 在 Pres.Slides 对于每个 shp 在 sldmissed.Shapes 如果关键字(shp)然后退出 sub Next shp Next sldmissed下一新闻
    • 你说的是这个吗??
    • 再次抱歉。我已经发布了修改后的代码。没看懂函数关键字的概念。
    • 明白了,非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2017-01-16
    • 2020-01-19
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多