Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2009, 01:12 PM
Registered User
 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default Copy Table using Range Object

Hi All

I am using the Range object to copy the contents of one document to a new one. I have tried pasting and also just making the range of the new document equal that of the source document. However, I cannot get it to retain tables in the new document, even if I use FormattedText. Is there a way to copy tables over? Is it also possible to retain their formatting?

Many thanks for any help
Archie
 
Old May 1st, 2009, 09:47 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Can you try something like the following:

Sub CopyContentsWithTable_Across_Documents()
Dim oWD1 As Word.Document
Dim oWD2 As Word.Document
Dim oRange As Word.Range
Set oWD1 = ActiveDocument
Set oWD2 = Documents.Open("C:\Documents\Sample.docx")
Set oRange = oWD2.Range(oWD2.Paragraphs(50).Range.Start, oWD2.Paragraphs(62).Range.End)
oRange.Copy
oWD1.Paragraphs(1).Range.Select
Selection.Paste

End Sub

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 2nd, 2009, 07:10 AM
Registered User
 
Join Date: May 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you very much for your reply, Shasur.

The code you posted does work thank you. I amended it slightly as follows to work with the entire document:

Code:
Set oRange = oWD2.Range(oWD2.Paragraphs(1).Range.Start, oWD2.Paragraphs(oWD2.Paragraphs.Count).Range.End)
I am embarassed to say though that the code I thought was not working yesterday is doing so today. I have included it here in case it is of any use to anyone in the future:

Code:
Sub CopyDocContent()
'copy entire document
Dim dSource As Document, dTarget As Document
Dim rSource As Range, rTarget As Range

Set dSource = ActiveDocument
Set rSource = dSource.Range
Set dTarget = Documents.Add
Set rTarget = dTarget.Range
rTarget.FormattedText = rSource.FormattedText
Set dSource = Nothing
Set rSource = Nothing
Set dTarget = Nothing
Set rTarget = Nothing
End Sub
 
Sub CopyDocContent2()
Dim dSource As Document, dTarget As Document
Dim rSource As Range, rTarget As Range
Set dSource = ActiveDocument
Set rSource = dSource.Range
'do not copy the last carriage return
With rSource
    .End = .End - 1
    .Copy
End With
Set dTarget = Documents.Add
Set rTarget = dTarget.Range
rTarget.Paste
Set dSource = Nothing
Set rSource = Nothing
Set dTarget = Nothing
Set rTarget = Nothing
End Sub
Thank you for taking the time to respond.

Archie





Similar Threads
Thread Thread Starter Forum Replies Last Post
"Value2" property of Range object sektor Excel VBA 1 April 11th, 2008 03:28 AM
Range copy Excel crash workaround TrevK Excel VBA 1 March 22nd, 2008 12:30 AM
Copy Paste Dynamic Range jgrant Excel VBA 1 February 5th, 2008 10:13 PM
Copy a worksheet range to a new workbook cej2583 Excel VBA 2 March 14th, 2006 11:55 PM
Looping thro' / Copy from Range iwat03 Excel VBA 1 July 3rd, 2003 11:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.