PDA

View Full Version : Insert text into next blank Page



GRCNC
08-25-2016, 12:57 PM
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

gmayor
08-25-2016, 08:51 PM
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

GRCNC
08-26-2016, 06:29 AM
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]

gmayor
08-28-2016, 08:44 PM
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

GRCNC
08-31-2016, 02:11 PM
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"

GRCNC
08-31-2016, 02:17 PM
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

gmaxey
08-31-2016, 07:29 PM
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