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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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!
 
Upvote 0
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!??
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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