Results 1 to 4 of 4

Thread: [Word] MS Word: VBA macro works normally in only debug mode

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2022
    Posts
    2

    Post [Word] MS Word: VBA macro works normally in only debug mode

    The following code works perfectly in debug mode by transforming each table in the document.

    However, unknowingly, the normal run mode transforms the tables in the first page only while keeping the tables in the following pages in their original state. Sometimes, the normal run mode can even skip the tables in the first page if the document is one-page length.

    So, what might be the issue cause?



    Code:
    Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As LongPtr)
    
    Sub Macro1()
        
    Dim itable As Table
        
    For Each itable In ThisDocument.Tables
    
        If itable.Rows.Count = 11 _
           And itable.Rows(1).Shading.BackgroundPatternColor = 15189684 _
           Then
            
            With itable
                
                .AutoFitBehavior (wdAutoFitWindow)
                
                .Cell(1, 1).PreferredWidthType = wdPreferredWidthPoints
                .Cell(1, 1).PreferredWidth = 75
                
            End With
    
        End If
    
    Sleep 1000
    
    Next
    
    Debug.Print "The End"
    
    ThisDocument.Save
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,621

    Re: [Word] MS Word: VBA macro works normally in only debug mode

    you could try replacing sleep with doevents
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2022
    Posts
    2

    Re: [Word] MS Word: VBA macro works normally in only debug mode

    Replacing
    Code:
    Sleep
    with
    Code:
    DoEvents
    doesn't help and the code behaves the same old way.

    For errors, I can execute flawlessly each line in debug mode, and the output is as expected without any errors.
    The problem lies in normal mode or when hitting F5 on Windows.

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,140

    Re: [Word] MS Word: VBA macro works normally in only debug mode

    Add some MsgBox statements at each stage of the macro to see where the code is actually getting to and what it it processing during normal mode, after the For, the If, and the Then, and before the Sleep.

Posting Permissions

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



Click Here to Expand Forum to Full Width