Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2016, 09:17 AM
jojo77 jojo77 is offline Touching Header range with VBA forces a page break Windows 10 Touching Header range with VBA forces a page break Office 2013
Novice
Touching Header range with VBA forces a page break
 
Join Date: Nov 2016
Posts: 1
jojo77 is on a distinguished road
Default Touching Header range with VBA forces a page break

I have a script to modify a field in a document header:


Code:
WordDoc.ActiveWindow.View.Type = wdPrintView
                WordDoc.ActiveWindow.ActivePane.View.SeekView = 9
		WordDoc.Sections(2).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
                WordDoc.ActiveWindow.ActivePane.View.SeekView = 0

                Dim fg As Field
                For Each fg In WordDoc.Sections(1).Headers(1).Range.Fields
                  If (InStr(1, fg.Code.text, "{ORDERNR}") > 0) Then
                     fg.Code.text = Replace(fg.Code.text, "{ORDERNR}", "{FONR}")
   		  End If
                Next fg
When I run this it causes a 4 page document to push down the last line to the next page (and since a hard page break has been put, the second page is then empty)

Now for the funny part.
After the VBA has run, double clicking on the header and then double clicking back on the document body, this resets the pages to their original state.

I have removed line by line and it turns out that to provoke the error it is enough to even have a reference to the Headers(1).Range, without even modifying the content.

When I have had similar issues I have forced a refresh by swithcing the view to the header and then back (similar to what I did with the double clicking), but for this problem it doesn't help.

Any ideas how to approach this problem to find a solution?

This is a Windows Server 2008 R2 installation accessed over Citrix
Office 15.0.4859.1001
Reply With Quote
  #2  
Old 11-22-2016, 03:10 PM
macropod's Avatar
macropod macropod is offline Touching Header range with VBA forces a page break Windows 7 64bit Touching Header range with VBA forces a page break Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There is no need to set the View.Type or use SeekView for what you're doing. Try:
Code:
Dim fg As Field
With WordDoc
  .Sections(2).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
  With .Sections(1).Headers(wdHeaderFooterPrimary).Range
  For Each fg In .Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields
    If (InStr(1, fg.Code.text, "{ORDERNR}") > 0) Then
      fg.Code.Text = Replace(fg.Code.Text, "{ORDERNR}", "{FONR}")
      fg.Update
    End If
  Next fg
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-27-2016, 03:25 PM
macropod's Avatar
macropod macropod is offline Touching Header range with VBA forces a page break Windows 7 64bit Touching Header range with VBA forces a page break Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: https://social.msdn.microsoft.com/Fo...?forum=worddev
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
vba word headers



Similar Threads
Thread Thread Starter Forum Replies Last Post
Page Break is adding Blank Page when Break occurs JoannL54 Word 3 07-01-2016 05:58 AM
Touching Header range with VBA forces a page break Section Break (Next Page) replaces Section Break (Continuous) when deleted Carlabasson Word 2 03-25-2013 10:13 PM
Touching Header range with VBA forces a page break Section Break changes formatting on next Header 1 Eastendr Word 1 01-21-2013 05:57 AM
Touching Header range with VBA forces a page break Help! Section Page Break Not Working - Need new Header and Footers dkgolfer16 Word 2 05-23-2012 11:02 AM
Touching Header range with VBA forces a page break Header after page break mashtonsmith Word 1 03-11-2011 10:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft