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!

Pause a Macro to Enable Document Edit While Running

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

I have a macro which will open every file in a folder and will call another macro to perform the same action to each file:

Code:
Option Explicit
 
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

Sub OpenAllFilesInFolder()
     
     'strStartPath is a path to start the macro looking at

    Dim strStartPath As String
    'ask the user where to look
    strStartPath = InputBox("Folder")
     
     '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
     
     'turn updating back on
    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(strPath As String)
     ' runs through a folder oPath, opening each file in that folder,
     ' calling a macro, and then closing each file in that folder
    Dim StrName As String
    Dim WdDoc As Document
    'closer as in the thing which closes, not the thing which is nearer
    Dim closer As Integer
    closer = 0
    '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(strPath)

    For Each scrFile In scrFolder.Files
            StrName = scrFile.Name 'the name of this file
            Application.StatusBar = strPath & "\" & StrName 'the status bar is just to let us know where we are
            'open the filename 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 = Documents.Open(FileName:=strPath & "\" & StrName, ReadOnly:=False, Format:=wdOpenFormatAuto)
                'Call the macro that performs work on the file passing a reference to it
                'choose from the options
                         
                'Replacement WdDoc
                'datingagent WdDoc
                'Reformat WdDoc
                'FormsFromText WdDoc
                'reallignment WdDoc
                'FindDateFields WdDoc
                'CorrectDefaultText WdDoc
                'ReplaceDefault WdDoc
                'Unprotect_Readonly_documents WdDoc
                'CorrectSignOff WdDoc
                'emailblue WdDoc
                'reformat WdDoc
                'Margins WdDoc
                'RemoveSpacing WdDoc
                AddFooter WdDoc
            
                'print the document if you want to
            'If MsgBox("You wanna print that?", vbYesNo + vbDefaultButton2, StrName) = vbYes Then
            '            WdDoc.PrintOut
            'Else
            '            'nothing
            'End If
                'close saving changes
                WdDoc.Close wdSaveChanges
        End If
    Next
    'return control of status bar to Word
    Application.StatusBar = False
End Sub

that's all fine, but I would like to be able to open each file in the folder, and if a certain criterion is met, pause the macro to allow me to manually edit the document and only resume when I close that document. So when I close the document, the next document to match the criterion will be opened. in pseudocode it might look something like:

For Each Document
if document Contains "Faithfully" then
replace "Faithfully" with "faithfully"
Else
Let the user edit the document
when the document is closed, goto nextone

end if
nextone:
next

it is the two bold lines that I cannot do. Is it possible to do this, and how? It is not a requirement that closing is the action which resumes the macro, if that is a difficulty. Any help much appreciated.
Mark
 



Hi,

That is not a proper method.

Drive you process from a combobox, where you select the document to open. You can get fancy and eliminate each document from the list as you process.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi, thanks for your answer.

please could you give a brief explanation of what to do with the combobox? Are you saying that I can make a combobox which will list all the files that meet the criterion, and I can open each file in turn by making selections from the combobox?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top