Insert HeaderFooter using vba

%3CLINGO-SUB%20id%3D%22lingo-sub-2246553%22%20slang%3D%22en-US%22%3EInsert%20HeaderFooter%20using%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2246553%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20been%20trying%20to%20insert%20header%20using%20the%20given%20blow%20code.%20But%20at%20one%20time%20it's%20working%20and%20other%20time%20it%20shows%20the%20error%206028%20(the%20range%20cannot%20be%20deleted).%20If%20anyone%20have%20idea%20how%20to%20correct%20this%20plz%20share.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20UpdateHeader()%3C%2FP%3E%3CP%3EDim%20oDoc%20As%20Word.Document%2C%20oSec%20As%20Word.Section%2C%20rng%20As%20Word.Range%3CBR%20%2F%3ESet%20oDoc%20%3D%20ActiveDocument%3C%2FP%3E%3CP%3EFor%20Each%20oSec%20In%20oDoc.Sections%3CBR%20%2F%3ESet%20rng%20%3D%20oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range%3CBR%20%2F%3EAddHeaderToRange%20rng%3C%2FP%3E%3CP%3ESet%20rng%20%3D%20oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range%3CBR%20%2F%3EAddHeaderToRange%20rng%3CBR%20%2F%3ENext%20oSec%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3EPrivate%20Sub%20AddHeaderToRange(rng%20As%20Word.Range)%3CBR%20%2F%3EWith%20rng%3CBR%20%2F%3E.Tables.Add%20Range%3A%3Drng%2C%20NumRows%3A%3D1%2C%20NumColumns%3A%3D2%3CBR%20%2F%3EWith%20.Tables(1)%3CBR%20%2F%3E.Borders.InsideLineStyle%20%3D%20wdLineStyleNone%3CBR%20%2F%3E.Borders.OutsideLineStyle%20%3D%20wdLineStyleNone%3CBR%20%2F%3E.Rows.SetLeftIndent%20LeftIndent%3A%3D-37%2C%20RulerStyle%3A%3DwdAdjustNone%3CBR%20%2F%3E.Columns(2).SetWidth%20ColumnWidth%3A%3D300%2C%20RulerStyle%3A%3DwdAdjustNone%3CBR%20%2F%3E.Cell(1%2C%201).Range.InlineShapes.AddPicture%20FileName%3A%3D%22Your%20Picture%20Location%22%2C%20LinkToFile%3A%3DFalse%2C%20SaveWithDocument%3A%3DTrue%3CBR%20%2F%3E.Cell(1%2C%202).Range.Font.Name%20%3D%20%22Arial%22%3CBR%20%2F%3E.Cell(1%2C%202).Range.Font.Size%20%3D%209%3CBR%20%2F%3E.Cell(1%2C%202).Range.ParagraphFormat.Alignment%20%3D%20wdAlignParagraphRight%3CBR%20%2F%3E.Cell(1%2C%202).Range.Text%20%3D%20%22Test%20header%22%20%26amp%3B%20vbNewLine%20%26amp%3B%20%22Second%20Line%22%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2246553%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHeaderFooter%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVba%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

I have been trying to insert header using the given blow code. But at one time it's working and other time it shows the error 6028 (the range cannot be deleted). If anyone have idea how to correct this plz share.

 

Sub UpdateHeader()

Dim oDoc As Word.Document, oSec As Word.Section, rng As Word.Range
Set oDoc = ActiveDocument

For Each oSec In oDoc.Sections
Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
AddHeaderToRange rng

Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
AddHeaderToRange rng
Next oSec
End Sub

Private Sub AddHeaderToRange(rng As Word.Range)
With rng
.Tables.Add Range:=rng, NumRows:=1, NumColumns:=2
With .Tables(1)
.Borders.InsideLineStyle = wdLineStyleNone
.Borders.OutsideLineStyle = wdLineStyleNone
.Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
.Columns(2).SetWidth ColumnWidth:=300, RulerStyle:=wdAdjustNone
.Cell(1, 1).Range.InlineShapes.AddPicture FileName:="Your Picture Location", LinkToFile:=False, SaveWithDocument:=True
.Cell(1, 2).Range.Font.Name = "Arial"
.Cell(1, 2).Range.Font.Size = 9
.Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Cell(1, 2).Range.Text = "Test header" & vbNewLine & "Second Line"
End With
End With
End Sub

0 Replies