Consulting

Results 1 to 7 of 7

Thread: How to introduce delay so that user can see the changes being made?

  1. #1
    VBAX Regular
    Joined
    Mar 2021
    Posts
    8
    Location

    How to introduce delay so that user can see the changes being made?

    I have code that removes extra spaces. The user is prompted for each change. When they user agrees to the change, I would like to make the change, then pause for 1/2 second to allow the user to see what the change is before moving on to the next item to correct. This would allow them to cancel out of the program and undo the change if they disagree with it.

    How can I introduce this 1/2 second delay?


      ' Turn on screen updating
      Application.ScreenUpdating = True
    
      ' Remove spaces before paragraph marks
      mySearchRegExpression = " {1,}^013"
      With Selection.Find
            .ClearFormatting
        
        Do While .Execute(findText:=mySearchRegExpression, MatchWildcards:=True, Forward:=True, Wrap:=wdFindContinue) = True
          result = MsgBox("Remove extra space(s)?", vbYesNoCancel)
          If result = vbYes Then
                Selection.TypeParagraph ' This works just like hitting the enter key
          ElseIf result = vbNo Then
                ' MsgBox "You clicked No"
          Else
                GoTo EndOfFunction
          End If
          
          findCounter = findCounter + 1
        Loop
      End With

  2. #2
    VBAX Tutor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    279
    Location
    I'm not sure if this will be of any help?

    https://answers.microsoft.com/en-us/...6-538d12e37424

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,625
    Location
        Do While .Execute(findText:=mySearchRegExpression, MatchWildcards:=True, Forward:=True, Wrap:=wdFindContinue) = True
          'Set Variable to  "  " location in Selection
          result = MsgBox("Remove extra space(s)?", vbYesNoCancel)
          If result = vbYes Then
                Selection.TypeParagraph ' This works just like hitting the enter key
          ElseIf result = vbNo Then
                ' Replace Variable (" ") with  "  "
          End If
    Loop
    Last edited by SamT; 04-11-2021 at 09:56 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Posts
    7,828
    Location
    One way


    Option Explicit
    
    
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    
    Sub Wait()
    
    
        Msgbox "Before"
        Sleep 500
        Msgbox "After"
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    The process will pause until you respond to the message box. Your problem appears to be that you can't see the found item. This should fix it.

           Do While .Execute(findText:=mySearchRegExpression, _                          MatchWildcards:=True, _
                              Forward:=True, _
                              Wrap:=wdFindContinue) = True
                Application.ScreenRefresh
                Result = MsgBox("Remove extra space(s)?", vbYesNoCancel)
                If Result = vbYes Then
                    Selection.TypeParagraph    ' This works just like hitting the enter key
                ElseIf Result = vbNo Then
                    GoTo NextFind
                Else
                    GoTo EndOfFunction
                End If
                findcounter = findcounter + 1
    NextFind:
            Loop
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    Mar 2021
    Posts
    8
    Location
    Thank you, all! The solution that worked for me ended up being a combination of the suggestions you all provided. Changes I made are in red below. As a result, the change is made, the technique pauses for a bit, then moves onto selecting the next item that is proposed to be changed.

    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
      ' Turn on screen updating
      Application.ScreenUpdating = True
    
      ' Remove spaces before paragraph marks
      mySearchRegExpression = " {1,}^013"
      With Selection.Find
            .ClearFormatting
        
        Do While .Execute(findText:=mySearchRegExpression, MatchWildcards:=True, Forward:=True, Wrap:=wdFindContinue) = True
          result = MsgBox("Remove extra space(s)?", vbYesNoCancel)
          If result = vbYes Then
                Selection.TypeParagraph ' This works just like hitting the enter key
                Application.ScreenRefresh
                Sleep 250 ' Give user time to see the change
          ElseIf result = vbNo Then
                ' MsgBox "You clicked No"
          Else
                GoTo EndOfFunction
          End If
          
          findCounter = findCounter + 1
        Loop
      End With

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,311
    Location
    I'd approach this rather differently:
    Sub Demo()
    Dim Rslt
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Text = "^w^p"
        .Wrap = wdFindStop
        .Forward = True
        .MatchWildcards = False
      End With
      Do While .Find.Execute = True
        .Select
        Rslt = MsgBox("Remove extra space(s)?", vbYesNoCancel)
        Select Case Rslt
          Case vbYes
            .Text = vbCr
            Application.ScreenRefresh
            If MsgBox("Keep This Change?", vbYesNo) = vbNo Then ActiveDocument.Undo
          Case vbNo
          Case vbCancel: Exit Do
        End Select
        .Collapse wdCollapseEnd
      Loop
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •