#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
Bearing in mind that there are no 'pages' in a Word document, if your selected range is in a table. then the following is an example of code to move the selection before the table.
Code:
Dim orng As Range Dim oTable As Table Set orng = Selection.Range If orng.Information(wdWithInTable) = True Then Set oTable = orng.Tables(1) orng.Start = oTable.Range.Start orng.Collapse 1 orng.Select Selection.SplitTable Set orng = Selection.Range orng.Text = "The selection is before the table" Else orng.Text = "The selection is not in a table" End If
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word Macro VBA | Insert images one by one, page by page | leonelcd | Word VBA | 1 | 11-05-2023 03:28 PM |
Printing Specific Page Issue | FCUCCMV | Word | 2 | 03-07-2022 07:15 AM |
Macro for word to add page break and specific text to end of document | pizzaman1 | Word VBA | 6 | 11-14-2014 11:25 PM |
Macro to insert multiple pictures to word to a specific size and text wrap | mescaL | Word VBA | 3 | 11-03-2014 10:51 PM |
Selection of all Text for a specific page in word is spanning selection across pages | ramsgarla | Word VBA | 9 | 12-05-2012 03:23 AM |