Copy cell formatting to embedded object

stewart1

Board Regular
Joined
Feb 25, 2010
Messages
66
Evening to all.

I've embedded a word document which I managed to cobble code together to open.

VBA Code:
Sub Macro4()
'
' Macro4 Macro
'

'

Dim wDoc As Object 'Word.Document
    With ActiveSheet
    ActiveCell.Copy
  Set o = .OLEObjects("Object 5")
  o.Verb xlVerbOpen
  ActiveSheet.Shapes.Range(Array("Object 5")).Select
    Selection.Verb Verb:=xlPrimary
    
 End With
End Sub

I'm a bit stuck on copying the cell contents to it via vba. Keeping all the cell formatting. Once the word doc opens, no more code is recorded.

Can anyone give me any ideas please?

As always really appreciated you taking the time to read.

Stu
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,466
Try to adapt the following code...

VBA Code:
Option Explicit

Sub test()

    Dim targetWorksheet As Worksheet
    Set targetWorksheet = ActiveSheet
    
    Dim targetCell As Range
    Set targetCell = ActiveCell

    Dim embeddedObject As OLEObject
    Set embeddedObject = targetWorksheet.OLEObjects("Object 5")
    embeddedObject.Verb Verb:=xlVerbPrimary
    
    Dim wordDocument As Object
    Set wordDocument = embeddedObject.Object
    
    targetCell.Copy
    With wordDocument
        With .Parent
            .Selection.EndKey Unit:=6 'wdStory
            .Selection.TypeParagraph
            .Selection.TypeParagraph
            .Selection.PasteAndFormat 16 'wdFormatOriginalFormatting
        End With
    End With
    
    Application.CutCopyMode = False
    
    Set wordDocument = Nothing
    Set embeddedObject = Nothing
    Set targetWorksheet = Nothing
    
End Sub

Hope this helps!
 

stewart1

Board Regular
Joined
Feb 25, 2010
Messages
66
You Domenic are an absolute legend! All last night I was trying to do this.... And tonight I will work on sending it back to the active cell with formatting.....

Honestly, really appreciate it Domenic!🤝👍
 

Watch MrExcel Video

Forum statistics

Threads
1,127,725
Messages
5,626,515
Members
416,187
Latest member
L_D18

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
Top