cfs3 Posted November 10, 2003 Posted November 10, 2003 Would someone please either provide a sample or point me to an online resource for code that shows how to select a page from a word document and append it to another word docuement using automation in VB.NET? Thanks for any help. Conrad Quote
Shamil Posted November 11, 2003 Posted November 11, 2003 Conrad, This code works, tested: Public Function CopyPage(ByRef rdocTo As Word.Document, _ ByRef rdocFrom As Word.Document, _ ByVal vlngPageFrom As Long, _ Optional ByVal vfInsertPageBreakBeforeCopy As Boolean = True) As Long ' Add page to the end of the target document ' ' Arguments: ' rdocTo - target document ' rdocFrom - source document ' vlngPageFrom - source page number ' vfInsertPageBreakBeforeCopy - if =True - insert PageBreak mark before copied page ' Dim rngPageToCopy As Word.Range ' range of the page to copy Dim rng As Word.Range ' temporary range Dim intPageStart As Integer ' first char position of the page to copy Dim intPageEnd As Integer ' last char position o fthe page to copy ' check that source page number >=1 If vlngPageFrom < 1 Then CopyPage = 1 ' source page number should be >=1 Exit Function End If ' check that page number corresponds to a page in the source document rngPageToCopy = rdocFrom.GoTo(what:=Word.WdGoToItem.wdGoToPage, which:=Word.WdGoToDirection.wdGoToAbsolute, Name:=CStr(vlngPageFrom)) rng = rdocFrom.GoTo(what:=Word.WdGoToItem.wdGoToPage, which:=Word.WdGoToDirection.wdGoToAbsolute, Name:=CStr(vlngPageFrom - 1)) If (rngPageToCopy.Start = rng.Start) And (vlngPageFrom > 1) Then CopyPage = 2 ' source page doesn't exist Exit Function End If ' set and copy the contents of the source page into clipboard intPageStart = rngPageToCopy.Start intPageEnd = rngPageToCopy.Start + PageLength(rdocFrom, rngPageToCopy, vlngPageFrom) rngPageToCopy = rdocFrom.Range(intPageStart, intPageEnd) rngPageToCopy.Copy() ' set the position in the target document to copy the source rng = rdocTo.Content If (rng.StoryLength > 1) And (vfInsertPageBreakBeforeCopy = True) Then ' insert PageBreak mark at the of the target document if it has any text rng.MoveStart(Word.WdUnits.wdStory, 1) rng.InsertBreak(Word.WdBreakType.wdPageBreak) rng = rdocTo.Content End If rng.MoveStart(Word.WdUnits.wdStory, 1) ' paste from clipboard the content of the source page rng.Paste() CopyPage = 0 ' SUCCESS End Function Private Function PageLength(ByRef rdoc As Word.Document, ByRef rrngPage As Word.Range, ByVal vlngPageNo As Long) As Long ' return pagelength of a page. ' note: pageLength includes PageBreak mark if the page vlngPageNo is not the last page Dim rng As Word.Range rng = rdoc.GoTo(what:=Word.WdGoToItem.wdGoToPage, which:=Word.WdGoToDirection.wdGoToAbsolute, Name:=CStr(vlngPageNo + 1)) If rng.Start > rrngPage.Start Then ' this isn't the last page PageLength = rng.Start - rrngPage.Start - 1 ' no PageBreakMark Else ' this is the last page PageLength = rng.StoryLength - rrngPage.Start End If End Function HTH, Shamil P.S. If you find a solution without Clipboard usage please send it to me. Quote e-mail: shamil-usersATmns.ru
Recommended Posts