Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

Speed up deletion of Footers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matt Williamson - 19 Dec 2006 17:16 GMT
I wrote this routine to delete all of the footers in all of the documents
under a specific folder. It works fine, but requires the document to display
for every iteration of the loop. Is there a faster way to do this? I have to
run this against thousands of files.

I tried Setting the visible property of Document.open to false but then it's
not able to access the activedocument object to manipulate it.

Sub DeleteAllFooters()

Dim sStartFolder As String
Dim i As Long, j As Long

sStartFolder = "c:\word docs"

If Len(sStartFolder) = 0 Then
 'Ask the user to select a folder
 With Dialogs(wdDialogCopyFile)
   'User selected a directory
   If .Display Then
     sStartFolder = .Directory
   Else
     'User chose Cancel
     Exit Sub
   End If
 End With
End If

Application.Visible = False
Application.DisplayAlerts = wdAlertsNone

With Application.FileSearch
   .LookIn = sStartFolder
   .SearchSubFolders = True
   .FileType = msoFileTypeWordDocuments
   .Execute
   For i = 1 To .FoundFiles.Count
       Debug.Print .FoundFiles(i)
       Documents.Open .FoundFiles(i)
       For j = 1 To ActiveDocument.Sections.Count
         With ActiveDocument.Sections(j)
           .Footers(wdHeaderFooterFirstPage).Range.Delete
           .Footers(wdHeaderFooterPrimary).Range.Delete
         End With
       Next j
      Documents.Close wdSaveChanges
   Next i
End With

End Sub

TIA

Matt
Jonathan West - 19 Dec 2006 17:53 GMT
Hi Matt

Change this

       Documents.Open .FoundFiles(i)
       For j = 1 To ActiveDocument.Sections.Count
         With ActiveDocument.Sections(j)
           .Footers(wdHeaderFooterFirstPage).Range.Delete
           .Footers(wdHeaderFooterPrimary).Range.Delete
         End With
       Next j
      Documents.Close wdSaveChanges

to this

       Set oDoc = Documents.Open(Filename:=.FoundFiles(i), Visible:=False)
       For Each oSection in oDoc.Sections
         With oSection
           .Footers(wdHeaderFooterFirstPage).Range.Delete
           .Footers(wdHeaderFooterPrimary).Range.Delete
         End With
       Next oSection
       oDoc.Close wdSaveChanges

In addition, put the following lines at the start of the routine

Dim oDoc as Document
Dim oSection as Section

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

>I wrote this routine to delete all of the footers in all of the documents
>under a specific folder. It works fine, but requires the document to
[quoted text clipped - 50 lines]
>
> Matt
Greg Maxey - 19 Dec 2006 18:05 GMT
Jonathan,

Your suggestion is better.  Looks like I need to make a change to my
standard batch file macro ;-)

> Hi Matt
>
[quoted text clipped - 86 lines]
> >
> > Matt
Matt Williamson - 19 Dec 2006 19:22 GMT
Thanks Jonathan

That's exactly what I wanted to do but just couldn't figure out the syntax.
I didn't realize there was a sections collection either.

It doesn't seem to be any faster though, it's taking 2-3 seconds per
document. Are there any further enhancements I can make to speed up the
process. I can do it in VB6 if that will speed it up any.

TIA

Matt

> Hi Matt
>
[quoted text clipped - 79 lines]
>>
>> Matt
Jonathan West - 19 Dec 2006 19:57 GMT
> Thanks Jonathan
>
[quoted text clipped - 4 lines]
> document. Are there any further enhancements I can make to speed up the
> process. I can do it in VB6 if that will speed it up any.

No, there's not much more you can do to speed it up. The largest part of the
time is spent opening and saving the file, and there is nothing you can do
in your code to help that. Coding it in VB won't help - you'll still have to
use Word through a reference to its object model.

If your files are on a network drive I would suggest moving them to a local
drive, as you then get the benefit of using the local drive's cache. But
apart from that, I think it is going as fast as it can.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

Greg Maxey - 19 Dec 2006 18:01 GMT
Matt,

You should be able to use Documents(1) instead of ActiveDocument.  Your
code would also fail to delete any even page footers.

I plugged your action into my standard batch file macro and it seemed
to work using Documents(1)

Sub BatchProcessFiles()
Dim strBatchDir As String
Dim oFSO As Scripting.FileSystemObject
Dim oBatchFolder As Scripting.Folder
Dim oBatchFile As Scripting.File
Dim oFld As Word.Field
Dim curCursor As Long

'Call function to pick a batch directory and set up for processing
strBatchDir = PickFolder
Set oFSO = New Scripting.FileSystemObject
Set oBatchFolder = oFSO.GetFolder(strBatchDir)

'Minimize screen flicker
curCursor = System.Cursor
System.Cursor = wdCursorWait
Application.ScreenUpdating = False

'Process files in the designated batch folder
For Each oBatchFile In oBatchFolder.Files
 'Narrow processing to Word files only
 If UCase(oFSO.GetExtensionName(oBatchFile.Path)) = "DOC" _
   Or oBatchFile.Type = "Word Document" Then
   Application.Documents.Open oBatchFile.Path, Visible:=False
   With Documents(1)
     Dim j As Long
     For j = 1 To Documents(1).Sections.Count
         With Documents(1).Sections(j)
           .Footers(wdHeaderFooterFirstPage).Range.Delete
           .Footers(wdHeaderFooterPrimary).Range.Delete
           .Footers(wdHeaderFooterEvenPage).Range.Delete
         End With
       Next j
     .Close SaveChanges:=wdSaveChanges
   End With
 End If
Next oBatchFile
Set oBatchFile = Nothing
If MsgBox("Do you want to process sub-folders in the batch folder?", _
        vbQuestion + vbYesNo, "Continue With Sub-Folders?") _
        = vbYes Then
 'call the recursive function
 ProcessSubFolders oBatchFolder.SubFolders, oFSO
End If
Set oFSO = Nothing
Set oBatchFolder = Nothing
'Restore visuals
Application.ScreenUpdating = True
System.Cursor = curCursor
End Sub

Function PickFolder() As String
'Open "Copy" so user can point and click to a path to use
Dim strFolderPath As String
With Dialogs(wdDialogCopyFile)
 If .Display <> 0 Then
   strFolderPath = .Directory
 Else
   MsgBox "Cancelled by User"
   Exit Function
 End If
End With
If Left(strFolderPath, 1) = Chr(34) Then
 strFolderPath = Mid(strFolderPath, 2, Len(strFolderPath) - 2)
End If
If Right(strFolderPath, 1) = "\" Then
 strFolderPath = Mid(strFolderPath, 1, Len(strFolderPath) - 1)
End If
PickFolder = strFolderPath
End Function

I copied but didn't edit the recursive function shown below

Function ProcessSubFolders(ByRef oBatchFolders As Scripting.Folders, _
        oSubFSO As Scripting.FileSystemObject)
Dim oSubFolder As Scripting.Folder
Dim oSubFolderFile As Scripting.File
Dim oFld As Word.Field
For Each oSubFolder In oBatchFolders
 For Each oSubFolderFile In oSubFolder.Files
   If UCase(oSubFSO.GetExtensionName(oSubFolderFile.Path)) = "DOC" _
     Or oSubFolderFile.Type = "Word Document" Then
     Application.Documents.Open oSubFolderFile.Path
     With ActiveDocument
       For Each oFld In .Fields
         If UCase(Mid(oFld.Code.Text, 2, 4)) = "DATE" _
           Or UCase(Mid(oFld.Code.Text, 2, 4)) = "TIME" Then
           oFld.Code.Text = " CREATEDATE \@ ""MMMM d, yyyy"" "
         End If
       Next oFld
      .Close SaveChanges:=wdSaveChanges
      End With
   End If
 Next oSubFolderFile
 'Call function for each sub-folder
 If oSubFolder.SubFolders.Count > 0 Then
   ProcessSubFolders oSubFolder.SubFolders, oSubFSO
 End If
Next oSubFolder
Set oSubFolderFile = Nothing
Set oSubFolder = Nothing
End Function

> I wrote this routine to delete all of the footers in all of the documents
> under a specific folder. It works fine, but requires the document to display
[quoted text clipped - 50 lines]
>
> Matt
Jonathan West - 19 Dec 2006 18:06 GMT
> Matt,
>
> You should be able to use Documents(1) instead of ActiveDocument.  Your
> code would also fail to delete any even page footers.

I wouldn't want to rely on that - if there happens to be another document
open at the time, I suspect the newly opened document will be Documents(2).
Much safer to assign the document to an object variable on opening.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.