![]() |
|
#1
|
||||
|
||||
![]() Quote:
Code:
Option Explicit Dim FSO As Object, oFolder As Object, StrFolds As String, wdDocSrc As Document, wdDocTgt As Document Sub Main() Application.ScreenUpdating = False Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long TopLevelFolder = GetFolder If TopLevelFolder = "" Then Exit Sub StrFolds = vbCr & TopLevelFolder If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject") End If Set wdDocSrc = ActiveDocument '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 Set wdDocSrc = Nothing: Set wdDocTgt = Nothing 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 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 Sub UpdateDocuments(oFolder As String) Dim strInFolder As String, strFile As String strInFolder = oFolder strFile = Dir(strInFolder & "\*.doc", vbNormal) While strFile <> "" Set wdDocTgt = Documents.Open(FileName:=strInFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) With wdDocTgt With .Sections.First .PageSetup.DifferentFirstPageHeaderFooter = True .Headers(wdHeaderFooterFirstPage).Range.FormattedText = _ wdDocSrc.Sections.First.Headers(wdHeaderFooterFirstPage).Range.FormattedText .Range.Characters.Last = vbNullString End With 'Save and close the document .Close SaveChanges:=True End With strFile = Dir() Wend End Sub There is nothing in the code that is capable of causing that.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#2
|
|||
|
|||
![]()
The code works perfectly until it hits runtime error 5138
Hitting debug then points me to Code:
.PageSetup.DifferentFirstPageHeaderFooter = True Thank you. Edit: I found the problem there are some weird documents with impossible margins, and I found a way to mass rename and remove the special chars from the filenames, the code is working as intended, thank you for your help! |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Updating Headers & Footers from Database | Yuvahari | Word VBA | 5 | 01-31-2022 10:12 PM |
Batch Replacing Headers using Macros | JakeG9 | Word VBA | 0 | 07-13-2021 10:54 AM |
![]() |
troyw | Word VBA | 8 | 05-05-2021 07:17 PM |
![]() |
Edszx | Word VBA | 2 | 05-27-2019 11:16 PM |
Updating headers in multiple files | cellophane | Word | 3 | 01-17-2013 06:36 AM |