If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Including Subfolders for use within Word 2003 Macro
Can someone suggest what should be modified to the macro code below,
which will permit including "sub folders" when running the following macro within Word 2003? Thanks. By the way, the use of this macro is for a non profit non-discriminatory youth program and your assistance will make a difference. Thank you for your consideration. The macro does work perfectly to add to the footer the filename and path, print date, and page x of y and then prints the document. What we spent the last 2 weeks trying to do is for the macro to run on subfolders since we have many documents and many folders. We note when running the macro as is that we have had to manually go into every folder individually and then run the macro. Permitting subfolders to run in addition will save a considerable amount of time and ensure we have left no folder/ or sub folder out of our print task. Thank you. ' onlyprint Macro ' Macro created 09/14/2004 by youth org ' Public Sub onlyprint() Dim myFile As String Dim PathToUse As String Dim myDoc As Document With Dialogs(wdDialogCopyFile) If .Display 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With 'Close any documents that may be open If Documents.Count 0 Then Documents.Close Savechanges:=wdPromptToSaveChanges End If If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If myFile = Dir$(PathToUse & "*.doc") While myFile "" Set myDoc = Documents.Open(PathToUse & myFile) If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader If Selection.HeaderFooter.IsHeader = True Then ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Else ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader End If Selection.Font.Size = 7 NormalTemplate.AutoTextEntries("Filename and path").Insert Whe= _ Selection.Range, RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Print Date").Insert Whe=Selection.Range _ , RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Page X of Y").Insert Whe=Selection. _ Range, RichText:=True 'print and close the document myDoc.PrintOut myDoc.Close Savechanges:=wdDoNotSaveChangesClose myFile = Dir$() Wend End Sub |
#2
|
|||
|
|||
The following page should get you going with the basic idea:
Find & ReplaceAll on a batch of documents in the same folder http://www.mvps.org/word/FAQs/MacrosVBA/BatchFR.htm Hope this helps. Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word "helpserv" wrote in message om... Can someone suggest what should be modified to the macro code below, which will permit including "sub folders" when running the following macro within Word 2003? Thanks. By the way, the use of this macro is for a non profit non-discriminatory youth program and your assistance will make a difference. Thank you for your consideration. The macro does work perfectly to add to the footer the filename and path, print date, and page x of y and then prints the document. What we spent the last 2 weeks trying to do is for the macro to run on subfolders since we have many documents and many folders. We note when running the macro as is that we have had to manually go into every folder individually and then run the macro. Permitting subfolders to run in addition will save a considerable amount of time and ensure we have left no folder/ or sub folder out of our print task. Thank you. ' onlyprint Macro ' Macro created 09/14/2004 by youth org ' Public Sub onlyprint() Dim myFile As String Dim PathToUse As String Dim myDoc As Document With Dialogs(wdDialogCopyFile) If .Display 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With 'Close any documents that may be open If Documents.Count 0 Then Documents.Close Savechanges:=wdPromptToSaveChanges End If If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If myFile = Dir$(PathToUse & "*.doc") While myFile "" Set myDoc = Documents.Open(PathToUse & myFile) If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader If Selection.HeaderFooter.IsHeader = True Then ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Else ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader End If Selection.Font.Size = 7 NormalTemplate.AutoTextEntries("Filename and path").Insert Whe= _ Selection.Range, RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Print Date").Insert Whe=Selection.Range _ , RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Page X of Y").Insert Whe=Selection. _ Range, RichText:=True 'print and close the document myDoc.PrintOut myDoc.Close Savechanges:=wdDoNotSaveChangesClose myFile = Dir$() Wend End Sub |
#3
|
|||
|
|||
Hi helpserv,
The Dir$() function can't do subdirectories (as explained in the Remarks section of the VBA help topic about the function). To work your way down through all subdirectories, you need a completely different tool, the FileSystemObject. First, in the VBA editor, go to Tools References and put a check mark next to Microsoft Scripting Runtime. Without that, you'd get compiler errors from the following code. The subroutine PrintSubfolders does something called 'recursion' -- that is, it calls itself over and over, each time going one level deeper in the folder tree until there are no more subfolders. At each folder along the way, it does the modify-and-print on each .doc file in that folder. Another point worth noting is that this version uses a Range object to add the fields to the footer instead of moving the cursor around with the Selection. That makes the macro faster, since Word doesn't have to spend time redrawing the screen. Public Sub OnlyPrint() Dim fs As FileSystemObject, oFolder As Folder Dim PathToUse As String With Dialogs(wdDialogCopyFile) If .Display 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With Set fs = CreateObject("Scripting.FileSystemObject") Set oFolder = fs.GetFolder(PathToUse) PrintSubfolders oFolder End Sub Private Sub PrintSubfolders(oRoot As Folder) Dim oSubFolders As Folders, oChild As Folder Dim oFileCollection As Files, oFile As File Dim myDoc As Document Dim myRange As Range ' handle files in this folder Set oFileCollection = oRoot.Files For Each oFile In oFileCollection If InStr(oFile.name, ".doc") Then Set myDoc = Documents.Open( _ oRoot.Path & "\" & oFile.name) With myDoc Set myRange = .Sections(1) _ .Footers(wdHeaderFooterPrimary).Range NormalTemplate.AutoTextEntries("Filename and path") _ .Insert Whe=myRange, RichText:=True Set myRange = .Sections(1) _ .Footers(wdHeaderFooterPrimary).Range myRange.InsertAfter vbTab Set myRange = .Sections(1) _ .Footers(wdHeaderFooterPrimary).Range myRange.Collapse wdCollapseEnd NormalTemplate.AutoTextEntries("Print Date") _ .Insert Whe=myRange, RichText:=True Set myRange = .Sections(1) _ .Footers(wdHeaderFooterPrimary).Range myRange.InsertAfter vbTab Set myRange = .Sections(1) _ .Footers(wdHeaderFooterPrimary).Range myRange.Collapse wdCollapseEnd NormalTemplate.AutoTextEntries("Page X of Y") _ .Insert Whe=myRange, RichText:=True .PrintOut Background:=False .Close Savechanges:=wdDoNotSaveChanges End With End If Next oFile ' get collection of this folder's child subfolders Set oSubFolders = oRoot.SubFolders For Each oChild In oSubFolders ' recursive call PrintSubfolders oChild Next oChild End Sub -- Regards, Jay Freedman http://aspnet2.com/mvp.ashx?JayFreedman Microsoft Word MVP FAQ: http://word.mvps.org (helpserv) wrote: Can someone suggest what should be modified to the macro code below, which will permit including "sub folders" when running the following macro within Word 2003? Thanks. By the way, the use of this macro is for a non profit non-discriminatory youth program and your assistance will make a difference. Thank you for your consideration. The macro does work perfectly to add to the footer the filename and path, print date, and page x of y and then prints the document. What we spent the last 2 weeks trying to do is for the macro to run on subfolders since we have many documents and many folders. We note when running the macro as is that we have had to manually go into every folder individually and then run the macro. Permitting subfolders to run in addition will save a considerable amount of time and ensure we have left no folder/ or sub folder out of our print task. Thank you. ' onlyprint Macro ' Macro created 09/14/2004 by youth org ' Public Sub onlyprint() Dim myFile As String Dim PathToUse As String Dim myDoc As Document With Dialogs(wdDialogCopyFile) If .Display 0 Then PathToUse = .Directory Else MsgBox "Cancelled by User" Exit Sub End If End With 'Close any documents that may be open If Documents.Count 0 Then Documents.Close Savechanges:=wdPromptToSaveChanges End If If Left(PathToUse, 1) = Chr(34) Then PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2) End If myFile = Dir$(PathToUse & "*.doc") While myFile "" Set myDoc = Documents.Open(PathToUse & myFile) If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader If Selection.HeaderFooter.IsHeader = True Then ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Else ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader End If Selection.Font.Size = 7 NormalTemplate.AutoTextEntries("Filename and path").Insert Whe= _ Selection.Range, RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Print Date").Insert Whe=Selection.Range _ , RichText:=True Selection.TypeText Text:=" " NormalTemplate.AutoTextEntries("Page X of Y").Insert Whe=Selection. _ Range, RichText:=True 'print and close the document myDoc.PrintOut myDoc.Close Savechanges:=wdDoNotSaveChangesClose myFile = Dir$() Wend End Sub |
#4
|
|||
|
|||
We appreciate your response to our request for assistance.
However, we do not see from your suggestion the answer of how to include subfolders for use within footers. Incidentially, the original code we are using does work perfectly for our task-- it just does not deal with subfolders! Can you please advise or can someone else? Thank you. |
#5
|
|||
|
|||
Hi Jay, and all the others out there willing to help others:
Installed the code as provided, after making the required selection of the Microsoft Scripting Runtime-- however the code does not appear to work (most likely something we are unaware of how to do). We note the following References are selected: Visual Basic for Applications, Microsoft Word 11.0 Object Library and Microsoft Scripting Runtime are the only 3 References selected. Are they the only ones necessary for this code to work properly? Are they the correct references? We are using Word 2003, as part of Office 2003 on a Windows XP Professional operating system. There is an issue with the process of the path and how the file folders are selected. Are we supposed to enter manually into the code, the path? Or are we supposed to be seeing a window of some kind where we make the actual selection. We pasted the code as offered into notepad and then copied it into the Macro editor. Are there any modifications of any kind supposed to be entered into the code by us? Or are we supposed to be able to use the code as written exactly? By the way, there appears in the code suggested, places where there are o's in what otherwise wouldn't spell a word. For example see the second line to follow where the word folder is preceded by the o: Public Sub OnlyPrint() Dim fs As FileSystemObject, oFolder As Folder Dim PathToUse As String Is the o that appears periodically throughout the code supposed to be there? Thank you for your consideration. Please be aware this macro is for use by a non profit youth program that does not discriminate in its membership and no one is turned away if they can not afford membership. Your assistance is appreciated and profoundly makes a difference. |
#7
|
|||
|
|||
In response to your query regarding the exact nature and error:
The following was done already- selected the reference as requested, and then copied directly from here the suggested code, pasted it into notepad, pasted it into Microsoft Visual Basic Editor with NO MODIFICATIONS TO THE CODE. Then selected from the Tools bar, Macro, in WORD 2003 and OFFICE 2003 using XP professional as the operating system. Then selected MACRO it opened a window where I then selected the name of the macro and then RUN. The Macro then opened a window entitled COPY, inside it were folders to look in and then I selected a folder with documents I wanted to use the macro with. I then left clicked ONE TIME thereby highlighting the folder and then left clicked OPEN on the bottom right. The macro then returns an error code Run-time error '76': Path not found Selecting debug brings up Microsoft Visual Basic Editor and in YELLOW highlight is the line as follows: Set oFolder = fs.GetFolder(PathToUse) The above line of code appears within the Public Sub section and is the 15th actual line of code, including the 2 empty blank lines where no code appears, In the VBA editor, there are NO lines in RED. Only green lines which are preceded by a ' and black lines of code. I note there are a few empty lines where no code appears. In an effort to eliminate a potential issue in advance, I have carefully deleted the few empty lines and that resulted in no difference, the same error appears. In addition, I note there is a solid line which crosses the entire Microsoft Visual Basic Editor editing portion of the screen and is immediately after the line of code End Sub which appears within the Public Sub Section, if that means anything. Any help or suggestions will be appreciated, this project is important and time sensitive and when done must be accurate. The original difficulty was the inability of the macro to also do the sub folders. Consequently, relying on long, hand written lists of folders and then volunteers making sure they wrote the folder list properly and then that someone ran the original macro on each and every folder. It's like inventory checking in a huge warehouse but worse. We noted entire folders were not included, and what's worse is when we needed to run the same thing several times since we are in editing mode of all these documents, its been contributing to a poor use of limited resources and volunteers. Thank you one and all for your help, and for your being thoughtful to others. Jay Freedman wrote in message . .. Answers, in order of your questions, with more questions: - When you say the code doesn't appear to work, exactly what are the symptoms? Do you get an error message, or do you get wrong footers or no footers in the documents, or does it seem that the macro doesn't run at all? Exactly how are you trying to run the macro? - Yes, the three references you list should be sufficient. The macro runs on my system -- which is also Word 2003 on Windows XP Pro -- with only those references selected. - When the macro runs, it should open the Copy dialog; you should be able to navigate to the topmost folder of the group you want to work on and click OK, and that path is returned to the macro. This part of the macro is exactly the same as the one you posted, which you said was working except for not doing subfolders. - If you get an error message about a syntax error, I suspect you might be having trouble with extra line breaks introduced into the code by the newsgroup posting mechanism. In the VBA editor, are there any lines in red? If so, which ones? - Yes, the o in oFolder is deliberate. It's a bad idea to name variables the same as data types or other keywords of the VBA language, which can lead to confusion. The o (which stands for object) distinguishes the variable oFolder from the data type Folder. This is based on a system called "Hungarian notation" (invented by Charles Simonyi), which you can read about at http://msdn.microsoft.com/library/te...hunganotat.htm. -- Regards, Jay Freedman http://aspnet2.com/mvp.ashx?JayFreedman Microsoft Word MVP FAQ: http://word.mvps.org (helpserv) wrote: Hi Jay, and all the others out there willing to help others: Installed the code as provided, after making the required selection of the Microsoft Scripting Runtime-- however the code does not appear to work (most likely something we are unaware of how to do). We note the following References are selected: Visual Basic for Applications, Microsoft Word 11.0 Object Library and Microsoft Scripting Runtime are the only 3 References selected. Are they the only ones necessary for this code to work properly? Are they the correct references? We are using Word 2003, as part of Office 2003 on a Windows XP Professional operating system. There is an issue with the process of the path and how the file folders are selected. Are we supposed to enter manually into the code, the path? Or are we supposed to be seeing a window of some kind where we make the actual selection. We pasted the code as offered into notepad and then copied it into the Macro editor. Are there any modifications of any kind supposed to be entered into the code by us? Or are we supposed to be able to use the code as written exactly? By the way, there appears in the code suggested, places where there are o's in what otherwise wouldn't spell a word. For example see the second line to follow where the word folder is preceded by the o: Public Sub OnlyPrint() Dim fs As FileSystemObject, oFolder As Folder Dim PathToUse As String Is the o that appears periodically throughout the code supposed to be there? Thank you for your consideration. Please be aware this macro is for use by a non profit youth program that does not discriminate in its membership and no one is turned away if they can not afford membership. Your assistance is appreciated and profoundly makes a difference. |
#8
|
|||
|
|||
OK, we're starting to get somewhere. I'm going to have to rely on you
to continue debugging. As I said before, the macro runs properly on my system, but I obviously don't have the same set of folders and files you do. First, here are some things that are *not* wrong, based on your description: - The references are correct. The macro starts, and the FileSystemObject (the object named fs) is being created. - The dialog is being presented to pick the topmost folder. - There are no "syntax errors" (that is, instructions that have typing errors or incorrect structure), which would be highlighted in red. - The presence or absence of blank lines in the code is immaterial. - The solid line between subroutines (that is, before a Sub line or after an End Sub line) is normal and expected. What appears to be happening is that the folder name supplied by the dialog is incorrect in some way. A 'path not found' error can mean literally that the path named by the variable PathToUse doesn't exist, but that's very improbable because you aren't typing the name, so there's no opportunity for a typing error. It might mean that the variable's value contains a doubled backslash or an invalid character. It could mean that there's a problem with the permissions granted to you in that folder, or that there's something wrong with the network (if the folder is on a server or another machine). Run the macro the same way again. When the error message pops up and you click the Debug button, hover the mouse over the word PathToUse. You should see a tooltip that shows the value of that variable. If you have trouble getting that, then right-click the word and choose Add Watch, click OK in the resulting dialog, and look at the Watch window that opens at the bottom of the editor. Examine the value of PathToUse to see if you can figure out what's wrong with it. Try running the macro again and choosing a different folder in the dialog, to see if it's something about the first one's name or permissions or location. Post back and show me the values you found. -- Regards, Jay Freedman http://aspnet2.com/mvp.ashx?JayFreedman Microsoft Word MVP FAQ: http://word.mvps.org (helpserv) wrote: In response to your query regarding the exact nature and error: The following was done already- selected the reference as requested, and then copied directly from here the suggested code, pasted it into notepad, pasted it into Microsoft Visual Basic Editor with NO MODIFICATIONS TO THE CODE. Then selected from the Tools bar, Macro, in WORD 2003 and OFFICE 2003 using XP professional as the operating system. Then selected MACRO it opened a window where I then selected the name of the macro and then RUN. The Macro then opened a window entitled COPY, inside it were folders to look in and then I selected a folder with documents I wanted to use the macro with. I then left clicked ONE TIME thereby highlighting the folder and then left clicked OPEN on the bottom right. The macro then returns an error code Run-time error '76': Path not found Selecting debug brings up Microsoft Visual Basic Editor and in YELLOW highlight is the line as follows: Set oFolder = fs.GetFolder(PathToUse) The above line of code appears within the Public Sub section and is the 15th actual line of code, including the 2 empty blank lines where no code appears, In the VBA editor, there are NO lines in RED. Only green lines which are preceded by a ' and black lines of code. I note there are a few empty lines where no code appears. In an effort to eliminate a potential issue in advance, I have carefully deleted the few empty lines and that resulted in no difference, the same error appears. In addition, I note there is a solid line which crosses the entire Microsoft Visual Basic Editor editing portion of the screen and is immediately after the line of code End Sub which appears within the Public Sub Section, if that means anything. Any help or suggestions will be appreciated, this project is important and time sensitive and when done must be accurate. The original difficulty was the inability of the macro to also do the sub folders. Consequently, relying on long, hand written lists of folders and then volunteers making sure they wrote the folder list properly and then that someone ran the original macro on each and every folder. It's like inventory checking in a huge warehouse but worse. We noted entire folders were not included, and what's worse is when we needed to run the same thing several times since we are in editing mode of all these documents, its been contributing to a poor use of limited resources and volunteers. Thank you one and all for your help, and for your being thoughtful to others. |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
PDF Maker Icon on Word Toolbar | TimSpeak | General Discussion | 14 | October 6th, 2004 05:31 PM |
Word 2000/2002 - Proper Mail Merge steps for ODBC? | Tony_VBACoder | Mailmerge | 7 | September 2nd, 2004 09:21 PM |
Word 2003 "error message" | Gary W. Plourde Sr. | New Users | 2 | August 29th, 2004 03:17 PM |
Word Perfect Macros to Word 2K or 2003 | RichNRockvilleMD | General Discussion | 2 | July 22nd, 2004 05:40 PM |
How to run word and pass a mail merge values and fax it to the recipient | Belinda | Mailmerge | 2 | June 13th, 2004 12:49 AM |