I am running the code below from a command button in VBA in Excel.
Private Sub CommandButton1_Click()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Report")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Christopher\Desktop\EIG\ReportTemplate.docx"
With objWord.ActiveDocument
.Bookmarks("User").Range.Text =ws.Range("A1").Value
.Bookmarks("Type").Range.Text =ws.Range("A2").Value
End With
Set objWord = Nothing
End Sub
It wont copy any images from the worksheet. How would I amend the above to get the images/pictures to copy to a bookmark in the document?
Related
I need to copy a table into a existing word document
I need to paste the data into a specific place in the word document, e.g. after a bookmark
I have a code that copy and paste, but not into an existing document.
I have tried to expand / change the code, but can not figure out how to paste to the target.
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add
Set ExcRng = ActiveSheet.Range("testdata")
ExcRng.copy
WrdDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=True, RTF:=False
Application.CutCopyMode = False
End Sub
This works, and paste into a new document.
But I would like to have data pasted into this document: wordApp.Documents.Open "c:\users\peter\documents\Data skal ind her.docm"
I need to have it here:
Here is text part 1
And I would like to have my “testdata” pasted here:
Xxx
This is bookmark ”xxx”
Best regards
Peter
pg#pb.dk
I found this Word MVP doc that gives a function for updating text at a bookmark. I have added it to your example code:
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = wordApp.Documents.Open "c:\users\peter\documents\Data skal ind her.docm"
Set ExcRng = ActiveSheet.Range("testdata")
UpdateBookmark "xxx", ExcRng
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, PasteRange As Variant)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange = PasteRange
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
SOURCE: https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
Excel provides me a customized text in cell A1, which then should be passed on to a word letter at a specific section (after a bookmark).
Just started with VBA and found+edited a code that was able to provide half of the solution. Where I struggle is how to insert the text in cell A1 in sheet OutputText to the word document after the bookmark?
Here is my code so far:
Function FnBookMarkInsertAfter()
Dim objWord
Dim objDoc
Dim objRange
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\[...]")
objWord.Visible = True
Set objRange = objDoc.Bookmarks("bookmark_1").Range
objRange.InsertAfter ("Cell A1 from Sheet OutputText")
End Function
Thank you!
Would this work for you? (I can't take credit for the code):
Sub test()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\test.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("bookmark_1").Range.Text = ws.Range("A1").Value
End With
Set objWord = Nothing
End Sub
I'm trying to copy a content from excel into a bookmark in MS word. But I'm getting run time error 424. Kindly help me with it. I'm very new to Visual basics and programming as well. I have attached my code.
Thanks
Sub WordDoc()
Dim wrdApp As Object
Dim Number As String
Dim wrdDoc As Object
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("H:\IP Automation\createDoc.docx")
Number = Worksheets("Sheet1").Range("A2")
Call InsBookmark(ID, Number)
End Sub
Sub InsBookmark(strBMName, strVariable)
If strVariable <> "" Then
If ActiveDocument.Bookmarks.Exists(ID) Then
ActiveDocument.Bookmarks(ID).Select
Selection.Delete
Selection.InsertAfter (strVariable)
End If
End If
End Sub
You shouldn't seperate this into two subs, as the word doc will not persist across them so "ActiveDocument" wont work. just copy the code from the second sub into the first and replace ActiveDocument with wrdDoc
This should work for you. Give it a go and see how you get along.
Sub Export_Table_Word()
'Name of the existing Word doc.
Const stWordReport As String = "Final Report.docx"
'Word objects.
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdbmRange As Word.Range
'Excel objects.
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnReport As Range
'Initialize the Excel objects.
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("PwC Contact Information")
Set rnReport = wsSheet.Range("Table1")
'Initialize the Word objets.
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordReport)
Set wdbmRange = wdDoc.Bookmarks("Report").Range
Dim tbl As Table
For Each tbl In wdDoc.Tables
tbl.Delete
Next tbl
'If the macro has been run before, clean up any artifacts before trying to paste the table in again.
On Error Resume Next
With wdDoc.InlineShapes(1)
.Select
.Delete
End With
On Error GoTo 0
'Turn off screen updating.
Application.ScreenUpdating = False
'Copy the report to the clipboard.
rnReport.Copy
'Select the range defined by the "Report" bookmark and paste in the report from clipboard.
With wdbmRange
.Select
.Paste
End With
'Save and close the Word doc.
With wdDoc
.Save
.Close
End With
'Quit Word.
wdApp.Quit
'Null out your variables.
Set wdbmRange = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
'Clear out the clipboard, and turn screen updating back on.
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
MsgBox "The report has successfully been " & vbNewLine & _
"transferred to " & stWordReport, vbInformation
End Sub
I wrote below code to take screenshot form Excel document and paste on word document.It works fine However I am unable to convert This Word document into PDF and error displays"Object does not support this property or method".It seems that i defined Variable (Objword as variant) not correct.Please any one can help.
Sub CopyToWordPicture()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim objword As variant
Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open "C:\Automation\BH Report\Daily&BH RAN KPI report_ok.docx"
Workbooks.Open Filename:="C:\Automation\BH Report\Daily_Hourly KPI Template.xlsx"
Sheets("BH_Graphs").Select
Range("A1:k50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'WDApp.Visible = True
' WDApp.Selection.GoToNext wdGoToPage
Windows("Daily_Hourly KPI Template.xlsx").Activate
Sheets("Daily_Graphs").Select
Range("A1:J50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'export as PDF
objword.ExportAsFixedFormat OutputFileName:="C:\Automation\BH Report\Daily&BH RAN KPI report.pdf", _
ExportFormat:=wdExportFormatPDF
end sub
I need to have a vb code in ms word 2003 that copy a a specific cell in excel file and paste it in word (filed). Below is what I have done and it result in error.
Sub cmdGetNumber()
Dim XL As Object
Dim WBEx As Object
Dim ExelWS As Object
Dim appwd As Object
Dim wdApp As Word.Application
''''
'On Error GoTo OLE_ERROR
Set XL = CreateObject("Excel.Application")
Set wdApp = CreateObject("Word.Application")
'Open Excel document
Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls")
Set ExelWS = WBEx.Worksheets("Sheet1")
XL.Visible = True
'appwd.Visible = True
ExelWS.Range("c2").Select
'Selection.Copy
'wdApp.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture
'wdApp.Documents.Save
Set wdApp = Nothing
Set ExelWS = Nothing
Set WBEx = Nothing
End Sub
Since this macro is in Word, you don't need to explicitly open a word instance. You can just do Documents.Add to add a new document, or Documents.Open to open an existing one.
Try this:
Sub cmdGetNumber()
Dim XL As Object
Dim WBEx As Object
Dim ExelWS As Object
Dim wdDoc As Word.Document
'On Error GoTo OLE_ERROR
Set XL = CreateObject("Excel.Application")
'Open Excel document
Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls")
Set ExelWS = WBEx.Worksheets("Sheet1")
'XL.Visible = True
ExelWS.Range("C2").Copy
Set wdDoc = Documents.Add
wdDoc.Activate
wdDoc.Select
Selection.Paste
WBEx.Close
XL.Quit
Set WBEx = Nothing
Set ExelWS = Nothing
Set XL = Nothing
End Sub
The above code will open your excel file, copy the cell C2, then open a new word document, and paste it there.
I see you have mentioned a (filed) in your question. Did you mean a Field or a File? If it is a Field then you may want to replace Selection.Paste with the relevant field name