Section Break Changes on File Insert with Global Template

M

Marco

I’ve created a few functions and procedures in a template loaded via
options/file locations/start up to enable users in a workgroup to insert
letterheads and footers to new or pre-existing documents. The letterheads
and footers are stored in individual templates (though I’ve also tested with
plain .doc). In each of the templates, a continuous section break follows
each of the letterheads. The purpose of the section break is to preserve the
formatting of the destination document, since the letterheads are fairly
wide, and I don’. Version of Word is 2003.

While I was in the startup-to-be template, I created and tested the
functions and procedures to make all this happen. No problems. When I saved
it to the start up directory and restarted word to load from it, I ran into a
problem. When a letterhead is inserted now, the section break is no longer
continuous but causes a page break.

I’ve tried this a few different ways, but the end result is always the same.
Here’s a look:

Select Case intMethod
Case 1 ' FIELD EMPTY
Dim strIncludeText As String
strIncludeText = Replace(PathToTemplates, "\", "\\") &
strLetterhead
strIncludeText = "INCLUDETEXT """ & strIncludeText & """" & "
bmkLetterhead"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:=strIncludeText, PreserveFormatting:=True
Case 2 ' FIELD: INCLUDE TEXT
Dim strIncludeText As String
strIncludeText = Replace(PathToTemplates, "\", "\\") &
strLetterhead
strIncludeText = """" & strIncludeText & """" & " bmkLetterhead"
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldIncludeText, _
Text:=strIncludeText, PreserveFormatting:=True
Case 3 ' INSERT FILE: Linked
Selection.InsertFile FileName:=strLetterhead,
Range:="bmkLetterhead", Link:=True
Case 4 ' INSERT FILE: Not Linked
Selection.InsertFile FileName:=strLetterhead,
Range:="bmkLetterhead", Link:=False
Case 5 ' COPY & PASTE
Documents.Open FileName:=strLetterhead,
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False,
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto,
XMLTransform:="", _
Visible:=False
Documents(strLetterhead).Bookmarks("bmkLetterhead").Select
Selection.Copy
Documents(strLetterhead).Close False
Selection.PasteAndFormat (wdPasteDefault)
End Select

One other thing that I tried was inserting the section break before
inserting the document. That also didn’t work. Here’s the code:

Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakContinuous
Selection.HomeKey Unit:=wdStory

My requirement is to actually insert the letterhead rather than link to it,
but I tried these other methods as alternatives hoping that one would work.
Any ideas what is causing this or how I can work around it?
 
D

Dawn Crosier

I have done this, however, I have always already had the Section Break in
the document before importing the additional text into the document. When
you insert the Continuous Section break, make sure that you are in that
section, and format the margins to accommodate what you are bringing into
the document. I may have made it easy on myself by enclosing my letterhead
in a table. That way I could grab the table out of my master document and
insert it into my newly created document. Notice too, that I have a
bookmark in my end document which I then paste into.

Here is the code that I have used to accomplish that task.

Sub AutoNew()
Dim appWord As Word.Application
Dim strLetter As String
Dim strTestFile As String
Dim strDocsPath As String
Dim strTemplatePath As String
Dim strFileName As String
Dim strDate As String

On Error GoTo EH

Application.ScreenUpdating = False
Set appWord = GetObject(, "Word.Application")
strFileName = "LetterHeadTable.doc"
strDocsPath = DocsDir
strTemplatePath = TemplateDir
strLetter = strTemplatePath & strFileName

appWord.Documents.Open strLetter
ActiveDocument.Tables(1).Range.Select
Selection.Copy
ActiveDocument.Close saveChanges:=wdDoNotSaveChanges
Selection.GoTo what:=wdGoToBookmark, Name:="letterhead"
Selection.Paste
.....

I then go onto to Format the table without borders. For some reason, I
never could get the table to stay "borderless" as it came into the document
so I just went with it and called a procedure to format table without
borders.

Procedures which are called from the above code are:

Public Function TemplateDir() As String
Dim appWord As Word.Application

On Error GoTo EH

Set appWord = GetObject(, "Word.Application")
TemplateDir = appWord.Options.DefaultFilePath(wdWorkgroupTemplatesPath) &
"\"

EH_Exit:
Exit Function

EH:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
Resume EH_Exit
End If
End Function

Public Function DocsDir() As String
Dim appWord As Word.Application

On Error GoTo EH

Set appWord = GetObject(, "Word.Application")
DocsDir = appWord.System.PrivateProfileString("", _
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders", _
"Personal") & "\"

EH_Exit:
Exit Function

EH:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
Resume EH
End If

End Function

Hope this helps you.

--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"

This message is posted to a newsgroup. Please post replies and questions to
the newsgroup so that others can learn as well.
 
M

Marco

Dawn,

Thanks for the reply. I really do appreciate the sample code,
especially the DocsDir function which has been handy elsewhere. Your
suggestion, however, was essentially the same as the fifth method I had
tried, and it produced the same result. :(

I should repeat that this continuous-to-new-page conversion *only*
occurs when the procedure is executed (via an addition to the Insert
menu that resides in the global template) from a *new* document. When
the global template itself is opened and the procedure is executed from
there, the section breaks do not convert.

I have examined this strange-to-me conversion more closely since
posting, and I've discovered that the value of these converted section
breaks is not 2 (the value of wdSectionNewPage) or 0 (the value of
wdSectionContinuous), but 9999999, which doesn't seem to correspond to
any of the wdSectionStart constants.

My solution thus far has been to...

1. Count the sections in the source document (the template containing
the letterhead)

2. Copy the letterhead by selecting a bookmark that spans a table,
paragraph, and continuous section break (see method five in my previous
post)

3. Go to the beginning of the destination document (doc I'm sticking
the letterhead in) and paste the selection

4. Step through the starting sections based on the count from step one,
looking for any section break <> 0 and changing to 0 (works for most of
those five methods I posted--don't remember which ones produced the
errors...prolly the linked ones)

The downside of the fifth method (or the way I'm accomplishing it) is
that it has required another workaround; that is, resetting the
FileOpenDirectory to its previous value. If this isn't done, then when
a user tries to open or save a new file, the path they see is to the
folder where the letterheads are stored. In my case, they only have
read access to that folder, and it's a bother for them to navigate
elsewhere. My workaround has been simply to grab the value stored in
Application.Options.DefaultFilePath(wdUserOptionsPath) beforehand and
then restore it before the code is done executing.

On a final note in reference to inserting the break into the document
before importing, it's my understanding that the section break contains
the formatting for its section. It shouldn't be necessary, therefore,
to insert a break and format its section before importing. It should
only be necessary to import the break.

All these things said, I'm no expert, so if I'm mistaken in any way,
please let me know.

Thanks again for the response.
 
D

Dawn Crosier

Marco -

I am not sure what to tell you. My destination document has a paragraph
which has been bookmarked and named letterhead.
Immediately below that is a second paragraph which is followed by a
continuous section break. The bookmark letterhead is contained in the first
section of the document. That section has been formatted with different
margins because the table that I grab is wider than what we want the bulk of
the letter to use as margins. This is why I suggested formatting the
section prior to inserting the table with your letterhead.

I just tested with setting up my new document totally with code, and here is
what I am doing to create the section break and insert the bookmark to paste
the letterhead into.

Sub InsertSectionBreak()
'
' Formats a blank document in preparation for adding the
' letterhead table into the document.
'
Selection.TypeParagraph
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="letterhead"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.InsertBreak Type:=wdSectionBreakContinuous
Selection.MoveUp Unit:=wdLine, Count:=1
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.5)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(0.25)
.RightMargin = InchesToPoints(0.25)
.DifferentFirstPageHeaderFooter = True
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Date"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub


--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"

This message is posted to a newsgroup. Please post replies and questions to
the newsgroup so that others can learn as well.
 
M

Marco

Dawn,

What I created had to allow users to insert any of a slew of letterheads and
customized footers to new or *existing* documents. Very few of these
letterheads share margin settings. They do, however, have section breaks,
and since a section break contains the formatting for its section, copying it
seems easier than what you're suggesting. If I were to create a new section
from scratch in the destination document, I'd have to grab the settings from
the source document first. Although that's not a lot of work to make happen,
it does seem superfluous when I'm already copying and pasting the letterhead
and can just include the section break with the selection. It works, at
least, when copying and pasting or linking. It doesn't work with .InsertFile
(without linking). The only way to get that to work, I believe, would be to
do as you've suggested: create the section from scratch. I hope that make
sense.

I guess my question is why is the value of the section break changed from 0
to 9999999 when it's copied and pasted via code that is in an attached global
template but remain constant when the code is executed directly from the
global template? Seems like a bug to me.

Also, are you not running into the same problem with the file dialog box
remembering the path to your letterhead?
 
D

Dawn Crosier

Marco -

I am not sure why your section break would change values. Maybe someone
else can step in on this and provide some enlightenment to both of us.

No, I am not running into a problem with the file dialog remembering the
path to my letterhead document. Perhaps because I have my letterhead
document stored with the remainder of my workgroup templates?

--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"

This message is posted to a newsgroup. Please post replies and questions to
the newsgroup so that others can learn as well.
 

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

Top