I'm copying a range of cells from Excel as a picture to a Word document. It pastes at the beginning of the document.
How could I paste in a specific area? The area could be denoted by some text that I'd later find/replace.
Range("A1:H5").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("MyFile.docx")
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.Paste
End Sub
I just came accross the same problem and used the following code. I use a bookmark called "here" which is saved in my Word document. HTH, Mitch.
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("MyFile.docx")
Range("A1:H5").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("here").Select
Set objSelection = WordApp.Selection
objSelection.Paste
Related
I'm writing a macro to paste a bunch of tables and charts into a Word doc to be uploaded to our underwriters' system. I've tried to do a small sample and keep getting an error that the Word Application can't be activated.
Sub PasteToWord()
Dim wordapp As Word.Appliation
Dim worddoc As Word.Document
Dim xlrng As Range
Set wordapp = New Word.Application
wordapp.Activate
wordapp.Visible = True
Set worddoc = wordapp.Documents.Add
Set xlrng = ActiveSheet.Range("A1:A10")
xlrng.copy
worddoc.Paragraphs(1).Range.PasteExcelTable False, True, False
End Sub
You need to display the application before activating anything:
Sub PasteToWord()
Dim wordapp As Word.Appliation
Dim worddoc As Word.Document
Dim xlrng As Range
Set wordapp = New Word.Application
wordapp.Visible = True
wordapp.Activate
Set worddoc = wordapp.Documents.Add
Set xlrng = ActiveSheet.Range("A1:A10")
xlrng.copy
worddoc.Paragraphs(1).Range.PasteExcelTable False, True, False
End Sub
I'm creating a tool in Excel
Which is going to read in some data and the create a word document based on that data.
So far I've got excel to create the word document and add a few lines of text without any issue.
The next bit though to add a table is causing issues.
I can add the table in fine, but for some reason it deletes the lines of text that I added in the first place.
This is my code:
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSelection As Object
Dim objRange As Object
Dim objTable As Object
Dim ctr as long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
Set objSelection = objWord.Selection
Set objRange = objDoc.Range
'Adding some heading Text
objSelection.Style = objDoc.Styles("Heading 1")
objSelection.Font.Bold = True
objSelection.TypeText ("Heading Text")
objSelection.TypeParagraph
'Adding some normal Text
objSelection.Style = objDoc.Styles("Normal")
objSelection.Font.Bold = False
objSelection.TypeText ("Normal Text")
objSelection.TypeParagraph
Stop
'Adding the table
objDoc.Tables.Add objRange, 10, 2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I put in a stop points after my heading and normal text are added and they appear in the word document fine.(screenshot below)
But as soon as the code reaches the Tables.Add bit, all my text disappears and the document has nothing but the table. (also screenshot below)
I looked around online and tried putting
objSelection.Collapse WdCollapseDirection.wdCollapseEnd
before the Tables.Add line of code, but that didn't help.
Your code to add a table fails because you are adding the table into objRange which you defined as the entire document.
You should also get into the habit of avoiding use of the Selection object, both in Word and Excel. Not only is it ineffecient (the screen has to be redrawn constantly) it is also error prone as the selection could be changed by the user to something you're not expecting.
The code below should work for you.
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objRange As Word.Range
Dim objTable As Word.Table
Dim ctr As Long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
'Adding some heading Text
With objDoc.Paragraphs(1).Range
.Style = objDoc.Styles(wdStyleHeading1)
.Font.Bold = True
.Text = "Heading Text"
.InsertParagraphAfter
End With
'Adding some normal Text
With objDoc.Paragraphs(2).Range
.Style = objDoc.Styles(wdStyleNormal)
.Font.Bold = False
.Text = "Normal Text"
.InsertParagraphAfter
End With
Set objRange = objDoc.Paragraphs.Last.Range
'Adding the table
Set objTable = objDoc.Tables.Add(objRange, 10, 2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I did the test with the code below and it works :
Pre requisite : add reference "Microsoft Word xx.x Object Library" in your VBA project
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
' create an instance of MS Word
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
Range("A1:A2").Copy
WordApp.Selection.TypeText ("Here are my comment")
WordApp.Selection.Paste
' fit the table with window
WordDoc.Tables(1).AutoFitBehavior wdAutoFitWindow
' Save the content into the .doc file
WordDoc.SaveAs2 ("C:\mypath\myDocument.doc")
I am new in VBA and would like to asking some help.
I have a list of word document in excel in range B3:B40. I would like to copy the document in the list and paste to a new document without changing the page format.
I already tried the code below, it give me "run time error 13". Can anybody help with this situation?
Thanks in advance for any help.
Application.ScreenUpdating=false
set objword = createobject("Word.Application")
set objdoc = objword.Documents.Add
objword.visible = true
set objselection = objword.Selection
Folderpath = "C:\desktop" 'where I save the word document that would be combined
set objtempword = createobject("Word.Application")
set tempdoc = objword.documents.open (Folderpath & "\" & Sheet1.Range ("B3:B40")
set objtempselection = objtempword.selection
tempdoc.range.select
tempdoc.range.copy
objselection.typeparagraph
objselection.paste
tempdoc.close
I think this could work for you. What was missing is a cycle to work for each file (cell in the range).
Option Explicit
Sub JoinDocs()
Application.ScreenUpdating = False
Dim objword As Object, objdoc As Object, objselection As Object
Set objword = CreateObject("Word.Application")
Set objdoc = objword.Documents.Add
objword.Visible = True
Dim Folderpath As String
Set objselection = objword.Selection
Folderpath = "C:\desktop\" 'where I save the word document that would be combined
Dim vDoc As Variant
Dim objtempword As Object, tempdoc As Object, objtempselection As Object
Set objtempword = CreateObject("Word.Application")
For Each vDoc In Sheet1.Range("B3:B40").Value
Set tempdoc = objword.Documents.Open(Folderpath & vDoc)
Set objtempselection = objtempword.Selection
tempdoc.Range.Select
tempdoc.Range.Copy
objselection.TypeParagraph
objselection.Paste
tempdoc.Close
Next vDoc
End Sub
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
In EXCEL, I have some VBA codes to open a Word Document A and copy its content from certain page to a new document. Currently, I can copy its text. I am wondering how to copy both context and formatting. Below is my current code and I appreciate any suggestions!
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
'Prepare Document B
objDoc.SaveAs (Folderpath to Document B)
Set objTempWord = CreateObject("Word.Application")
Set tempDoc = objWord.Documents.Open(Folderpath to Document A)
'copy context from Document A
With tempDoc.Application
.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:="2"
.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
.Selection.Copy
End With
objSelection.TypeParagraph
objSelection.Paste
objSelection.InsertBreak Type:=wdSectionBreakNextPage
tempDoc.Close
objDoc.Application.Statusbar = False
objDoc.Save
This here does the same, without the superfluous extra Application object and without the use of Selection:
Dim objWord As Word.Application
Dim objDoc As Word.Document, newDoc As Word.Document
Dim r As Word.Range, r2 As Word.Range
Set objWord = CreateObject("Word.Application") 'or Set objWord = new Word.Application
Set objDoc = objWord.Documents.Open(FolderpathToDocumentA)
Set newDoc = objWord.Documents.Add
newDoc.SaveAs FolderpathToDocumentB
Set r = objDoc.GoTo(what:=wdGoToPage, which:=wdGoToAbsolute, Name:=2)
r.End = objDoc.Range.End
'copy context from Document A
r.Copy
newDoc.Content.InsertBreak Type:=wdSectionBreakNextPage
newDoc.Range(newDoc.Content.Start, newDoc.Content.Start).Paste
newDoc.Content.InsertBefore vbCrLf
newDoc.Save
objWord.Quit
Does that do what you need?