Can Someone Help me fix this macro?

J

Jeremiah

I have created a macro in word that
inserts a new section break on a new page, opens the header on the new page, unlinks the new page header from the previous, selects and deletes the image in the header, and then replaces it with one specific to the macro.

The macro works except for one problem.
Instead of correcting the header on the new page.
the macro changes the page before it (The page where the macro was started).

Here is the cod
Sub Header_EconomicDevelopment(

' Header_EconomicDevelopment Macr
' Inserts a new section with Economic Development Heade

Selection.InsertBreak Type:=wdSectionBreakNextPag
If ActiveWindow.View.SplitSpecial <> wdPaneNone The
ActiveWindow.Panes(2).Clos
End I
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.
ActivePane.View.Type = wdOutlineView The
ActiveWindow.ActivePane.View.Type = wdPrintVie
End I
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeade
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter.
LinkToPreviou
Selection.Delete Unit:=wdCharacter, Count:=
Selection.InlineShapes.AddPicture FileName:=
"I:\Report_Headers\Header_EconomicDevelopment.jpg", LinkToFile:=False,
SaveWithDocument:=Tru
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocumen
End Su

I have looked through the code and I cannot seem to find what is causing the macro to shift back to the previous page.
No stepes seem to be out of place.

Any feedback would be extremely wonderful

thanks

-Jeremiah
 
D

Doug Robbins - Word MVP

Hi Jermiah,

Try this way:

Dim i As Integer
' Get the section number of the Section in which the selection is located
i = Selection.Information(wdActiveEndSectionNumber)
Selection.InsertBreak Type:=wdSectionBreakNextPage
' Act on the Header in the next Section
With ActiveDocument.Sections(i + 1).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Delete
.Range.InlineShapes.AddPicture FileName:= _
"I:\Report_Headers\Header_EconomicDevelopment.jpg",
LinkToFile:=False, _
SaveWithDocument:=True
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Jeremiah said:
I have created a macro in word that
inserts a new section break on a new page, opens the header on the new
page, unlinks the new page header from the previous, selects and deletes the
image in the header, and then replaces it with one specific to the macro.
The macro works except for one problem.
Instead of correcting the header on the new page.
the macro changes the page before it (The page where the macro was started).

Here is the code
Sub Header_EconomicDevelopment()
'
' Header_EconomicDevelopment Macro
' Inserts a new section with Economic Development Header
'
Selection.InsertBreak Type:=wdSectionBreakNextPage
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"I:\Report_Headers\Header_EconomicDevelopment.jpg", LinkToFile:=False, _
SaveWithDocument:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


I have looked through the code and I cannot seem to find what is causing
the macro to shift back to the previous page.
 
J

Jezebel

It's nearly always better, in VBA, to avoid the Selection object and Windows
and Panes -- all these things can be achieved more reliably by direct
reference to the objects themselves.

Try it like this:

'Add a new section to the end of the document
ActiveDocument.Sections.Add Start:=wdSectionNewPage

'Refer to the header of the new section
With
ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(wdHeaderFoote
rPrimary)

'Clear link to previous
.LinkToPrevious = False

'Delete existing content
.Range.Text = ""

'Add graphic
.Range.InlineShapes.AddPicture ......

End With


Note: this assumes that your layout uses the same header for all pages in
the section. If you have a different first page header for the section
you'll need to change wdHeaderFooterPrimary.




Jeremiah said:
I have created a macro in word that
inserts a new section break on a new page, opens the header on the new
page, unlinks the new page header from the previous, selects and deletes the
image in the header, and then replaces it with one specific to the macro.
The macro works except for one problem.
Instead of correcting the header on the new page.
the macro changes the page before it (The page where the macro was started).

Here is the code
Sub Header_EconomicDevelopment()
'
' Header_EconomicDevelopment Macro
' Inserts a new section with Economic Development Header
'
Selection.InsertBreak Type:=wdSectionBreakNextPage
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"I:\Report_Headers\Header_EconomicDevelopment.jpg", LinkToFile:=False, _
SaveWithDocument:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


I have looked through the code and I cannot seem to find what is causing
the macro to shift back to the previous page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Watermark macro 3
Macro error 5941 1
Macro to fix header 0
Bookmark problem 0
Macro assitance for Letterhead 2
unlink Footer/Header in VBA 11
Word 2007 macro problem-different first page header 2
create macro? 4

Top