![]() |
#8
|
||||
|
||||
![]()
Changing the 'ScreenUpdating' line to 'True' only slows down the macro and causes a lot of screen flicker; it doesn't mean you see each change, especially since the code is also written to not show the documents that are being processed (via Visible:=False). Even if you changed that, you'd be unlikely to see much activity, since the code never selects any of the objects being worked on.
As said in the thread you linked to, it would be nice if you said up-front what you want to do. It is only now that you've mentioned sub-folder processing. Your code should now be: Code:
Option Explicit Dim FSO As Object, oFolder As Object, StrFolds As String Sub Main() Application.ScreenUpdating = False Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long TopLevelFolder = GetFolder StrFolds = vbCr & TopLevelFolder If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject") End If 'Get the sub-folder structure Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders For Each aFolder In TheFolders RecurseWriteFolderName (aFolder) Next 'Process the documents in each folder For i = 1 To UBound(Split(StrFolds, vbCr)) Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i))) Next Application.ScreenUpdating = True End Sub Sub RecurseWriteFolderName(aFolder) Dim SubFolders As Variant, SubFolder As Variant Set SubFolders = FSO.GetFolder(aFolder).SubFolders StrFolds = StrFolds & vbCr & CStr(aFolder) On Error Resume Next For Each SubFolder In SubFolders RecurseWriteFolderName (SubFolder) Next End Sub Sub UpdateDocuments(oFolder As String) Dim strFolder As String, strFile As String, wdDoc As Document strFile = Dir(strFolder & "\*.docx", vbNormal) While strFile <> "" Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) With wdDoc Call RefreshFields(wdDoc) 'Optional - create a PDF of the updated document '.SaveAs2 FileName:=Split(.Fullname, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False .Close SaveChanges:=True End With strFile = Dir() Wend Set wdDoc = Nothing End Sub Sub RefreshFields(wdDoc As Document) Dim oStory As Range, oTOC As TableOfContents, oTOF As TableOfFigures With wdDoc For Each oStory In .StoryRanges oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange oStory.Fields.Update Wend End If Next oStory For Each oTOC In .TablesOfContents oTOC.Update Next oTOC For Each oTOF In .TablesOfFigures oTOF.Update Next oTOF End With End Sub Function GetFolder() As String Dim oFolder As Object GetFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to update calculated fields | sectionbreak | Mail Merge | 4 | 06-04-2014 12:12 AM |
Office 2010 Can't Open Or Save Documents in My Documents Folder | trippb | Office | 1 | 07-12-2013 07:29 AM |
![]() |
PeaceDove | Word | 3 | 01-17-2012 02:45 PM |
VBA to update certain (but not all) fields | sparkyrose | Word VBA | 0 | 05-20-2010 12:50 PM |
Can no longer update fields! | slindsay | Word | 0 | 09-03-2009 05:10 PM |