PDA

View Full Version : Loop for copying values from excel file to fill sentences in Word



Adarsh
01-27-2016, 01:53 AM
Hello everyone,

I am a novice to VBA and am trying to make a code in VBA, which can take 3 values from different columns in an Excel file and fill it up in a sentence in a Word file. And I wish this Loop to continue and make the same sentence and fill it up as many number of times as the number of data sets filled in the Excel file.


I would be really grateful if someone could suggest how to start and go with achieving it or give the code straightway ! Thanks in advance.


Example >

Excel file
A B C ----Columns --
1 5 9 ----Values --
2 7 3
3 8 4

Word file
Position of Point (value from column A) is X (value from column B) Y(value from column A)



>>>>>Required Output<<<<<<
Position of Point 1 is X 5 Y 9
Position of Point 2 is X 7 Y 3
Position of Point 3 is X 8 Y 4

snb
01-27-2016, 03:21 AM
Use the mailmerge method in Word.

Adarsh
01-27-2016, 04:02 AM
Use the mailmerge method in Word.


I already thought of that, Sir.

But doesnt it work for just one Iteration ?
I Need all those data and same sentence to be there in a single Word file.

snb
01-27-2016, 07:44 AM
Have you ever used Word or Excel ?
Did you take a course for Word and/or Excel ?

JKwan
01-27-2016, 08:35 AM
Give a try, you need to Reference Word in

Sub WriteToWord()
Dim wdApp As Word.Application
Dim docNew As Document
Dim lOffset As Long
Dim LastRow As Long
Dim lRow As Long
Dim WrdSel As Selection
Dim WSInput As Worksheet

Set WSInput = ThisWorkbook.Worksheets("Sheet1")
Set wdApp = New Word.Application
Set docNew = wdApp.Documents.Add
wdApp.Visible = True
LastRow = FindLastRow(WSInput, "A")
Set WrdSel = wdApp.Selection
For lRow = 1 To LastRow
WrdSel.TypeText "Position of Point " & WSInput.Cells(lRow, "A") & " is " & _
"X " & WSInput.Cells(lRow, "B") & " " & _
"Y " & WSInput.Cells(lRow, "C") & vbCrLf
Next lRow
wdApp.Activate
Set docNew = Nothing
Set wdApp = Nothing
Set WSInput = Nothing
End Sub
Function FindLastRow(ByVal WS As Worksheet, ColumnLetter As String) As Long
FindLastRow = WS.Range(ColumnLetter & Rows.Count).End(xlUp).Row
End Function

Adarsh
01-28-2016, 06:43 AM
Have you ever used Word or Excel ?
Did you take a course for Word and/or Excel ?

I have not taken any courses for Word or Excel.
I always try to learn things by myself.

The solution to the Problem using VBA is something I dont have enough time to explore and figure out myself, that's why I seeked help here.

Adarsh
01-28-2016, 06:45 AM
Give a try, you need to Reference Word in
[CODE]Sub WriteToWord()


Thank you Sir.

I will try it ASAP....
Will post any clarifications, if needed. The doubts may be stupid too..