Hi friends,
I want to copy few cells of an excel sheet and paste special(picture) in ms-word. I want to develop a vb code code for this.
Please help!...
Regards,
Dinil
Hi friends,
I want to copy few cells of an excel sheet and paste special(picture) in ms-word. I want to develop a vb code code for this.
Please help!...
Regards,
Dinil
A few practical improvements and a working VB.NET pattern that complement 's late-binding answer and address 's bookmark request.
Use Excel's Range.CopyPicture (clean image) instead of Selection.Copy, paste directly into a Word bookmark Range with PasteSpecial, and release COM objects properly so Excel/Word processes do not stay running. Add COM references to the Microsoft Excel and Microsoft Word interop assemblies in your VB.NET project so you can use the strongly-typed enums (for example, Word.WdPasteDataType.wdPasteMetafilePicture).
' VB.NET example (add COM refs to Microsoft.Office.Interop.Excel and Microsoft.Office.Interop.Word)
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Sub PasteRangeAsPictureToBookmark(excelFile As String, sheetName As String, rangeA1 As String, wordFile As String, bookmarkName As String)
Dim xlApp As Excel.Application = Nothing
Dim xlWb As Excel.Workbook = Nothing
Dim xlWs As Excel.Worksheet = Nothing
Dim rng As Excel.Range = Nothing
Dim wdApp As Word.Application = Nothing
Dim doc As Word.Document = Nothing
Dim bmRange As Word.Range = Nothing
Try
xlApp = New Excel.Application()
xlWb = xlApp.Workbooks.Open(excelFile)
xlWs = CType(xlWb.Worksheets(sheetName), Excel.Worksheet)
rng = xlWs.Range(rangeA1)
wdApp = New Word.Application()
doc = wdApp.Documents.Open(wordFile)
rng.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture)
bmRange = doc.Bookmarks(bookmarkName).Range
bmRange.PasteSpecial(DataType:=Word.WdPasteDataType.wdPasteMetafilePicture)
doc.Save()
Finally
If doc IsNot Nothing Then doc.Close(False)
If wdApp IsNot Nothing Then wdApp.Quit()
If xlWb IsNot Nothing Then xlWb.Close(False)
If xlApp IsNot Nothing Then xlApp.Quit()
If bmRange IsNot Nothing Then Marshal.ReleaseComObject(bmRange)
If doc IsNot Nothing Then Marshal.ReleaseComObject(doc)
If wdApp IsNot Nothing Then Marshal.ReleaseComObject(wdApp)
If rng IsNot Nothing Then Marshal.ReleaseComObject(rng)
If xlWs IsNot Nothing Then Marshal.ReleaseComObject(xlWs)
If xlWb IsNot Nothing Then Marshal.ReleaseComObject(xlWb)
If xlApp IsNot Nothing Then Marshal.ReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub Troubleshooting notes:
This complements the thread example by avoiding Select/Selection, showing how to target bookmarks, and adding robust cleanup.
Jump to Post— aktharshaik 16Do u want to write this code :
a) from within an Excel file in VBA Editor?
OR
b) from within Word file in VBA Editor?
OR
c) from VISUAL BASIC IDE?
Regards
Shaik Akthar
Jump to Post— QVeen72 104Hi,
Try This Code:
Dim exel As Object Dim WBEx As Object Dim ExelWS As Object Dim appwd As Object Set exel = CreateObject("Excel.Application") Set WBEx = exel.Workbooks.Open("C:\Myexce.xls") Set ExelWS = WBEx.Worksheets("Sheet1") Set appwd = CreateObject("Word.Application") appwd.Visible = True appwd.Documents.Open FileName:="C:/doc1.doc" ExelWS.Range("A1:D20").Select Selection.Copy appwd.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture …
Do u want to write this code :
a) from within an Excel file in VBA Editor?
OR
b) from within Word file in VBA Editor?
OR
c) from VISUAL BASIC IDE?
Regards
Shaik Akthar
I want to write this code from VISUAL BASIC IDE.
Regards,
Dinil
Hi,
Try This Code:
Dim exel As Object
Dim WBEx As Object
Dim ExelWS As Object
Dim appwd As Object
Set exel = CreateObject("Excel.Application")
Set WBEx = exel.Workbooks.Open("C:\Myexce.xls")
Set ExelWS = WBEx.Worksheets("Sheet1")
Set appwd = CreateObject("Word.Application")
appwd.Visible = True
appwd.Documents.Open FileName:="C:/doc1.doc"
ExelWS.Range("A1:D20").Select
Selection.Copy
appwd.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture
appwd.Documents.Save
Set appwd = Nothing
Set ExelWS = Nothing
Set WBEx = Nothing
Set exel = Nothing Change your range selection accordingly..
Regards
Veena
Thanks a lot.....
Its done...
Regards,
Dinil
Hi Shaik,
Could you pls tell the similar code for VBA.
Thanks in advance.
Regards,
Vijay
I request to Veena also for VBA code :)
Regards,
Vijay
Hi friends
I need to a vb code under ms word that copy from a specifice cell in excell and past the value in ms word in a bookmark (field).
can you help
by the way i am using ms word 2003
many thanks,
khuraidah
Hi,
Try This Code:
Dim exel As Object Dim WBEx As Object Dim ExelWS As Object Dim appwd As Object Set exel = CreateObject("Excel.Application") Set WBEx = exel.Workbooks.Open("C:\Myexce.xls") Set ExelWS = WBEx.Worksheets("Sheet1") Set appwd = CreateObject("Word.Application") appwd.Visible = True appwd.Documents.Open FileName:="C:/doc1.doc" ExelWS.Range("A1:D20").Select Selection.Copy appwd.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture appwd.Documents.Save Set appwd = Nothing Set ExelWS = Nothing Set WBEx = Nothing Set exel = NothingChange your range selection accordingly..
Regards
Veena
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.