Log in

View Full Version : [SLEEPER:] Paste selection from excel to word in specific paragraph



Naziya
11-29-2016, 03:13 AM
Hi, anyone please help me solve this, the code below is selecting the fields to be pasted in word, but its creating a new word file and pasting the selection.
I need anyone to help me quickly with pasting the selection in the word file i choose and in paragraph 5. the word file already has content, this code is just to fit the table from excel to word. please help


Sub Test()
Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
Sheets("Sheet1").Select
With ActiveSheet
.AutoFilterMode = False
.Range("A1:I1").AutoFilter
.Range("A1:I1").AutoFilter Field:=1, Criteria1:=InputBox("Please provide Country name")
End With
With ActiveSheet.AutoFilter.Range
.Offset(1, 0).Resize(.Rows.Count - 1).Copy
End With
' Set tbl = ThisWorkbook.Worksheets(Sheet1.Name).ListObjects("Table1").Range
Application.GetOpenFilename ("Word Files (*.doc*), *.doc*")
WordApp.Visible = True
WordApp.Activate
' tbl.Copy
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
EndRoutine:
' Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
' Clear The Clipboard
Application.CutCopyMode = False
' Opening the word document
' Set wordapp = CreateObject("word.Application")
' wordapp.documents.Open
' wordapp.Visible = True
:crying:

Aussiebear
01-04-2025, 12:26 PM
Maybe try the following



Sub CopyExcelDataToWord()
' Declare variables
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rng As Excel.Range
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
' Create Excel Application object
Set xlApp = New Excel.Application xlApp.Visible = False
' Hide Excel application
' Open the Excel workbook
Set xlWB = xlApp.Workbooks.Open("C:\Path\To\Your\Excel\File.xlsx")
' Replace with the actual path to your Excel file
' Select the worksheet and range Set xlSheet = xlWB.Sheets("Sheet1")
' Replace "Sheet1" with the actual sheet name
Set rng = xlSheet.Range("A1:B10")
' Replace with the actual range
' Create Word Application object
Set wdApp = New Word.Application wdApp.Visible = True
' Show Word application
' Open or create the Word document
Set wdDoc = wdApp.Documents.Open("C:\Path\To\Your\Word\Document.docx")
' Replace with the actual path to your Word document
' If the document doesn't exist, it will be created
' Find the specific paragraph
Set wdRng = wdDoc.Paragraphs(1).Range ' Replace 1 with the paragraph number
' Paste Excel data into Word
wdRng.PasteSpecial DataType:=wdPasteExcelTable
' Close and quit Excel xlWB.Close False xlApp.Quit
' Save and close Word
wdDoc.Save wdDoc.Close
' Quit Word wdApp.Quit
' Clean up objects
Set rng = Nothing
Set xlSheet = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
Set wdRng = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub

Debbie KLu
02-06-2025, 08:24 PM
Code snippets are provided for those who want to automate this process, although they may need to be adapted for specific requirements. To paste the selection into the fifth paragraph of an existing Word document, you can modify the code to set the range to wdDoc.Paragraphs(5).Range before pasting the data. Make sure the Word document is open and accessible in your code.

batonring
03-05-2025, 07:28 PM
Code snippets are provided for those who want to automate this process, although they may need to be adapted for specific requirements. To paste the selection into the fifth paragraph of an existing Word document, you can modify the code to set the range to wdDoc.Paragraphs(5).Range before pasting the data. Make sure the Word document is open and accessible in your code.

If you paste the code below into word, it will be an error, so you need to change it to wdDoc.Paragraphs(5). Then paste this code only to match the table from excel to word.

SaulMuller
03-25-2025, 01:52 AM
If you paste the code below into word, it will be an error, so you need to change it to wdDoc.Paragraphs(5). Then paste this code only to match the table from excel to word.Block Blast (https://blockblast.org)
Did you forget that excel to word also needs to be compatible with the machine?

Aussiebear
03-25-2025, 02:07 AM
@ SaulMuller, batonring's post was a spamming event. Pay no attention to it.