Consulting

Results 1 to 6 of 6

Thread: Paste selection from excel to word in specific paragraph

  1. #1
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    2
    Location

    Paste selection from excel to word in specific paragraph

    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
    Last edited by Aussiebear; 01-04-2025 at 12:18 PM.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,381
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    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.
    Last edited by Aussiebear; 02-07-2025 at 03:17 AM.

  4. #4
    Banned VBAX Newbie
    Joined
    Jun 2024
    Posts
    2
    Location
    Quote Originally Posted by Debbie KLu View Post
    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.

  5. #5
    Quote Originally Posted by batonring View Post
    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
    Did you forget that excel to word also needs to be compatible with the machine?

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,381
    Location
    @ SaulMuller, batonring's post was a spamming event. Pay no attention to it.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •