Gasman
Enthusiastic Amateur
- Local time
- Today, 18:20
- Joined
- Sep 21, 2011
- Messages
- 16,680
Hi all,
Word 2007.
I found some code on the net and amended it to do as I need.
I tested it at home and it worked fine.
I emailed myself the .docm document with the code in it.
When I run it in work on my new computer, it wipes out the header completely and also creates a second page.?
Can anyone please explain what I am missing?
I thought it might be due to references as this computer is new, but it does not complain about any code at all.
TIA
Word 2007.
I found some code on the net and amended it to do as I need.
I tested it at home and it worked fine.
I emailed myself the .docm document with the code in it.
When I run it in work on my new computer, it wipes out the header completely and also creates a second page.?
Can anyone please explain what I am missing?
I thought it might be due to references as this computer is new, but it does not complain about any code at all.
TIA
Code:
Sub EditHeader()
Dim Doc As Document
Dim i As Integer
Dim docToOpen As FileDialog
Dim strText As String
Dim blnSave As Boolean
blnSave = True
On Error GoTo Err_Exit
Set docToOpen = Application.FileDialog(msoFileDialogFilePicker)
docToOpen.Show
For i = 1 To docToOpen.SelectedItems.Count
'Open each document
Set Doc = Documents.Open(FileName:=docToOpen.SelectedItems(i))
'With Doc.Sections(1)
'remove all existing headers and footers
' Call RemoveHeadAndFoot
'End With
With Doc.Sections(1)
'insert new headers
'strText = .Headers(wdHeaderFooterPrimary).Range.Text
'now replace the text
'strText = Replace(strText, "aaaa", "bbbbb")
'strText = Replace(strText, "cccc", "ddddd")
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "aaaa", "bbbb)
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "cccc", "ddddd")
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "eeee", "fffff")
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "gggg", "hhhhh")
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "iiii", "jjjjj")
.Headers(wdHeaderFooterPrimary).Range.Text = Replace(.Headers(wdHeaderFooterPrimary).Range.Text, "kkkk", "lllll")
'.Range.Paragraphs.Alignment = wdAlignParagraphRight
End With
'With ActiveDocument.Sections(1)
'insert new footers
'Call editfooter
'End With
Doc.Save
Doc.Close
Next i
Err_Exit:
Set docToOpen = Nothing
Set Doc = Nothing
End Sub