My Macro in MS word keeps getting me minor problem on the final
document(in .rtf)

For instance,

I created 5 rtf files from SAS. I'm combining them into
one RTF file using a VB code.
But the problem is that page numbers located Header in each RTF file
get shifted to
the next line. In original RTF files, Header is as below

___________________________________________________________
Table
Title
Page 1 of 2

But after I run this code, page number moved below the table title as
below

___________________________________________________________
Table Title
Page 1 of 2



Can anyone see the problem from the following code?


--------------VB Code Starts
here---------------------------------------
Sub Combine_files()

filepath = InputBox("Name of directory with rtf files, with no ending
slash! (e.g. c:\define\nda)")
Documents(1).Close SaveChanges:=wdDoNotSaveChanges
   Documents.Add DocumentType:=wdNewBlankDocument

Application.ScreenUpdating = False

Set fs = Application.FileSearch
   With fs
       .LookIn = filepath
       .FileName = "*.RTF"
       If .Execute = 0 Then
           MsgBox "There were no files found."
       End If
   End With


   For i = 1 To fs.FoundFiles.Count

       Documents.Open FileName:=fs.FoundFiles(i), Visible:=True
       Selection.HomeKey Unit:=wdStory
       Selection.EndKey Unit:=wdStory, Extend:=wdExtend
       Selection.Copy

       Documents(fs.FoundFiles(i)).Close
SaveChanges:=wdDoNotSaveChanges
       Selection.Paste

       If i < fs.FoundFiles.Count Then
           Selection.InsertBreak Type:=wdSectionBreakNextPage
       End If

   Next i

   For Each s In ActiveDocument.Sections
       With s.Headers(wdHeaderFooterPrimary).PageNumbers
           .RestartNumberingAtSection = True
           .StartingNumber = 1
       End With
   Next s

   Application.ScreenUpdating = True

   ActiveDocument.SaveAs FileName:=filepath & "\all.rtf",
fileformat:=wdFormatRTF

   MsgBox "I'm Done!"

   End Sub

-----------------VB Code Ends
here-------------------------------------------

Reply via email to