Consulting

Results 1 to 7 of 7

Thread: Loop for copying values from excel file to fill sentences in Word

  1. #1

    Exclamation Loop for copying values from excel file to fill sentences in Word

    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

  2. #2
    Use the mailmerge method in Word.

  3. #3
    Quote Originally Posted by snb View Post
    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.

  4. #4
    Have you ever used Word or Excel ?
    Did you take a course for Word and/or Excel ?

  5. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    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

  6. #6
    Quote Originally Posted by snb View Post
    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.

  7. #7
    Quote Originally Posted by JKwan View Post
    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..

Posting Permissions

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