Consulting

Results 1 to 3 of 3

Thread: Solved: Word Template using data from a text delimited file using VBA

  1. #1

    Solved: Word Template using data from a text delimited file using VBA

    I need to be able to create a template so that when the template is run it will get data from the text delimited file and put it into the template in the correct text form field. The delimited file contains only 1 record set and would only fill in a portion of the letter created. The rest would be filled in using forms which would prompt the user for information or to make choices and those choices would fill in the letter accordingly. I need some help in how to get the information from the delimited text file into the letter using VBA. Doing a merge did not work because in running the merge all the text form fields and book marks are removed in the resulting document.

    So if the delimited text file contains the following fields: FName, LName, City, State, ZipCode. The delimitted text file would reside on a mapped drive say L:\Data.

    What would the code in VBA look like to ge the information in FName to a text form field in the template called FName?

    I have coded many templates in VBA and many are pretty complex, I am looking for a starting point to point me in the right direction.

    Any help is greatly appreciated.

    Thanks.

  2. #2
    Maybe it would be easier if I were to have the data put into a Excel file and then acquire the data from the Excel Spreadheet. I know how to copy the whole spreadsheet from excel to Word but how would you copy an individual cell to Word? Any help would be appreciated.

  3. #3
    This is the code I have from another project I had that copied the whole spreadsheet to Word.
    [VBA]Sub Reinstatement_From_Involuntary_Inactive_Enrollment()
    Dim Xl As Excel.Application, Wb As Excel.Workbook, ws As Excel.Worksheet, i As Integer
    Dim XlOpen As Boolean
    '//This code requires a reference to the Excel object library to use some Xl methods
    '//such as End(xlup) and Dim objects Xl and Wb (otherwise just declare as Object)


    On Error Resume Next
    Set Xl = GetObject(, "Excel.Application") 'Select Xl app if open
    If Err.Number <> 0 Then 'If Excel is not already open then open an instance
    Set Xl = CreateObject("Excel.Application")
    Else
    XlOpen = True 'an indicator so we know whether to close Xl app or not when finished
    End If
    On Error GoTo 0

    'Inserts the heading label in the document
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 10
    Selection.Font.Bold = True
    Selection.Font.Underline = True
    Selection.TypeText Text:="Reinstatement from Involuntary Inactive Enrollment" & vbCr & vbCr
    Selection.Font.Bold = False
    Selection.Font.Underline = False

    'Open workbook
    On Error Resume Next
    Set Wb = Xl.Workbooks.Open("F:\End\Reinstatement_From_Involuntary_Inactive_Enrollmen t_05.xml")
    Wb.Application.DisplayAlerts = False
    If Wb Is Nothing Then
    MsgBox "Unable to open file!"
    On Error GoTo 0
    GoTo CleanUp_05
    End If
    On Error GoTo 0

    With Wb.Sheets(1)
    'see if theres anything to copy
    If .Cells(1, 1) = vbNullString Then
    MsgBox "There is no data for this category!"
    GoTo CleanUp_05
    End If

    .UsedRange.Copy ' Copy contents of Sheet 1
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 10
    Selection.TypeText Text:="The following persons, originally enrolled as inactive members pursuant to "
    Selection.Font.Bold = True
    Selection.TypeText Text:="Rule 9.21 "
    Selection.Font.Bold = False
    Selection.TypeText Text:="of the California Rules of Court, "
    Selection.TypeText Text:="were reinstated on the dates set forth below upon completion of all requirements:" & vbCr & vbCr
    Selection.Paste 'Paste sheet 1 into document at cursor
    Selection.TypeText Text:=vbCr & vbCr
    End With
    Wb.Application.DisplayAlerts = False
    WordBasic.EditOfficeClipboard
    CommandBars("Office Clipboard").Visible = False

    CleanUp_05:
    'If XlOpen = False Then Xl.Quit 'close xl if we started it otherwise leave open
    'Set Xl = Nothing
    'Set Wb = Nothing
    Wb.Close[/VBA]

    How would I cahnge this code so that I could copy a single cell to a text Form Field

    [VBA]ActiveDocument.FormFields("FName").Result = (the content of Cell A2 of the Excel spreadsheet)[/VBA]

Posting Permissions

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