KEMBAR78
New Ms Wird | PDF
0% found this document useful (0 votes)
9 views3 pages

New Ms Wird

This document contains a VBA macro that opens a specified Excel workbook and a Word document, retrieves data from a specific row in the Excel sheet, and inserts that data into the Word document. It handles the opening of Word and the specified documents, iterates through the cells of the selected row, and inserts the data into the Word document with new lines. Finally, it closes the workbook and notifies the user that the data has been successfully filled into the Word document.

Uploaded by

daneben81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

New Ms Wird

This document contains a VBA macro that opens a specified Excel workbook and a Word document, retrieves data from a specific row in the Excel sheet, and inserts that data into the Word document. It handles the opening of Word and the specified documents, iterates through the cells of the selected row, and inserts the data into the Word document with new lines. Finally, it closes the workbook and notifies the user that the data has been successfully filled into the Word document.

Uploaded by

daneben81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub OpenWordAndFillDataWithoutBookmarks()

Dim wdApp As Object

Dim wdDoc As Object

Dim wb As Workbook

Dim ws As Worksheet

Dim filePath As String

Dim wordDocPath As String

Dim rowIndex As Long

Dim i As Integer

Dim textToInsert As String

' Define paths

filePath = ThisWorkbook.Path & "\WAWASE JHS_A_B-7A.xlsm" ' Path to the workbook

wordDocPath = ThisWorkbook.Path & "\SBAFORM BS7A.docx" ' Path to the Word document

' Open the target workbook

Set wb = Workbooks.Open(filePath)

Set ws = wb.Sheets("MASTERSHEET")

' Get the data row to insert (update the rowIndex if necessary)

rowIndex = 2 ' Example: Insert data from the 2nd row of the worksheet

' Start Word and open the document

On Error Resume Next

Set wdApp = GetObject(, "Word.Application")


If wdApp Is Nothing Then Set wdApp = CreateObject("Word.Application")

On Error GoTo 0

wdApp.Visible = True

Set wdDoc = wdApp.Documents.Open(wordDocPath)

' Iterate through cells in the row and add data to the Word document

With wdDoc

For i = 1 To ws.UsedRange.Columns.Count ' Loop through each column in the row

textToInsert = ws.Cells(rowIndex, i).Value ' Get the data from the cell

.Content.InsertAfter textToInsert & vbNewLine ' Insert data with a newline

Next i

End With

' Close the workbook (optional)

wb.Close False

' Notify user

MsgBox "Data from MATERSHEET has been filled into the Word document!", vbInformation

' Clean up

Set wdDoc = Nothing

Set wdApp = Nothing

Set ws = Nothing

Set wb = Nothing
End Sub

You might also like