PDA

View Full Version : [SOLVED:] Copy and Paste Between Documents Macro



tstan
03-19-2016, 04:05 PM
Hello,

I've been trying to create a macro that will help me copy and transfer text from one document to another. After selecting text in the first document (Document A), I would like the macro to copy and paste that text into the second document (Document B). Document B contains a two column table, where I would like the copied text to be pasted in the first column with the page number from which it was copied in Document A pasted in the second column. I'm not sure if this is even possible. If it's not, please let me know, and I'll stop researching how to do this.

Thanks!

Todd

gmayor
03-19-2016, 10:49 PM
This is fairly straightforward. Save and name the document with the table and put the path where indicated. The macro assumes there is already content in nthe table and adds the selection to a new row, with the page number of the last page of the selection.

Option Explicit

Sub CopySelectionToTable()
Dim oSource As Document
Dim oTarget As Document
Dim oTable As Table
Dim oRow As Row
Dim oCell As Range
Dim oRng As Range
Dim iPage As Integer
Set oSource = ActiveDocument
Set oRng = Selection.Range
iPage = oRng.Information(wdActiveEndPageNumber)
Set oTarget = Documents.Open("C:\Path\Forums\Target.docx") ' The document with the table
Set oTable = oTarget.Tables(1)
Set oRow = oTable.Rows.Add
Set oCell = oRow.Cells(1).Range
oCell.End = oCell.End - 1
oCell.FormattedText = oRng.FormattedText
Do While oCell.Characters.Last = Chr(13)
oCell.Characters.Last.Delete
Loop
Set oCell = oRow.Cells(2).Range
oCell.End = oCell.End - 1
oCell.Text = CStr(iPage)
lbl_Exit:
Set oSource = Nothing
Set oTarget = Nothing
Set oTable = Nothing
Set oCell = Nothing
Set oRng = Nothing
Exit Sub
End Sub

tstan
03-20-2016, 07:00 AM
Awesome, thank you, Graham!

tstan
03-20-2016, 12:44 PM
Graham,

Thanks again for your solution, it works great.

I have one other question relating to a different task that I perform. Is it possible to create a macro that would paste the word count of the selected text in Document A into the table in Document B? For this scenario, I need the word count instead of the selected text copied and pasted into the table along with its corresponding page number. Can this be done by using the "Words.Count" property?

Best,

Todd

gmayor
03-20-2016, 09:56 PM
I think the following macro will demonstrate the difference between words.count and the number of Words, which relates to what Word VBA consuders to eb a Word. Use UBound(sWords) as shown below for the Word count

Sub macro1()
Dim oRng As Range
Dim sWords() As String
Set oRng = Selection.Range
sWords = Split(oRng.Text, Chr(32))
MsgBox UBound(sWords) & vbCr & oRng.Words.Count
End Sub

tstan
03-21-2016, 07:26 PM
Graham,

Thank you! Is it possible to paste the word count into the table?

Best,

Todd

gmayor
03-21-2016, 10:40 PM
Add the following line to the declarations at the top of the macro
Dim sWords() As String
Assuming that the table has three columns, add the following
sWords = Split(oRng.Text, Chr(32))
Set oCell = oRow.Cells(3).Range
oCell.End = oCell.End - 1
oCell.Text = UBound(sWords)
immediately before lbl_Exit:

gmaxey
03-22-2016, 04:29 AM
Graham,

For the folks that tend to format with spaces ;-)


Sub macro1()
Dim oRng As Range
Dim sWords() As String
Set oRng = Selection.Range
sWords = Split(oRng.Text, Chr(32))
MsgBox UBound(sWords) & vbCr & oRng.Words.Count & vbCr & oRng.ComputeStatistics(wdStatisticWords)
End Sub

tstan
03-22-2016, 05:25 PM
Perfect, thank you Graham!

tstan
03-22-2016, 05:26 PM
Thank you, Greg!

gmayor
03-22-2016, 10:13 PM
Graham,
For the folks that tend to format with spaces ;-)
Good thinking, I had forgotten about oRng.ComputeStatistics(wdStatisticWords). Old age is taking its toll :(

Kurt
04-11-2016, 12:44 PM
How can you use this or something similar to copy all text, graphics and hyperlinks in Word 2007 vba?

In other words I want to copy an entire document and make sure the hyperlinks are copied without breaking.