none
VBA Macro - Worked in Word 2003 but has an issue in Word 2007 RRS feed

  • Question

  • Hello

    We have a macro that is generated via an inhouse toolbar in Word.  The macro inserts selected text (the letter recipient's name) into the header section of page 2 onwards.

    This has worked without issue prior to upgrading from MS Office 2003 to MS Office 2007.

    When using this macro in a Word 2007 document (.docx or .dotx) - the last part of the macro (in bold at bottom of code below) no longer works, ie so there is an unwanted page break now left at the end of each document - requiring the user to remember to delete it.

    Any idea how I can fix this?

    Here's the VBA macro code:

    ' Copy the selected text - user must select text first
    ' then go to end of document and put in a page break
        Selection.Copy
        Selection.EndKey Unit:=wdStory
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.InsertBreak Type:=wdPageBreak
       
    ' this was put in the recorded macro and determines the view
    ' our document is in and should remain as is even though we
    ' may not understand it fully - second bit ensures a valid view
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
             = wdMasterView Then
            ActiveWindow.ActivePane.View.Type = wdPageView
        End If
    ' get into the header for this page (2+) and paste the text - no
    ' formatting hence paste special.  This text is pasted at LHS - assumes
    ' other required information has been put in the header
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= _
            wdInLine, DisplayAsIcon:=False
    ' back to main document and remove additional hard page
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Selection.TypeBackspace
    End Sub

    Any assistance is greatly appreciated ... :0)

    Friday, July 1, 2011 12:34 AM

All replies

  • Why insert a pagebreak? Why not paste the text in the header and select Different first page under page setup? Record a macro to see teh code needed.

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Friday, July 1, 2011 2:54 AM
  • The reason for this is a change of behaviour in the way a Page Break is inserted in Word 2007 - with an extra paragraph mark.
     
    However, the code, based on recorded code, is far more complex than it needs to be. Assuming you have "different first page" already set, then this should do the same as your whole macro:
     
        Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertBefore Selection.Text
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, July 1, 2011 10:13 AM