'================================================================== ' File: MULTIMAC.TXT ' Ref: Macro for WinWord 6.0/7.0 UK/US ' Desc: Runs specified macro on specified files in specified directory + subdirectories ' Author: WordFactory - Marcel Brugmans - toolmaster@wordfactory.nl ' History: ' created: June 28, 1996 ' last updated: January 19, 1998 ' Status: Freely distributable ' Comments: With ideas from article in PCW UK April 1996, p 298 '================================================================== '----------- ' Declare Global Vars. Filespec$ is target files, path$ is the ' target dir, TempFile$ is temporary file, tcnt is running total of files found '------------ Dim Shared filespec$, path$, TempFile$, tcnt Sub MAIN '--------------------------- ' Get the file specs, dir name and macro name to run '--------------------------- 'temp file for storing files to act on TempFile$ = Environ$("TEMP") + "\multimac.$$$" 'curr directory curdir$ = Files$(".") Size = CountMacros(0) - 1 'all macros in normal template Dim m$(size) 'array to hold macro names For count = 1 To size m$(count) = MacroName$(count, 0) Next count 'sort array SortArray m$() On Error Goto ErrorTrap Begin Dialog UserDialog 724, 352, "Multi File Macro Runner - WordFactory - 19 jan 1998" OKButton 519, 320, 88, 21 CancelButton 620, 319, 88, 21 ListBox 25, 119, 325, 216, m$(), .MacroN Text 24, 106, 103, 12, "Macro to run:", .Text1 Text 25, 5, 75, 12, "Directory:", .Text3 TextBox 25, 19, 520, 18, .Directory$ Text 25, 47, 83, 12, "File specs:", .Text56 TextBox 25, 62, 520, 18, .FileSpecs Text 25, 85, 529, 13, "Macro will run on selected files in this directory and all subdirectories.", .Text5 GroupBox 374, 108, 335, 84, "How to handle files after macro has run?" OptionGroup .SaveGroup OptionButton 398, 125, 148, 16, "Prompt for save", .OBSave0 OptionButton 398, 142, 180, 16, "Save before closing", .OBSave1 OptionButton 398, 159, 244, 16, "Do NOT save before closing", .OBSave2 CheckBox 399, 207, 300, 16, "Update screen while macro runs", .ScreenUpdate End Dialog Dim Macrodialog As UserDialog MacroDialog.Directory$ = CurDir$ Dialog MacroDialog GetCurValues Macrodialog MacroNaam$ = m$(MacroDialog.MacroN) Path$ = MacroDialog.Directory$ If Right$(path$, 1) <> "\" Then path$ = path$ + "\" FileSpec$ = MacroDialog.FileSpecs VSave = MacroDialog.SaveGroup If MacroDialog.ScreenUpdate = 0 Then ScreenUpdating 0'screen is not updated SearchDirs If tcnt < 1 Then x = MsgBox("Warning: No files found", " WARNING", 0) Goto EINDE EndIf Goto SkipError ERRORTRAP: Error err MsgBox("Operation cancelled by user", "MultiMacro") Goto EINDE SKIPERROR: '------------------ 'Confirm operation, no errors allowed '----------------- x = MsgBox(("Running macro: " + MacroNaam$ + Chr$(13) + "on files specified as: " + filespec$ + Chr$(13) + "in directory: " + path$ + " plus all subdirectories." + Chr$(13) + " (" + Str$(tcnt) + " files total)"), "Please confirm", 1) If x <> - 1 Then Goto EINDE 'CANCEL was pressed '--------------- 'open text file with list of files to operate upon. 'First line of this file is the dir name to read into PTH$. 'Next lines are file names. '--------------- Open TempFile$ For Input As 1 Input #1, pth$ '---------------------------------------- 'Open the files sequentially and do the job '---------------------------------------- While Not Eof(1) Input #1, fname$ fto$ = fname$ 'Chr$(34) added to allow for spaces in file ' or directory names in Win95 FileOpen .Name = Chr$(34) + fto$ + Chr$(34) '---------- 'run macro '------------- Print "Running " + MacroNaam$ + " on " + fto$ ToolsMacro .Name = MacroNaam$, .Run, .Show = 0 '---------- ' if MacroNaam$ hasn't already closed the file, then do it If CommandValid("FileClose") = - 1 Then FileClose VSave End If Wend MsgBox "MultiMac has processed all files and is finished" EINDE: '--------- 'remove temporary file(s) '---------- Close #1 ''Kill TempFile$ End Sub '+++++++++++++++++++++++++++++++++++ 'Subroutines that create file list TempFile$ '========= 'Search dir for matching files '=========== Sub SearchDirs Open TempFile$ For Output As 1 'this is the file list Print #1, path$ listfiles(path$) 'call recursive subroutine ' listdirs(path$) Close 1 'Close Ouput File End Sub '======== 'recursive subroutine to list the files in the directory '================ Sub Listfiles(p$) Dim cnt Dim f$(100) 'large enough Dim i, fname$ fname$ = Files$(p$ + filespec$) 'get first filename '------- 'read all files found into array f$() '-------- While fname$ <> "" f$(cnt) = LCase$(fname$) cnt = cnt + 1 fname$ = Files$() Wend '--------- 'write filenames to file '---------- tcnt = tcnt + cnt 'running total of found files cnt = cnt - 1 For J = 0 To cnt Print #1, f$(J) Next listdirs(p$) End Sub '============= 'Get subdirs in a dir. Count first then read each one. '============= Sub ListDirs(p$) x = CountDirectories(p$) For I = 1 To x d$ = GetDirectory$(p$, i) dpath$ = p$ + d$ + "\" listfiles(dpath$) Next End Sub