Transferring Information - EXCEL > WORD

Discussion in 'Visual Basic ( VB )' started by IcePirate, Dec 3, 2008.

Thread Status:
Not open for further replies.
  1. IcePirate

    IcePirate New Member

    Joined:
    Dec 3, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    (Code is below)

    Hello, I have a script that will open a word document based on the data I put in row 1 in excel up to column "E"

    Then in "Column F" there is a button that says "Create document" and iwhen you click it, the button triggers Word to open; and create a word document based on row1 of whats in your Excel document

    My problem is what about rows 2, 3, 4, and so on and so on...I want that "Create document" button to be listed on every rows in column "F" and when the button is clicked the script to build a word document based on only the information in that row the button is clicked for
    IE:
    <Tim> <26> <Coral St. > "<Create Document>"
    <Jim> <Knows> <Red-Deer Rd.> "<Create Document>"

    My code is below...I cant seem to determine what I should add to the script to have a command button on every row and how to only get it to trigger and make a word document for the information only on that specific row (any help is appreciated!!)

    Code:
    Option Explicit
    Public Sub TransferData()
    'This macro transfers the data range "A1:E1" to a table in Word
    '
    'Constants:
    'docFullName = The full name of an already existing Word document
    '
    'Variables:
    'doc = The Word document (assumed to be empty)
    'i = A counter (for rows)
    'j = A counter (for columns)
    'tbl = A Word table
    'wdRng = A Word range (the first paragraph of doc)
    'wks = The worksheet "data" that contains the data range
    '
    'Const docFullName = "C:\OLE Automation\Word.doc" '
    Dim doc As Object
    Dim i As Long
    Dim j As Long
    Dim tbl As Object
    Dim wdApp As Object 'Only if you require a new document each time
    Dim wdRng As Object
    Dim wks As Worksheet
    
    'Assing Word objects 'Only if you require a new document each time
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    Set doc = wdApp.Documents.Add
    
    'Assign variables and objects
    'Set doc = GetObject(docFullName) 'Only if you want a specific document
    Set wdRng = doc.Paragraphs(1).Range
    Set tbl = doc.Tables.Add(wdRng, 11, 5)
    Set wks = ThisWorkbook.Worksheets("Transmittal")
    
    'Transfer the data
    With tbl
    For i = 1 To 1
    For j = 1 To 5
    .Cell(i, j) = wks.Cells(i, j)
    Next j
    Next i
    End With
    
    'Save and close doc 'Only if you want a specific document
    'Call doc.Save
    'Call doc.Close(False)
    
    'Clean
    Set doc = Nothing
    Set wks = Nothing
    
    End Sub
    
    Private Sub CommandButton2_Click()
    
    End Sub 
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice