Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide Word or Prevent Flashes After a Userform and While Opening new fi

Status
Not open for further replies.
Jan 12, 2011
18
GB
Hi people,

I have a Word 2010 macro which allows a user to choose a target folder (via a userform) and which will then apply the same changes to every document in that folder. In order for any user to run the macro, I have pasted it from my personal macro and put it in a document. When the user clicks a button the userform appears and off they go.

Unfortunately because the macro is now in the file with the button, this file has to remain open while the macro runs. Even with Screen Updating turned off, every time the macro opens a new document, the screen displays it for a moment and every time a document is closed the original button file is shown in a flash.

When I run the macro from my personal macro folder this does not happen as I do not need to have any files open - I just see a blank screen and the status bar updates indicating progress.

I therefore would like to know how to stop this flashing - either by minimising the original document even while the macro runs or by some other means. I have tried playing around with ShowModal in the userform "ActiveDocument.Windows(1).WindowState = wdWindowStateMinimize" as the first action the macro performs but so far to no avail.

Any suggestions very gratefully received!
 
Check to make sure that you are only turning on and off your screen updating one time. In some code the screen updating is turned on and off when other macro's are called and it creates the flash.

Assuming you call several items simply set your master macro to turn off the screen updating and then turn it back on after making the call and don't have any of the calls turn it on and off.

Of course using an error handler to turn on the screen updating if something fails is prudent.
 
Thanks for the answer; I have checked my code and unfortunately (in terms of finding a solution) I have already put the screen updating toggles in the right places - at the beginning and the end of the procedure which calls the others.

Is there any circumstance where Word will turn screen updating back on on its own?
 
You can work with hidden second word application:
Code:
Dim wdApp As Word.Application, wdDoc As Word.Document
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open(FileName:=.....)
' process the file
wdDoc.Close
' terminate application
wdApp.Quit
Set wdApp = Nothing


combo
 
That sounds like a great idea, but I'm struggling to get it to work... I get an error on the line "Set wdDoc = wdApp.Documents.Open(FileName:=strStartPath & "\" & strName, ReadOnly:=False, Format:=wdOpenFormatAuto)" saying "Command Failed". Here is some of the code:


Most of the variables are taken from userforms

Code:
Option Explicit
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Dim scrFso As Object 'a FileSystemObject
Dim scrFolder As Object 'the folder object
Dim scrSubFolders As Object 'the subfolders collection
Dim scrFile As Object 'the file object
Dim scrFiles As Object 'the files object
Dim strDefault As String
Dim strDocName As String
Dim strTargetPath As String
Dim strPrefix As String
Dim strFolder As String
Dim strStartPath As String
Dim strFooterText As String
Dim boolPrefixChange As Boolean
Dim boolSaveAsNew As Boolean
Dim i As Integer
Dim strMacro As String
Dim boolPrint As Boolean
Dim strName As String
Dim wdDoc As Word.Document
Dim iFileNo As Integer
Dim oFrmFlds As FormFields
Dim pIndex As Long
Dim oVar As Variant
Dim newnm As String
Dim oldnm As String
Dim strSignOff As String
Dim strNewPrefix As String
Dim wdApp As Word.Application


Sub EditLetters()
ShowVisualBasicEditor = False
With Choices
    .SaveAs.Value = True
End With
Choices.Show
End Sub


Sub OpenAllFilesInFolder(strAction As String, boolSaveAs As Boolean, boolChangeNm As Boolean, strStartLocation As String, strTargetLocation As String, boolPrintOut As Boolean, Optional strText As String)
     
    'where to look
    strStartPath = strStartLocation
    'and where to put the new files
    strTargetPath = strTargetLocation
    boolPrefixChange = boolChangeNm
    boolSaveAsNew = boolSaveAs
    strMacro = strAction
    boolPrint = boolPrintOut
    strFooterText = strText
    Unload Choices
    Unload FooterText
Set wdApp = New Word.Application
If strMacro = "FormsFromText" Then
        iFileNo = FreeFile()
        'Use the name, path and correct extension to make an output file
        Open strTargetPath & "Anomaly Report" & ".txt" For Output As iFileNo
Else
        'nothing
End If

If boolPrefixChange = True Then strPrefix = InputBox("Please enter the last two letters of the letter prefix for the new filenames", "Naming Conventions", "EE")
    'stop the screen flickering
    Application.ScreenUpdating = False
     
     'open the files in the start folder and do what you will with them (see below)
    DoToAllFiles strStartPath
     'search the subfolders for more files
    SearchSubFolders strStartPath
     
    'Close the anomaly report file
    Close #iFileNo
    'open explorer to show the new files
If strTargetPath = "" Then
        ShellExecute 0, "open", strStartPath, 0, 0, 1
Else
        ShellExecute 0, "open", strTargetPath, 0, 0, 1
End If
     'turn updating back on
    Set wdApp = Nothing
    Application.ScreenUpdating = True
    
End Sub

Sub SearchSubFolders(strStartPath As String)
     
     'starts at path strStartPath and looks at its subfolders and files
     'if there are files below it calls DoToAllFiles, which opens them one by one
     'once its checked for files, it calls itself to check for subfolders.
     
    If scrFso Is Nothing Then Set scrFso = CreateObject("scripting.filesystemobject")
    'start looking in the place we said to look
    Set scrFolder = scrFso.getfolder(strStartPath)
    'tell it to be aware of sub folders
    Set scrSubFolders = scrFolder.subfolders
    For Each scrFolder In scrSubFolders
        'tell it to be aware of any files in the sub folder
        Set scrFiles = scrFolder.Files
        'if there are files below, call openFiles to open them
        If scrFiles.Count > 0 Then DoToAllFiles scrFolder.Path
        'call ourselves in a moebus whatsit to see if there are subfolders below
        SearchSubFolders scrFolder.Path
    Next
     
End Sub

Sub DoToAllFiles(strStartPath As String)
     ' runs through a folder oPath, opening each file in that folder,
     ' calling a macro, and then closing each file in that folder
    'if we are repeating this it may already be set, otherwise, set it
    If scrFso Is Nothing Then Set scrFso = CreateObject("scripting.filesystemobject")
    'set up the supfolders to be searched
Set scrFolder = scrFso.getfolder(strStartPath)

    For Each scrFile In scrFolder.Files
            strName = scrFile.Name 'the name of this file
            Application.StatusBar = strStartPath & "\" & strName 'the status bar is just to let us know where we are
            'open the file Name only if it is a word document, because that is all GoTrex understands
        If Right(strName, 4) = ".doc" Then
                'make sure that the file is editable
                Set wdDoc = wdApp.Documents.Open(FileName:=strStartPath & "\" & strName, ReadOnly:=False, Format:=wdOpenFormatAuto)
                'Call the macro that performs work on the file pasing a reference to it
        If IsEmpty(strFooterText) = True Then strFooterText = ""

                    Application.Run strMacro, wdDoc

                
                'print the document if you want to
            If boolPrint = True Then
                        wdDoc.PrintOut
            Else
                        'nothing
            End If
            
            'If the user has specified to save in a new location
            If boolSaveAsNew = True Then
                If boolPrefixChange = True Then
                        'save as with a new name
                        strNewPrefix = Left(strName, 2) & strPrefix
                        strDocName = Replace(strName, Left(strName, 4), strPrefix, 5)
                        wdDoc.SaveAs2 FileName:=strTargetPath & strNewPrefix & strDocName, Fileformat:=wdFormatDocument97
                        wdDoc.Close
                Else
                        'otherwise save in the new location with the same name
                        wdDoc.SaveAs2 FileName:=strTargetPath & strName, Fileformat:=wdFormatDocument97
                        wdDoc.Close
                End If
            Else
                    'close saving changes
                    wdDoc.Close wdSaveChanges
            End If
        Else
                'if the file is not a .doc, ignore it
        End If
    Next
    'return control of status bar to Word
    Application.StatusBar = False
End Sub


sorry to include so much code - I figured it would be for the best. Any idea why the WdApp.Documents.Open command would fail?
 
You could try to make the instance visible:
[tt]Set wdApp = New Word.Application
wdApp.Visible = True[/tt]

Try to leave only one paremetr in 'Open' method:
[tt]Set wdDoc = wdApp.Documents.Open(FileName:=strStartPath & "\" & strName)[/tt]

Is there no word 2007 file format processed (.docx etc.)?


combo
 
It is still kicking up the error about the command failing.. I have made WdApp visible (which is nicer anyway so thanks for that :)) but even with one parameter, the open fails. I removed the "& "\" " but that did not sort it either...

The documents are to be used in a program which can only deal with .doc, so even though I am writing in 2010, I only need to look at 2003 compatible files.
 
I would try to test opening opening a file with word 2010 automation:
- test the string: strStartPath & "\" & strName,
- test if wdApp is not Nothing (just before wdApp.Documents.Open),
- try to open hard-coded known file with and without creation of variable:
[tt]Set wdDoc = wdApp.Documents.Open(FileName:="Drive:\Path\Filename.doc")[/tt]
and
[tt]wdApp.Documents.Open FileName:="Drive:\Path\Filename.doc"[/tt]
- check syntax in object browser (I use still old 2003 version),
- record macro and adapt it.


combo
 
Sorry about de.lay answering here - I have been pulled in a different direction for a bit here but will check your suggestions and test as soon as I can! Thanks for the help so far - I let you know how it goes...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top