View Single Post
 
Old 02-16-2022, 06:59 AM
kenny84 kenny84 is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2022
Posts: 6
kenny84 is on a distinguished road
Default

Attached with the code below. But still unable to figure out how to move heading 1.0 to start at page 3 although no error when running the macro. Any expert can guide me on this? Thanks!



Sub UpdateDocumentHeaders()

Application.ScreenUpdating = False

Dim strFolder As String, strFile As String
Dim wdDocTgt As Document, wdDocSrc As Document
Dim Sctn As Section, HdFt As HeaderFooter


Dim oPara As Paragraph
Dim r As Range


strFolder = GetFolder

If strFolder = "" Then Exit Sub

Set wdDocSrc = ActiveDocument

strFile = Dir(strFolder & "\*.doc", vbNormal)

While strFile <> ""


If strFolder & "" & strFile <> wdDocSrc.FullName Then
Set wdDocTgt = Documents.Open(FileName:=strFolder & "" & strFile, _
AddToRecentFiles:=False, Visible:=False)

With wdDocTgt

For Each Sctn In .Sections
'For Headers
For Each HdFt In Sctn.Headers
With HdFt
If .Exists Then
If Sctn.Index = 1 Then
.Range.FormattedText = _
wdDocSrc.Sections.First.Headers(HdFt.Index).Range. FormattedText
.Range.Characters.Last = vbNullString
ElseIf .LinkToPrevious = False Then
.Range.FormattedText = _
wdDocSrc.Sections.First.Headers(HdFt.Index).Range. FormattedText
.Range.Characters.Last = vbNullString
End If
End If
End With
Next



'For footers
For Each HdFt In Sctn.Footers
With HdFt
If .Exists Then
If Sctn.Index = 1 Then
.Range.FormattedText = _
wdDocSrc.Sections.First.Footers(HdFt.Index).Range. FormattedText
.Range.Characters.Last = vbNullString
ElseIf .LinkToPrevious = False Then
.Range.FormattedText = _
wdDocSrc.Sections.First.Footers(HdFt.Index).Range. FormattedText
.Range.Characters.Last = vbNullString
End If
End If
End With
Next

'For Heading 1 start at page 3


For Each oPara In wdDocTgt.Paragraphs
If oPara.Style = "Heading 1" Then
Set r = oPara.Range
With r
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End If



Next

wdDocTgt.PageSetup.FooterDistance = InchesToPoints(0.6)
wdDocTgt.PageSetup.HeaderDistance = InchesToPoints(0.8)

Next



.Close SaveChanges:=True
End With


End If



strFile = Dir()
Wend
Set wdDocSrc = Nothing: Set wdDocTgt = Nothing
Application.ScreenUpdating = True
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
Reply With Quote