简体   繁体   中英

Combining contents of certain pages from several word documents using VBA

I have a question regarding combining contents of a couple of word documents using VBA in EXCEL . This could be very simple but I am new to VBA world... Currently, I can

  1. open a word document and copy all of its content
  2. paste it to a combined word document
  3. repeat steps 1 and 2

However, I am wondering how to skip the first page of all of the document using VBA. I tried tempDoc.Range(Start:=2).Select , but it did not work. Thanks for any suggestions!

Set objTempWord = CreateObject("Word.Application")
Set tempDoc = objWord.Documents.Open(Folderpath to Word Document)
Set objTempSelection = objTempWord.Selection
tempDoc.Range.Select
tempDoc.Range.Copy
objSelection.TypeParagraph
objSelection.Paste
objSelection.InsertBreak Type:=wdSectionBreakNextPage
tempDoc.Close

EDIT - Add another Word constant - change definition of Selection object

Assuming you have everything else defined this will start at page 2 and then select eveyrthing to end of file

const wdGoToAbsolute = 1
const wdGoToPage =  1 
const wdGoToNext = 2 
const wdStory = 6 
const wdExtend = 1 

Set objTempWord = CreateObject("Word.Application")
Set tempDoc = objWord.Documents.Open(Folderpath to Word Document)

With tempDoc.Application
   .Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:="2"
   .Selection.EndKey Unit:=wdStory, Extend:=wdExtend
   .Selection.Copy
End With

objSelection.TypeParagraph
objSelection.Paste
objSelection.InsertBreak Type:=wdSectionBreakNextPage
tempDoc.Close

Not a Word VB expert but I know the range object can accept arguments for the number of the first character and last character - https://msdn.microsoft.com/en-us/library/office/ff845882.aspx . So, you need to know how many characters the number of the first character on the second sheet is and how many characters the last one on the last sheet is. Then you can use the range object - https://msdn.microsoft.com/en-us/library/office/ff845882.aspx .

That said you mention Excel but don't explain why - is this a Word thing or Excel?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM