Results 1 to 14 of 14

Thread: need vba code to export data from excel to word file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    i had tried with my code but it not copy as each 2 segment into one page all range copy in one page i want each 2rows date in one page
    i want set location path on code to save word on particular path file
    help me on this

    Sub run()
      Dim wdDoc As Word.document, wdTbl As Word.Table, wdApp As Object, wdRng As Word.Range
      Dim iRow As Integer, aSheet As Worksheet, iCol As Integer, iPair As Integer
      Dim sPath As String
      Set aSheet = ActiveSheet
      iRow = 2
      sPath = ActiveWorkbook.Path & "\"
      Set wdApp = CreateObject("Word.Application")
      wdApp.Visible = True
      
      Set wdDoc = wdApp.Documents.Add(sPath & "OUTPUT_RESULT-FILE.docx")  
      
      Do While aSheet.Cells(iRow, 1).Value <> ""
        Set wdRng = wdDoc.Range
        wdRng.Collapse Direction:=0
        wdRng.InsertBefore Chr(13) & Chr(13)
        wdRng.Collapse Direction:=0
        wdRng.FormattedText = wdDoc.Tables(1).Range.FormattedText
        Set wdTbl = wdRng.Tables(1)
        For iPair = 0 To 1
          For iCol = 0 To 7
            wdTbl.Cell(iCol + 1, iPair + 2).Range.Text = aSheet.Cells(iRow, 1).Offset(iPair, iCol).Value
          Next iCol
        Next iPair
        iRow = iRow + 2
      Loop
    End Sub
    pls find the error attachment

    thanks
    Attached Images Attached Images
    Last edited by jackhack33; 07-03-2022 at 06:27 AM. Reason: explaination

Tags for this Thread

Posting Permissions

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