Copy from an Embedded Word Document to a Single Cell

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
I have two embedded Word documents in one of my sheets that allow users to edit I can copy the contents and paste them in a sheet, but the carriage returns break the contents up into multiple cells. As my algorithm is intended to house an entire report on a single row, this is causing an issue.

For instance, the embedded Word document contains the following:

this is line 1
this is line 2

I want the datasheet to copy the object's values into a single cell, such that it would appear as:

this is line1this is line 2

https://answers.microsoft.com/en-us...a/ee2b3cce-3171-4b58-9b08-75606ac6e5be?auth=1

The code that I have implemented is from the page above and was slightly adapted to my needs:

'ws is used to loop through each worksheet; ar is used for my datasheet

x = 98 'column value
For Each Oo In ws.OLEObjects
If InStr(1, Oo.progID, "Word.Document", vbTextCompare) > 0 Then
'Open the embedded document
Oo.Verb xlVerbPrimary

Set wDoc = Oo.Object

'Copy the contents to cell A1
wDoc.Content.Copy
ar.Cells(rowF, x).PasteSpecial xlPasteValues
x = x + 1

'Select any cell to close the document
Range("A1").Select
'Done
End If
Next
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Instead of doing a copy/pastespecial, assign the Word contents to a string variable, replace vbCr with either vbCrLf to get line breaks within the cell itself, or replace it with nothing ("") or space (" "). Then write the string to your cell.

Code:
[FONT=Courier New][COLOR=green]'Copy the contents to cell A1[/COLOR]
strContent = wDoc.Content
strContent = Replace(strContent, vbCr, vbCrLf) [COLOR=#008000]'or replace vbCrLf with "" or " "[/COLOR]
ar.Cells(rowF, x).Value = strContent
x = x + 1[/FONT]


Also, you can add the following declaration...

Rich (BB code):
Dim strContent As String

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top