Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-07-2017, 04:32 AM
slaycock slaycock is offline Trouble with .linktoprevious when merging sections Windows 7 64bit Trouble with .linktoprevious when merging sections Office 2013
Expert
Trouble with .linktoprevious when merging sections
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default Trouble with .linktoprevious when merging sections

I'm using a macro based on one found in this forum to split a document into its constituent 'chapters'. Each chapter starts with "Heading 1" and finishes with a section break because each chapter has a unique primary header and primary footer.

The split out chapter therefore contains an extraneous page at the end which comes after the section break.



To remove the extraneous page and preserve headers and footers I need to link the last section of the split out document to the previous. However it appears that it is not sufficient to link just the primary headers and footers, it is also necessary to link the first page, primary and even page headers and footers (six items altogether). Deleting the section break without linking all 6 items results in the headers and footers disappearing.

This is the code I am using to do the linking

Code:
Sub sbRemoveLastPage(ThisDoc As Document)

Dim myrange As Range

   On Error GoTo foundError
   With ThisDoc.StoryRanges(wdMainTextStory).Sections.Last
      .Headers(wdHeaderFooterPrimary).LinkToPrevious = True
      .Headers(wdHeaderFooterFirstPage).LinkToPrevious = True
      .Headers(wdHeaderFooterEvenPages).LinkToPrevious = True
      .Footers(wdHeaderFooterPrimary).LinkToPrevious = True
      .Footers(wdHeaderFooterFirstPage).LinkToPrevious = True
      .Footers(wdHeaderFooterEvenPages).LinkToPrevious = True
    End With
   Set myrange = ThisDoc.StoryRanges(wdMainTextStory).Characters.Last
   Do While myrange.Characters.Last.Previous = ChrW(12) Or myrange.Characters.Last.Previous = ChrW(13)
      myrange.Characters.Last.Previous.Delete
   Loop
   Exit Sub
foundError:
   MsgBox "We got an error", vbOKOnly
   If Err.Number = 4605 Then
         ' retry linking
         ThisDoc.StoryRanges(wdMainTextStory).Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
         Resume Next
      End If
End Sub
This code may process one or more chapters before stopping with error 4605 (No previous section - which should not be the case). If I elect to debug and press F5 the macro happily continues but may stop again with the same error 4605. Single stepping with F8 also works fine.

This problem has been described elsewhere and marked as resolved.

https://social.msdn.microsoft.com/Fo...?forum=worddev

I've tried all of the solutions proposed in the link above but I get the same problem with all of them.

If I add a pause at the start of the code above it progresses further but then generates a different error

error 5496 - check that the drive door is closed.

Last week this code worked without a hitch.

So the issues are

1. What is the easiest way to merge two section preserving the header and footer from the first section

2. Why is the on error statement not operating. I never see the msgbox just error dialog boxes.
Reply With Quote
  #2  
Old 03-07-2017, 08:43 AM
slaycock slaycock is offline Trouble with .linktoprevious when merging sections Windows 7 64bit Trouble with .linktoprevious when merging sections Office 2013
Expert
Trouble with .linktoprevious when merging sections
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

I solved error 5496. This was due to the source file being on a network share and consequently the individual sections were also being saved to the same network share.

I moved the source file to a local folder and the 5496 error went away.
Reply With Quote
  #3  
Old 03-07-2017, 02:08 PM
macropod's Avatar
macropod macropod is offline Trouble with .linktoprevious when merging sections Windows 7 64bit Trouble with .linktoprevious when merging sections Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by slaycock View Post
What is the easiest way to merge two section preserving the header and footer from the first section
See, for example: https://www.msofficeforums.com/word/...html#post20227
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 03-08-2017, 05:38 AM
Charles Kenyon Charles Kenyon is offline Trouble with .linktoprevious when merging sections Windows 10 Trouble with .linktoprevious when merging sections Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

You may be able to avoid needing separate sections by using one or more StyleRef fields.
Reply With Quote
  #5  
Old 03-08-2017, 07:41 AM
slaycock slaycock is offline Trouble with .linktoprevious when merging sections Windows 7 64bit Trouble with .linktoprevious when merging sections Office 2013
Expert
Trouble with .linktoprevious when merging sections
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Thanks for the link. I suspected it wouldn't be as simple as .pageformat=.pageformat.

Based on the link I now have a working process. For folks who follow on its important to have .papersize before .orientation (as in the example) because setting the paper size resets orientation to portrait.

Quote:
You may be able to avoid needing separate sections by using one or more StyleRef fields.
Stylerefs would have made the heading content easier to automate but doesn't solve the problem as some of the chapters have portrait and landscape sections with the landscape sections usually being at the end of the chapter.

I gave up on using stylerefs in headers because they don't respect the outline level as a boundary for the search. e.g. if I use a styleref in the header for Heading 2 and there is no Heading 2 before the previous Heading 1 then you get a Heading 2 reference from the previous chapter rather than a null result.

We found this out when using the following stylrefs in the header

{styleref "Heading 1" \n} {Styleref "Heading 1"}
{styleref "Heading 2" \n} {Styleref "Heading 2"}

and found we were getting stuff like

3.2.P.3 Manufacture
3.2.P.2.6 Compatibility

Now - does anyone have any input on why I get a standard dialog box for an error rather than my MsgBox for the 4605 error
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with .linktoprevious when merging sections Word VBA - "Page left Blank" between Sections to make all Sections start on even page Cov_ATC Word VBA 10 11-28-2021 03:41 PM
Trouble with .linktoprevious when merging sections Merging different sections in one document apanbasu Word 5 03-04-2017 07:18 PM
Four Sections salvarez83 PowerPoint 2 02-22-2017 12:40 PM
Pagination in Different Sections JBCook91 Word 5 02-12-2014 01:20 AM
Powerpoint Sections franglais31 PowerPoint 1 02-16-2013 08:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:04 PM.


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