Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where are the footers coming from? 1

Status
Not open for further replies.

lemonhalls

Technical User
Jun 11, 2004
52
CA
I am trying to merge files from one document into one big doc and trying to insert page number footer to every page except the first wo.

I have two places in my code where I see the word footer, in mergeDoc and insertPageNumFooter.

The footer, page number, and a text about my company seem to be inserted when mergeDoc calls insertfiles. But the original files dont' have a footer. Where are the page numbers coming from?


Sub mergeDoc()
'extracts the files from the folder and inserts them
'onto active document one after another

Dim activeDir As String

'allows user to input the name of the folder each
'time instead of defaulting the folder location
'this is more flexible in case the folder locations gets changed
activeDir = "f:\dbprinterdoc"
'InputBox _
'(Prompt:="Enter the path where the files to be merged are stored. End with '\', for example U:\SYS\PSMP-PGSPS\", _
'Title:="Path", Default:="U:\") 'sets the default location in the textbox

With Application.FileSearch
.LookIn = activeDir 'folder with old files
End With

'move to the end of the document to insert files
Selection.EndKey Unit:=wdStory, Extend:=wdMove

With Application.FileSearch
.NewSearch
.LookIn = activeDir
.SearchSubFolders = False
.FileName = "*.doc"
.MatchTextExactly = False

'retrieves the files in alphabetical order
If .Execute(SortBy:=msoSortByFileName, _
sortOrder:=msoSortOrderAscending) > 0 Then

For i = 1 To .FoundFiles.Count
Selection.TypeParagraph
Selection.InsertFile FileName:=.FoundFiles(i), Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False

ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
Next i
End If
End With
End Sub

__________________
Sub insertPageNumFooter()
Dim mySection As Section

With ActiveDocument
For Each mySection In .Sections
If mySection.Index = 1 Then
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = True
ElseIf mySection.Index = 2 Then
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = True
Else
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = False

'.Sections (mySection.Index) _
'.Footers(wdHeaderFooterPrimary).PageNumbers. _
'Add PageNumberAlignment:=wdAlignPageNumberRight
End If
Next mySection
End With
End Sub
 
where are the footers coming from?
From the doc where you insert the files ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
But I haven't set any footers and headers into the doc where the files are merged onto.

and the company text, doesn't usually appear on my documents, but it does normally on the files I'm importing into my active doc.

 
How is the big document created ? From which template ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It's actually not created from a template, just a document, I copied the cover page from the original document, then run coes to insert a TOC, and insert the files one by one from a specified folder.

If you don't find it too troublesome,

this is the code in procedure order:

Sub margin()
With ActiveDocument.Paragraphs
.LeftIndent = CentimetersToPoints(0.3)
.RightIndent = CentimetersToPoints(0)
End With

With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(0.95)
.BottomMargin = CentimetersToPoints(1.27)
End With
End Sub

Sub insertTOC()
'inserts the Table of Contents or TOC to the second page
'it first moves the cursor to the end of the first page (cover page)
'then inserts a page break to begin the table of contents

'moves the cursor to the end of the document
Selection.EndKey Unit:=wdStory, Extend:=wdMove

'insert page break where the cursor is currently (bottome of first)
Selection.InsertBreak Type:=wdSectionBreakNextPage

'if for some reason the format of the previous page was carried over
'ie, bolding, or italic, this will clear the page back to the original setting
Selection.ClearFormatting

'Inserts the text Table of Contents
Selection.TypeParagraph
Selection.TypeText Text:="Table Of Contents"
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle

Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

'inserts a table of contents into the document.
'entries are based on text with Heading 2 in the document
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
:="Heading 2,1", UseHyperlinks:=False, HidePageNumbersInWeb:=True, _
UseOutlineLevels:=True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With

'inserts a page break at the end of the table of contents
Selection.InsertBreak Type:=wdSectionBreakNextPage

End Sub
________________________________________________
Sub mergeDoc()
'extracts the files from the folder and inserts them
'onto active document one after another

Dim activeDir As String

'allows user to input the name of the folder each
'time instead of defaulting the folder location
'this is more flexible in case the folder locations gets changed
activeDir = "f:\dbprinterdoc"
'InputBox _
'(Prompt:="Enter the path where the files to be merged are stored. End with '\', for example U:\SYS\PSMP-PGSPS\", _
'Title:="Path", Default:="U:\") 'sets the default location in the textbox

With Application.FileSearch
.LookIn = activeDir 'folder with old files
End With

'move to the end of the document to insert files
Selection.EndKey Unit:=wdStory, Extend:=wdMove

With Application.FileSearch
.NewSearch
.LookIn = activeDir
.SearchSubFolders = False
.FileName = "*.doc"
.MatchTextExactly = False

'retrieves the files in alphabetical order
If .Execute(SortBy:=msoSortByFileName, _
sortOrder:=msoSortOrderAscending) > 0 Then

For i = 1 To .FoundFiles.Count
Selection.TypeParagraph
Selection.InsertFile FileName:=.FoundFiles(i), Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False

ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
Next i
End If
End With
End Sub
_____________________________________________________
Sub insertPageNumFooter()
Dim mySection As Section

With ActiveDocument
For Each mySection In .Sections
If mySection.Index = 1 Then
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = True
ElseIf mySection.Index = 2 Then
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = True
Else
.Sections(mySection.Index) _
.PageSetup.DifferentFirstPageHeaderFooter = False

'.Sections (mySection.Index) _
'.Footers(wdHeaderFooterPrimary).PageNumbers. _
'Add PageNumberAlignment:=wdAlignPageNumberRight
End If
Next mySection
End With
End Sub
 
It's actually not created from a template
Really ? Even the default one (Normal.dot) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm not the brightest when it comes to templates..

can you explain why the company name, page number shows up on the report when I call the code, but not when I open a new doc if it is opened from the default normal.dot?

moreover, the footers automatically do not insert themselves on teh cover page nor TOC just like it does when the report was manually done.

confused.
 
One of the problems is that you are inserting the files, not copying them. Word assumes (correctly) when you insert a file, that you want all of it, including the header/footer story. Here is a suggested reorder of the procedures.

Sub MyMainSub()
Dim aDoc As Document
Set aDoc = ActiveDocument

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage

With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
Content.End).PageSetup
.TopMargin = CentimetersToPoints(0.95)
.BottomMargin = CentimetersToPoints(1.27)
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With

aDoc.Bookmarks.Add Range:=Selection.Range, Name:="myToC"

Selection.InsertBreak Type:=wdPageBreak
Selection.InsertBreak Type:=wdPageBreak
Selection.InsertBreak Type:=wdSectionBreakNextPage

mergeDoc
MakeToC
aDoc = Nothing
End Sub



This code:
Makes a new section, and bookmark, for the ToC
Makes a new section and sets margins, for the incoming content,
Calls a file search sub
Calls a ToC sub

The filesearch sub (mergeDoc) should only:
Search for files
Call the copy sub for each found file

A copy sub that:
opens each foundfile and copies the contents back to the target doc

A make Toc sub that:
Goes to the bookmark in the ToC section, and generates a ToC


Here is a bit more detail

Assumption: a document with ONE page, the cover page.

Main Sub:

1. make the active document a document object. You can then refer to it explicitly, and it makes for shorter code. Be sure to destroy the object at the end of the Sub Set aDoc = Nothing

Code:
  Dim aDoc As Document
  Set aDoc = ActiveDocument

2. Go to end of document, and make a SECTION break. NOTE: in your posted code you comment about making a page break, but your code is actually inserting a section break.

Code:
  Selection.EndKey Unit:=wdStory
  Selection.InsertBreak Type:=wdSectionBreakNextPage

3. Set the margins for the rest of the document, and set it so the footers are NOT different for the rest of the document. I think this is what you are trying to do.

Code:
    With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
        Content.End).PageSetup
       .TopMargin = CentimetersToPoints(0.95)
        .BottomMargin = CentimetersToPoints(1.27)
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
    End With

3. Insert a bookmark to mark where the ToC will go.
Code:
  aDoc.Bookmarks.Add Range:=Selection.Range, Name:="myToC"

4. Insert 2 PAGE breaks, then a SECTION break. This will make sure that when the ToC generates, if it expands more than a page, the section headers are maintained
Code:
Selection.InsertBreak Type:=wdPageBreak
Selection.InsertBreak Type:=wdPageBreak
  Selection.InsertBreak Type:=wdSectionBreakNextPage

5. Call the file gathering routine. OK, first off, this routine is inserting a file. Word assumes (correctly) that you want all of the file, including the header/footer story. What I would do is open the file, grab the MainText story, copy it, return to target doc, and paste.

Code:
   mergeDoc

The procedure mergeDoc is OK but I would remove the adding page break. Just use it to search to files. Declare a string variable in it, (Dim sFile As String), to pick up each FoundFile, then call another routine – JustMain.

For i = 1 To .FoundFiles.Count
sFile = .FoundFiles(i)
JustMain(sFile)
Next i

Here is the code for JustMain, which inserts a section break, opens the FoundFiles, copies just the main story (NOT the header/footer story) and pastes it into the target doc. It then closes the FoundFiles.

Code:
Sub JustMain(sFile As String)
Dim TempDoc As Document
Dim lngEndDoc As Long
Dim r As Range
Selection.InsertBreak Type:=wdSectionBreakNextPage
Application.Documents.Open FileName:=sFile
    Set TempDoc = ActiveDocument
Selection.EndKey Unit:=wdStory
Selection.Collapse Direction:=wdCollapseEnd
lngEndDoc = Selection.Range.End

Set r = TempDoc.Range(Start:=0, End:=lngEndDoc)
    r.Select
    r.Copy
aDoc.Activate
Selection.Paste
   Set r = Nothing
TempDoc.Close
    Set TempDoc = Nothing
End Sub

6 Call the MakeToC procedure.

Code:
   MakeToC

Call it after the filesearch routine has collected all the content. This is assuming the heading for the ToC is in fact a style. Please use styles, rather than manually formatting, or even formatting by code. This will also ensure that the table of Contents is itself in the ToC.

Code:
Sub MakeToC()
    Selection.GoTo What:=wdGoToBookmark, Name:="myToC"
Selection.TypeText Text:="Table of Contents"
    Selection.Style = aDoc("Heading 1")
    With ActiveDocument
         .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
          True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
          :="Heading 2,1", UseHyperlinks:=False, HidePageNumbersInWeb:=True, _
          UseOutlineLevels:=True
          .TablesOfContents(1).TabLeader = wdTabLeaderDots
          .TablesOfContents.Format = wdIndexIndent
     End With
End Sub



Gerry
 
Gerry,

Thanks for taking the time to reply with such a detail solution! I'm looking forward to trying out your code and improving my code.

In the makeToc sub, I'm getting an error on this line
Selection.Style = aDoc("Heading 1")

compile Error: wrong number of arguments or invalid property assigment.

any idea why? should i post more code?

Thanks!

S
 
Ooops, it should be: aDoc.Styles("Heading 1")

It is applying a specific Style, that is Heading 1. If that Style is not available, you will get an error. My point, really, was that you should be using styles. You had manual formatting in your code. While it is certainly possible to apply format to text via code, using Styles is much, much, better. In particular for the generation of ToC. Tables of Contents are generated by Styles.



Gerry
 
Selection.Style = aDoc.Styles("Heading 1")

I'm stil getting object required on this line in makeTOC.

I think the two page breaks then a section break is a great idea, except now I have two blank pages after the TOC page. should I run a code at the end to check for empty pages and delete them?

thanks!

S
 
Gerry,

When it comes to inserting files, do the styles I create get inserted into the current document as well>?

Thanks,

S
 
Selection.Style = aDoc.Styles("Heading 1")

I'm stil getting object required on this line in makeTOC.

Then you most likely do not have that specific style, which is a default style. Most likely you have modified it. In any case, it is just to point out that it is better to apply SOME style. You can use what ever one you want.

When it comes to inserting files, do the styles I create get inserted into the current document as well

Huh? Styles created where? I have no idea what you mean. You open a file - if you copy the contents to another file, the target file styles trump...if they are applied. You can copy and paste the source file format, if you want. However, the source file styles are not copied with it. You can copy over styles via code, but I would suggest not doing that right now.

Gerry
 
Gerry,

whenever I use aDoc.something, it always says object required. I set it up the way you advised as follows
Sub MyMainSub()
Dim aDoc As Document
Set aDoc = ActiveDocument

mergeDoc
blah..
end sub

This means I can use aDoc inside mergeDoc, justMain etc right?
or is there a global location I should be putting this variable?

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top