Excel to Word Copy paste Data using bookmarks by VBA

mohichem

New Member
Joined
Dec 18, 2018
Messages
3
Below Code is working only for one cell,I want to change this to "If a particular cell is double-clicked automatically transfer the entire row data into the corresponding word bookmarks"

Sub test()
Dim objWord As Object
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

objWord.Documents.Open "C:\test.docx" ' change as required

With objWord.ActiveDocument
.Bookmarks("Text1").Range.Text = ws.Range("A1").Value
.Bookmarks("Text2").Range.Text = ws.Range("A2").Value
.Bookmarks("Text3").Range.Text = ws.Range("A3").Value
End With

Set objWord = Nothing

End Sub


I was tried many combinations it was not working,Please help me .........
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim objWord As Object
    If Not Application.Intersect(Target, Me.Range("A1:A3")) Is Nothing Then
        Cancel = True
    End If

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True

    objWord.Documents.Open "C:\Users\davem\OneDrive\Excel Examples\Test.docx" ' change as required

    With objWord.ActiveDocument
        .Bookmarks("Text1").Range.Text = Target.Value
        .Bookmarks("Text2").Range.Text = Target.Offset(, 1).Value
        .Bookmarks("Text3").Range.Text = Target.Offset(, 2).Value
    End With

    Set objWord = Nothing

End Sub

This belongs in the worksheet module

1599903092381.png
 
Upvote 0
Thanks for the help,it is very useful me.(y)(y)(y)(y)(y)(y)

I would like to learn VBA, Could you please suggest any Youtube video channel?
 
Upvote 0

Forum statistics

Threads
1,215,530
Messages
6,125,347
Members
449,220
Latest member
Edwin_SVRZ

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