Screenupdating (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
Hi all,
Word problem for a change.;)

I have some code below, that allows me to add extra text to a set of letters created before I joined this firm.

As you will see I have 'Application.ScreenUpdating = False' in the code, yet the screen shows all of the processing when the code is editing the documents?

What else can I try please?

Code:
Sub InsertText()
Dim Shp As Shape, Doc As Document, strTextToInsert As String, strTextToFind As String
Dim i As Long, docToOpen As FileDialog, sHght As Single
Dim rngToSearch As Word.Range
Dim DataObj As New MSForms.DataObject
    
    On Error GoTo Err_Exit
    'strText = InputBox("New Text", "Header Textbox Update", "New Text")
    
    ' Switch off the updates of screen
    Application.ScreenUpdating = False
    
    ' Set the text
    strTextToInsert = "Annual bonus rates for the last five years"
    strTextToFind = "Discharge Pack"
    
    DataObj.SetText strTextToInsert
    DataObj.PutInClipboard
    
    Set docToOpen = Application.FileDialog(msoFileDialogFilePicker)
    docToOpen.Show
    For i = 1 To docToOpen.SelectedItems.Count
        'Open each document
        Set Doc = Documents.Open(FileName:=docToOpen.SelectedItems(i))
            Selection.Find.ClearFormatting
        With Selection.Find
            .Text = strTextToFind
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeParagraph
        Selection.PasteAndFormat (wdListCombineWithExistingList)
        ' The above inserts a line which creates a second page, so delete a line after
        Selection.EndKey Unit:=wdStory
        Selection.Delete Unit:=wdCharacter, Count:=1

        ActiveDocument.Close SaveChanges:=wdSaveChanges
    Next
    
    Set docToOpen = Nothing: Set Doc = Nothing
    ' Switch Screen updates back on
    Application.ScreenUpdating = True
    Exit Sub
Err_Exit:
    MsgBox Err.Description & Err.Number
End Sub
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 09:29
Joined
Apr 27, 2015
Messages
5,125
This is from MSDN's site:
The ScreenUpdating property controls most display changes on the monitor while a procedure is running. When screen updating is turned off, toolbars remain visible and Word still allows the procedure to display or retrieve information using status bar prompts, input boxes, dialog boxes, and message boxes. You can increase the speed of some procedures by keeping screen updating turned off. You must set the ScreenUpdating property to True when the procedure finishes or when it stops after an error.

Not an expert but I think the Application.Echo method might be a better choice?
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
Sorry NauticalGent, you have me confused. Easily done I admit.

I googled application.echo and it appears to be for Access?
The code above is solely in a word document for inserting text. Access is not involved.
The application.screenupdating works in Excel, not sure why it does not in Word?
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 09:29
Joined
Apr 27, 2015
Messages
5,125
Could be...I was unaware it was exclusive to Access. I was under the impression that it was VBA and worked on all Office platforms.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
OK, I'll give it a try tomorrow.
Thanks
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
No, that did not work.
I get method or property not found.

Tried
Code:
Sub InsertText()
Dim Shp As Shape, Doc As Document, strTextToInsert As String, strTextToFind As String
Dim i As Long, docToOpen As FileDialog, sHght As Single
Dim rngToSearch As Word.Range
Dim DataObj As New MSForms.DataObject
    
    On Error GoTo Err_Exit
    'strText = InputBox("New Text", "Header Textbox Update", "New Text")
    
    ' Switch off the updates of screen
    Application.ScreenUpdating = False
    
    ' Set the text
    strTextToInsert = "Annual bonus rates for the last five years"
    strTextToFind = "Discharge Pack"
    
    DataObj.SetText strTextToInsert
    DataObj.PutInClipboard
    
    Set docToOpen = Application.FileDialog(msoFileDialogFilePicker)
    docToOpen.Show
    For i = 1 To docToOpen.SelectedItems.Count
        'Open each document
        Set Doc = Documents.Open(FileName:=docToOpen.SelectedItems(i))
            Selection.Find.ClearFormatting
    Application.Echo False, "Editing " & Doc.Name
        With Selection.Find
            .Text = strTextToFind
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeParagraph
        Selection.PasteAndFormat (wdListCombineWithExistingList)
        ' The above inserts a line which creates a second page, so delete a line after
        Selection.EndKey Unit:=wdStory
        Selection.Delete Unit:=wdCharacter, Count:=1

        ActiveDocument.Close SaveChanges:=wdSaveChanges
    Next
    
    Set docToOpen = Nothing: Set Doc = Nothing
    ' Switch Screen updates back on
    Application.Echo True
    Application.ScreenUpdating = True
    Exit Sub
Err_Exit:
    MsgBox Err.Description & Err.Number
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
Yes, it's a puzzler :)

Thanks for the suggestion though.?

Perhaps it is something to do with opening the documents.?
I can live with it, just thought it would be nice not to see it flicker like it was going out of fashion. :D
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 09:29
Joined
Apr 27, 2015
Messages
5,125
Going over your code one thing did come to mind...please keep in mind that what I DONT know about VBA could fill a book...but sometimes I get lucky.

It appears that you are setting screenupdate to false BEFORE you make a selection with the filedialog and open the files.

Have you tried setting it to false AFTER the document has been opened, maybe just before the With.selection bit?

If that doesn't work then I really am out of Schlitz...
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838
Great minds think alike:D

Yes, I did think of that and put it in the loop, with the same results.

Not to worry, this particular piece of code is only run once in a while and I'll experiment further when I have any spare moments.
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 09:29
Joined
Apr 27, 2015
Messages
5,125
Cool beans, let me know how it goes. If I ever make to Wales, I expect a pint of Strongbow for me troubles!
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
10,838

NauticalGent

CopyPaster of the First Order
Local time
Today, 09:29
Joined
Apr 27, 2015
Messages
5,125
Great feedback, thanks for letting us hear the other shoe fall!
 

Users who are viewing this thread

Top Bottom