Insert Excel cell text in Word after bookmark via VBA - excel

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

Related

VBA Copy long text from multiple Words document and paste into a single Excel cell

My goal is to copy a multiline formatted text from a cicle of Word documents and paste it to an Excel worksheet into one single cell with in the first column the title of the document and in the second column the entire document using a VBA macro.
Now I've got a multiline text which needs more cells and get overrided.
This is my current code:
Sub Excel_Word()
Dim WordApp As Object 'New Word.Application
Dim objDoc As Object ' New Word.Document
Dim Range As Object 'Word.Range
Dim WordDoc As String
Dim sPath As String
Dim i As Long
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
sPath = "C:\Users\gabri\Desktop\Test\"
WordDoc = Dir(sPath & "*.docx")
Do While WordDoc <> ""
Set objDoc = WordApp.Documents.Open(sPath & WordDoc)
objDoc.Range.Copy
i = i + 1
Foglio1.Cells(i, 1) = objDoc.Name
Foglio1.Cells(i, 2).PasteSpecial xlPasteValues
WordDoc = Dir()
Loop
WordApp.Quit
'elimina variabili
Set WordApp = Nothing
Set objDoc = Nothing
End Sub
Any ideas how I can handle this or any input?

This Code extracts a data range Into a word document but doesn't stop doing so at the right time. How do I use a range var for a dynamic range?

I would like this script to only output the correct number of rows to the document. Also, I would like it to generate a PDF instead of a Word Document, but I am unable to make this change.
This line has errors when incorporating a dynamic range:
'Dim rows As Integer
'Sheets("Terminal Unit Output").Range("M1").Value2 = rows
Sheets("Terminal Unit Output").Range("A1:O" & rows).CopyPicture Appearance:=xlScreen, Format:=xlPicture
I don't know how to stop the excess Word Document outputs here:
Excess Word Document outputs
there's a bunch of blank items at the bottom of my dropdown list I don't know how to remove.
To reset the sheet back to the first item at the end of the loop (AHU-1) I've tried:
Set dvCell = inputRange(1)
editable xlsm
Option Explicit
Sub GenerateReports()
Dim objWord As Object
Dim objDoc As Object
Dim dvCell As Range
Dim inputRange As Range
Dim c As Range
Dim i As Long
ActiveWindow.View = xlNormalView
Set dvCell = Sheets("General_Input").Range("B16")
Set inputRange = Evaluate(dvCell.Validation.Formula1)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
For Each c In inputRange
dvCell.Value = c.Value
Sheets("Terminal Unit Output").Range("A1:O75").CopyPicture Appearance:=xlScreen, Format:=xlPicture
objWord.Selection.Paste
objWord.Selection.TypeParagraph
Next c
Set objWord = Nothing
Set objDoc = Nothing
Set dvCell = Nothing
Set inputRange = Nothing
End Sub

Using excel to add a table in Word document

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")

Can excel data be pushed into a existing word document at a specific place

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

Vba code to copy image from cell into word document

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?

Resources