Add header to every word page where Excel table spills over to subsequent pages

Code Bot

I want to add header row to every word page where Excel table spills over to subsequent pages. Help is needed in the last part of the code. But I have pasted fullcode here for better understanding.

Current word file output - enter image description here

what I need is something like this - enter image description here

How can I add these headers from my excel sheet header row to every page in word file if teh excel table spills over to subsequent pages. Thanks

my code -

Sub GenerateWordFiles()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False

    Set wsSummary = ThisWorkbook.Sheets("Summary")
    Set wsFormat = ThisWorkbook.Sheets("Format")
    lrow = wsSummary.Range("D" & Rows.Count).End(xlUp).Row

    Set wbNew = Workbooks.Add
    Set wsDestination = wbNew.Sheets(1)

    ' Set the source and destination worksheets
    Set wsSource = wsSummary

    ' Check if a filter is applied in the source sheet
    If Not wsSource.AutoFilterMode Then
        wsSource.Range("B5").AutoFilter
    Else
        wsSource.AutoFilterMode = False
    End If

    lrowSource = lrow

    ' Define the filter range and filter value
    Set filterRange = wsSource.Range("B5:U" & lrowSource)
    filterValue = ">0"

    ' Apply the filter to the filter range
    filterRange.AutoFilter Field:=11, Criteria1:=filterValue

    ' Copy the filtered data (excluding headers) to the destination sheet
    Set copyRange = wsSource.Range("D5:D" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("A1")

    Set copyRange = wsSource.Range("Q5:Q" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("B1")

    Set copyRange = wsSource.Range("L5:L" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("C1")

    Set copyRange = wsSource.Range("N5:N" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("D1")

    Set copyRange = wsSource.Range("O5:O" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("E1")

    Set copyRange = wsSource.Range("P5:P" & lrowSource).SpecialCells(xlCellTypeVisible)
    copyRange.Copy wsDestination.Range("F1")

    wsDestination.Range("A1").Value = "Name of the Investment"
    wsDestination.Range("B1").Value = "Listed-Unlisted"
    wsDestination.Range("C1").Value = "Cost Value"
    wsDestination.Range("D1").Value = "Fair Value"
    wsDestination.Range("E1").Value = "Valuation Methodology"
    wsDestination.Range("F1").Value = "Sector"

'    With wsDestination.Range("A1:F1").Interior
'        .Pattern = xlSolid
'        .PatternColorIndex = xlAutomatic
'        .ThemeColor = xlThemeColorDark1
'        .TintAndShade = -0.499984740745262
'        .PatternTintAndShade = 0
'    End With
'
'    With wsDestination.Range("A1:F1").Font
'        .Bold = True
'        .Italic = False
'    End With

    lrowDestination = wsDestination.Range("A" & Rows.Count).End(xlUp).Row

    wsFormat.Columns("A:F").Copy

    wsDestination.Columns("A:F").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

    ' Turn off the filter in the source sheet
    wsSource.AutoFilterMode = False


    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Dim excelApp As Excel.Application
    Dim excelWorkbook As Excel.Workbook
    Dim excelWorksheet As Excel.Worksheet

    ' Initialize Word and open a blank document
    Set wordApp = New Word.Application
    wordApp.Visible = True ' Set to False if you don't want the Word application to be visible

    ' Create a new blank document
    Set wordDoc = wordApp.Documents.Add
    
    
    ' Set the page margins
    With wordDoc.PageSetup
        .LeftMargin = wordApp.InchesToPoints(0.5) ' Set the left margin to 0.5 inches
        .RightMargin = wordApp.InchesToPoints(0.5) ' Set the right margin to 0.5 inches
        .TopMargin = wordApp.InchesToPoints(0.5) ' Set the top margin to 0.5 inches
        .BottomMargin = wordApp.InchesToPoints(0.5) ' Set the bottom margin to 0.5 inches
    End With
    
    Set wsUI = ThisWorkbook.Sheets("UI")

    Text1 = wsUI.Range("D3").Value
    Text2 = wsUI.Range("D4").Value
    Text3 = wsUI.Range("D5").Value

    ' Write the title in the middle of the document
    With wordDoc.Content
        .Paragraphs.Add
        .ParagraphFormat.Alignment = wdAlignParagraphCenter ' Center alignment
        .Font.Name = "Calibri"
        .Font.size = 22
        .Font.Bold = True
        .InsertAfter Text1
    End With

    ' Add three lines of free text
    With wordDoc.Content
        .Paragraphs.Add
        .InsertAfter Text2
        .ParagraphFormat.Alignment = wdAlignParagraphLeft ' Left alignment
        .Font.Name = "Calibri (Body)"
        .Font.size = 12
        .Font.Bold = True
        .Collapse Direction:=wdCollapseEnd ' Move the cursor to the end of the text
        .InsertParagraphAfter ' Add a new paragraph
        .InsertAfter Text3
        .ParagraphFormat.Alignment = wdAlignParagraphLeft ' Left alignment
        .Font.Name = "Calibri (Body)"
        .Font.size = 12
        .Font.Bold = False
    End With

    ' Format the first line
    With wordDoc.Content.Paragraphs(2).Range
        .Font.size = 24 ' Example: Increase font size for the first line
        .Font.Bold = True ' Example: Apply bold formatting to the first line
        .ParagraphFormat.Alignment = wdAlignParagraphCenter ' Center alignment
        ' Apply other formatting as needed
    End With

    ' Set the worksheet to copy data from
    Set excelWorksheet = wbNew.Sheets(1) ' Replace with the actual worksheet name

    ' Move the cursor to the end of the document
    wordDoc.Content.End = wordDoc.Content.End - 1
    
    ' Copy the range from Excel
    excelWorksheet.Range("A1:F" & lrowDestination).Copy
    
    ' Move the cursor to the end of the document
    wordApp.Selection.EndKey Unit:=wdStory
    
    ' Paste the Excel table in the Word document after the existing content
    wordApp.Selection.Collapse Direction:=wdCollapseEnd ' Move the cursor to the end of the document
    wordApp.Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
       
    
    wbNew.Close False


    ' Get the folder path of the Excel file
    Dim excelFolderPath As String
    excelFolderPath = ThisWorkbook.path
    LocalFolderPath = GetLocalPath(excelFolderPath)
    
    ' Save the Word document with the specified name in the same folder
    Dim savePath As String
    savePath = LocalFolderPath & "\" & Text1 & ".docx"
    wordDoc.SaveAs2 savePath



    ' Clean up
    Set wbNew = Nothing
    Set wordDoc = Nothing
    Set wordApp = Nothing

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True


    MsgBox "Done!"

End Sub
Ike

Add wordDoc.Tables(1).Rows(1).HeadingFormat = True after your insertion of the table (wordApp.Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add a Header to Every Page in a PDF?

How to repeat table group header on every page in SSRS when outputting to [Word] Documents?

Formatting "spills over" to next rows: is this a bug in Excel 2013?

MigraDoc table goes over header on page 2?

Interop - prevent a table wrapping over two pages in Word

How to add table headers on every FPDF page

fpdf add title and table header on each page

How to add navbar to every admin page but not on shop pages

How to add pages without put addViewController for every page?

VBA Import page number of Word table into Excel

Add Header and Footer on all the pages except the first page?

How to add a table in word to a new page

Scrape a table iterating over pages of a website: how to define the last page?

Using excel to add a table in Word document

How to paste values on every page of word document from excel vba?

Header in every page SSRS

Is there a way to repeat the first two rows of a table as a header on each page in Word?

Adding page number in word header table C#

NReco PDF Generator overlapping table header when the table is split over pages

Unable to add a table in the header of a batch of word documents using VBA

How can I loop over pages and get data from every page with selenium?

How to get the Excel header on each print page to be the first row of the table

MS Word - add Appendix listing to table of contents without page number

number of occurrences of every word on page

ITextSharp table over several Pages

PDFPTable over multiple pages with repeating header

SSRS Repeat page header on every page

looking over previous and subsequent rows where condition is met by group R

Add a character to the start of every word