View Single Post
 
Old 02-16-2024, 08:07 AM
syl3786 syl3786 is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 97
syl3786 is on a distinguished road
Unhappy Word Macro issue: Insert specific page between pages

Hi everyone,

I am currently developing a word macro to:

1) Ask user Yes/No ( to execute the macro or not)
2) Ask user to input page number (to inform the macro to insert the specific page after which page)
3) Pop out filedialog box to let user choose the specific document(it has one page only) for inserting.
4) Insert the user selected document after user input page.
5) set the inserted page's page number as "Different first page" and page n+1

For example:

0) The document has 10 pages, 1 section only.
1) User press "yes"
2) User input "2"
3) User selected the document for inserting from the filedialog box
4) The macro goes to page 2, insert a blank page after page 2. Then open the selected document, copy its table and paste to that blank page.
5) Now the inserted page number is page 3. But it should not show page number. So the macro set it as a new section, without showing page number (different first page. Then the page 4's page number will also show as p.3. (P.1,P.2, P.2a(the inserted document), P.3(actually is p.4)).

I have a problem where if there is a table at the start of the page, the inserted document will be in that table. I've attached a sample document.

Taking the sample document as an example:

0) The sample document has two pages, where page 2 starts with a small shaded table.

1) I used the "InsertTable" macro to input "1" and select the document for inserting.
2) However, the selected document was inserted into the shaded table.

Can someone help me solve this? I'm confused and have already tried recording how Microsoft Word inserts a new page, but the recorded code has the same issue.

sample document.docx
Result of running the word macro.docx
Doc for inserting.docx

Code:
Sub InsertTable()

    Dim intPage As Integer
    Dim strPage As String
    Dim strFile As String
    Dim rng As Range
    Dim doc As Document
    Dim tbl As Table
    Dim sec As Section
    Dim hdr As HeaderFooter
    Dim fd As fileDialog
    Dim response As VbMsgBoxResult
   
    response = MsgBox("Do you need to insert table?")
    If response = vbNo Then Exit Sub

    strPage = InputBox("Insert after which page?")
    If strPage = "" Then Exit Sub

    intPage = CInt(strPage)

    ' Ask the user for the path to the specific document
    Set fd = Application.fileDialog(msoFileDialogFilePicker)
    With fd
        .Title = "Select Document"
        .AllowMultiSelect = False
        .InitialFileName = ActiveDocument.Path & "\"
        .Filters.Clear
        .Filters.Add "Word Documents", "*.docx"
        If .Show = True Then
            strFile = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
   
    ' Open the specific document
    Set doc = Documents.Open(strFile)
   
    ' Copy the first table from the specific document
    Set tbl = doc.Tables(1)
    tbl.Range.Copy
   
    ' Close the specific document
    doc.Close
   
    ' Set the range according to the user inputs
    Set rng = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=intPage + 1).GoTo(What:=wdGoToBookmark, Name:="\page")
   
    ' Insert a section break at the specified location
    rng.InsertBreak Type:=wdSectionBreakNextPage
   
    ' Insert the copied table at the specified location
    rng.PasteAndFormat (wdFormatOriginalFormatting)
   
    ' Remove the page number of the inserted table
    Set sec = ActiveDocument.Sections(ActiveDocument.Sections.Count)
    sec.PageSetup.DifferentFirstPageHeaderFooter = True
    For Each hdr In sec.Headers
        If hdr.Exists And hdr.IsHeader = True Then
            hdr.PageNumbers.RestartNumberingAtSection = True
            hdr.PageNumbers.StartingNumber = intPage
        End If
    Next hdr

   
End Sub
Reply With Quote