Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2014, 05:25 AM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default Copy table content between documents

I'm new to VBA, and I'd like to know if anyone has code for copying the content of a table from one document to a table in another document. Cells in the two documents are similar but formatted exactly the same. Right now, I am cut and pasting each cell's content, and its taking way too long. Thanks!

Basically I'm trying to move the cell content of Document A to Document B.

Alternatively, if you have a way to reformat a entire table Doc A to match Doc B, that would work too. Thanks!
Reply With Quote
  #2  
Old 06-10-2014, 07:29 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There is a problem with the content in your links - all it displays is some html code. Even once that's re-processed, the content appears to be the same for both files.

Rather than providing links to other sites, you could attach your documents to a post here. You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-10-2014, 09:04 PM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

Thank you. Here's the files.
Attached Files
File Type: docx Document A.docx (182.5 KB, 16 views)
File Type: docx Document B.docx (43.3 KB, 16 views)
Reply With Quote
  #4  
Old 06-10-2014, 11:19 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Unless there is always a fixed relationship between the tables in the two documents, this would be a difficult task.

Although you say "Cells in the two documents are similar but formatted exactly the same", that's not really the case. For example, whereas your B document has a separate cell for headings such as 'LEARNING ACTIVITIES', your A document has those headings in the same cells as the content to which they relate. Consequently, even if there is a fixed relationship between the documents, one can't simply transfer entire cell contents - some editing would also be required.

If there isn't a fixed relationship, the difficulties associated with automatically transferring the data increase substantially.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-11-2014, 05:43 AM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

It really wouldn't be a problem to repeat those cells headings if it meant a more expedient method of turning Doc A into Doc B. The manual cutting and pasting that I'm doing is going to take longer than I have to complete this project.
Reply With Quote
  #6  
Old 06-11-2014, 05:51 AM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by mcb09 View Post
It really wouldn't be a problem to repeat those cells headings if it meant a more expedient method of turning Doc A into Doc B.
That is much less of an issue than being able to determine which content in Doc A goes into which cells in Doc B.

You also haven't said whether there is always a fixed relationship between the tables in the two documents (eg row 5, column 3 of table 2 in Doc A always does into Row 7 Column 1 of table 1 in Doc B). If there isn't a fixed relationship of this kind, there will be significant difficulties in transferring the data.

From what I've seen, reformatting Doc A wouldn't be any easier.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-11-2014, 12:09 PM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

The fixed relationship between the docs only relates to certain cells. Here's the mapping.


ROW 4, COLUMN 1 of TABLE 2 in Doc A → ROW 10, COLUMN 1 of TABLE 1 in Doc B
ROW 4, COLUMN 2 of TABLE 2 in Doc A → ROW 10, COLUMN 2 of TABLE 1 in Doc B
ROW 5, COLUMN 1 of TABLE 2 in Doc A → ROW 12, COLUMN 1 of TABLE 1 in Doc B
ROW 5, COLUMN 2 of TABLE 2 in Doc A → ROW 12, COLUMN 2 of TABLE 1 in Doc B
ROW 6, COLUMN 1 of TABLE 2 in Doc A → ROW 14, COLUMN 1 of TABLE 1 in Doc B
ROW 6, COLUMN 2 of TABLE 2 in Doc A → ROW 14, COLUMN 2 of TABLE 1 in Doc B
ROW 8, COLUMN 1 of TABLE 2 in Doc A → ROW 17, COLUMN 1 of TABLE 1 in Doc B
ROW 8, COLUMN 2 of TABLE 2 in Doc A → ROW 17, COLUMN 2 of TABLE 1 in Doc B
ROW 10, COLUMN 1 of TABLE 2 in Doc A → ROW 20, COLUMN 1 of TABLE 1 in Doc B
ROW 10, COLUMN 2 of TABLE 2 in Doc A → ROW 20, COLUMN 2 of TABLE 1 in Doc B
ROW 11, COLUMN 1 of TABLE 2 in Doc A → ROW 22, COLUMN 1 of TABLE 1 in Doc B
ROW 11, COLUMN 2 of TABLE 2 in Doc A → ROW 22, COLUMN 2 of TABLE 1 in Doc B
Reply With Quote
  #8  
Old 06-11-2014, 05:32 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following macro. Add it to your destination document (or to Word's 'Normal' template and activate your destination document), then run it. Select the source document and the destination document will be updated.

Note: I had to change your source row references, plus some destination column references, and you seemed to have too many cells overall. I've also managed to exclude the source table headings.
Code:
Sub XferTblData()
Application.ScreenUpdating = False
Dim DocSrc As Document, TblSrc As Table, RngSrc As Range, RowSrc As String, ColSrc As String
Dim DocTgt As Document, TblTgt As Table, RngTgt As Range, RowTgt As String, ColTgt As String
Dim i As Long
RowSrc = "5,5,6,6,7,7,9,9,11,11"
ColSrc = "1,2,1,2,1,2,1,2,1,2"
RowTgt = "10,10,12,12,14,14,17,17,20,20"
ColTgt = "1,2,1,2,1,2,1,2,2,3"
Set DocTgt = ActiveDocument
With Application.Dialogs(wdDialogFileOpen)
  If .Show = -1 Then
    .AddToMru = False
    .ReadOnly = True
    .Visible = False
    .Update
    Set DocSrc = ActiveDocument
  End If
End With
If DocSrc Is Nothing Then Exit Sub
Set TblSrc = DocSrc.Tables(2)
Set TblTgt = DocTgt.Tables(1)
For i = 0 To UBound(Split(RowSrc, ","))
  Set RngSrc = TblSrc.Cell(Split(RowSrc, ",")(i), Split(ColSrc, ",")(i)).Range
  RngSrc.End = RngSrc.End - 1
  RngSrc.Start = RngSrc.Paragraphs(1).Range.End
  Set RngTgt = TblTgt.Cell(Split(RowTgt, ",")(i), Split(ColTgt, ",")(i)).Range
  RngTgt.End = RngTgt.End - 1
  RngTgt.FormattedText = RngSrc.FormattedText
Next
DocSrc.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 06-11-2014, 09:12 PM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

This is AMAZING, Paul! Thank you! I do get the below message. I click and END and it completes the macro. Row 10, Col 2 of the target doc doesn't get populated with content (Row 20, Col 2 of the source).
Attached Images
File Type: png Capture.PNG (19.5 KB, 44 views)
Reply With Quote
  #10  
Old 06-11-2014, 09:23 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Using the documents attached to post #3, the macro runs to completion with no errors for me. The error message suggests a referenced cell in one of the documents you're working with doesn't exist.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 06-11-2014, 10:39 PM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

I see my error. I tried to add the Tier 2 and Tier 3 cells at the bottom of Doc B. They exist in Doc A, but I never created them in Doc B. I tried to edit your code to accommodate for the addition, but that didn't work. Word kept getting hung and crashing.

Below is the Doc A content that I trying to move as well. How would you alter your code?
Attached Images
File Type: png Capture4.PNG (25.1 KB, 44 views)
File Type: png Capture2.PNG (9.2 KB, 44 views)
Reply With Quote
  #12  
Old 06-11-2014, 10:44 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Use:
Code:
RowSrc = "5,5,6,6,7,7,9,9,11,11,12,12"
ColSrc = "1,2,1,2,1,2,1,2,1,2,1,2"
RowTgt = "10,10,12,12,14,14,17,17,20,20,22,22"
ColTgt = "1,2,1,2,1,2,1,2,2,3,1,2"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 06-12-2014, 06:40 AM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

That worked!! Thank you so much. This is going to save me so much time and sweat.

In case you have another miracle to perform, some of the source content for Doc A comes from an Excel sheet. The content there is in HTML markup. If you're feeling up another challenge, is their way to create a macro that moves the data from those cells in sheet to corresponding cells in Doc B while converting the HTML to readable text?
Reply With Quote
  #14  
Old 06-12-2014, 05:19 PM
macropod's Avatar
macropod macropod is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by mcb09 View Post
some of the source content for Doc A comes from an Excel sheet. The content there is in HTML markup. If you're feeling up another challenge, is their way to create a macro that moves the data from those cells in sheet to corresponding cells in Doc B while converting the HTML to readable text?
Possibly - even probably. However, that would require knowing a lot about the Excel data source and how a given Doc A relates to those data, so that the correct set from the Excel data source could be imported into Doc B. Not something I have time to tackle right now.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 06-12-2014, 09:33 PM
mcb09 mcb09 is offline Copy table content between documents Windows 7 32bit Copy table content between documents Office 2010 32bit
Novice
Copy table content between documents
 
Join Date: Jun 2014
Posts: 8
mcb09 is on a distinguished road
Default

I understand. Thanks again for your help. It's making all the difference.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy content control entries to next table next page Mel_Herndon Word VBA 2 05-22-2014 05:07 PM
Copy table content between documents Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM
Copy table content between documents Content of all documents in folder changes to match one of them jemmac2525 Word 2 11-11-2013 12:32 AM
table of content suna Word 2 05-16-2013 02:39 PM
Copy table content between documents share content between documents? albytrott Word 1 10-08-2009 08:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:58 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft