Consulting

Results 1 to 7 of 7

Thread: Insert text into next blank Page

  1. #1
    VBAX Regular
    Joined
    Aug 2016
    Posts
    11
    Location

    Insert text into next blank Page

    Hi - I've got a macro that opens up several Word documents and copies/combines all of the contents into 1 file. I'm having an issue with the "Pasting" portion of the macro. For example, File #2 is copying over right where File #1 ended. What I'd like to happen is to have File #2 "go to the next page (which is blank) and paste the contents".

    Would I use a PageBreak?

    I've tried the following code, but am getting a compile error

    Selection.InsertBreak.Type = wdPageBreak


    any help is greatly appreciated.

    thanks

  2. #2
    This is essentially the process at http://www.gmayor.com/Boiler.htm though rather than paste, the process inserts the file(s) To paste the clipboard content at the end of the document on a new page
    Sub PasteAtEnd()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        oRng.Collapse 0
        oRng.InsertBreak wdSectionBreakNextPage
        oRng.End = oRng.End + 1
        oRng.Collapse 0
        oRng.Paste
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Aug 2016
    Posts
    11
    Location
    Hi Graham - thanks for the reply. I tried using your code but I'm obviously not using it correctly. However, I did have a look at your website, and the Boiler addin is doing exactly what I need (very impressive).
    Below is what I'm using right now, but it still isn't starting the 2nd file (based on the selection from Combobox2) on "the next blank page". I'm thinking there has to be an easier way than the way I'm doing it below (IE: 1 line of code?), but I can't figure it out. Would you be able to have a look and offer a suggestion?

    [UVBA]
    '''Opens the 1st file (based on user selection from ComboBox1) and inserts the entire document
    ChangeFileOpenDirectory "C:\Users\MyDirectory\Documents\Word Automation\Files\"
    Selection.InsertFile FileName:="Test " & ComboBox1.Value & " Template.dotx", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
    ActiveDocument.Range.Collapse 0
    ActiveDocument.Range.InsertBreak wdSectionBreakNextPage
    ActiveDocument.Range.End = ActiveDocument.Range.End + 1
    ActiveDocument.Range.Collapse 0

    '''Opens the 2nd file (based on user selection from ComboBox2) and inserts the entire document
    ChangeFileOpenDirectory "C:\Users\MyDirectory\Documents\Word Automation\Files\"
    Selection.InsertFile FileName:="Test " & ComboBox2.Value & " Template.dotx", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
    ActiveDocument.Range.Collapse 0
    ActiveDocument.Range.InsertBreak wdSectionBreakNextPage
    ActiveDocument.Range.End = ActiveDocument.Range.End + 1
    ActiveDocument.Range.Collapse 0
    [UVBA]

  4. #4
    ActiveDocument.Range is a fixed entity. You need to set the selection point with respect to that range or set a variable to that range and work with the selection or variable. The variable is preferable e.g.

    Dim strPath As String
    Dim oRng As Range
        'declare the path as a string variable
        strPath = "C:\Users\MyDirectory\Documents\Word Automation\Files\"
        ''Opens the 1st file (based on user selection from ComboBox1) and inserts the entire document at the current selection
        Selection.InsertFile Filename:=strPath & "Test " & ComboBox1.Value & " Template.dotx"
        'set a variable to the document range
        Set oRng = ActiveDocument.Range
        'Collapse the range to its end
        oRng.Collapse 0
        'Insert a section break at the range (the end of the document)
        oRng.InsertBreak wdSectionBreakNextPage
        'Move the end of the range after the  break
        oRng.End = oRng.End + 1 'or oRng.End = ActiveDocument.Range.End
        'Collapse the range to its end
        oRng.Collapse 0
    
        '''Opens the 2nd file (based on user selection from ComboBox2) and inserts the entire document
        oRng.InsertFile Filename:=strPath & "Test " & ComboBox2.Value & " Template.dotx", Range:="orng"
        oRng.End = ActiveDocument.Range.End
        oRng.Collapse 0
        oRng.InsertBreak wdSectionBreakNextPage
        oRng.End = oRng.End + 1
        oRng.Collapse 0
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Aug 2016
    Posts
    11
    Location
    Hi Graham - thanks for your help. I copied your code directly into my document and I'm getting an error at

    Range:="orng"

    if I use double quotes (""), the code will run without error, but it isn't working as expected.

    oRng.InsertFile Filename:=strPath & "Test " & ComboBox2.Value & " Template.dotx", Range:="orng"

  6. #6
    VBAX Regular
    Joined
    Aug 2016
    Posts
    11
    Location
    Graham - I did some additional searching and was able to come up with this code below and it seems to be working as expected. Thanks for you help, it got me going in the right direction and I was able to learn some more about VBA.

    
    
    Dim i As Integer
    For i = 1 To ComboBox_Cover.Value
    
    
    Dim j As String
    j = Me.Controls.Item("ComboBox" & i).Value
    
    
    Selection.InsertFile FileName:=strPath & "Test " & j & " Template.dotx", Range:=""
    Selection.InsertParagraphAfter
    Selection.InsertBreak Type:=wdSectionBreakNextPage
    Selection.Collapse direction:=wdCollapseEnd
    
    
    Next i

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    j is a strange variable name for a string

    Dim i As Integer, strTest as String
      For i = 1 To ComboBox_Cover.Value 
          strTest = "Test " & Me.Controls.Item("ComboBox" & i).Value 
          With Selection
              .InsertFile FileName:=strPath & " strTest & " Template.dotx", Range:="" 
              .InsertParagraphAfter 
              .InsertBreak Type:=wdSectionBreakNextPage 
              .Collapse direction:=wdCollapseEnd 
         End With
      Next i
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •