Consulting

Results 1 to 2 of 2

Thread: Need to edit VB code to transfer Word form data to Excel

  1. #1

    Need to edit VB code to transfer Word form data to Excel

    Hi,

    I've inherited some VB code that takes data from a word form and puts it in Excel. Unfortunately the code uses form fields and I want to be able to use content controls. Can someone tell me how to edit the code to use Content Controls?

    The code I have is below

    Dim vField As FormField
    Dim fso As Scripting.FileSystemObject
    Dim fsDir As Scripting.Folder
    Dim fsFile As Scripting.File
    Dim wdApp As Word.Application
    Dim myDoc As Word.Document
    Dim vColumn As Integer
    Dim vLastRow As Integer
    Dim x As Integer

    Sub AddFormFields()

    vLastRow = ActiveSheet.UsedRange.Rows.Count + 1
    vColumn = 1

    Set fso = New Scripting.FileSystemObject

    Set fsDir = fso.GetFolder _
    ("\\MEL19A001PPN\uam2a$\Evaluation\Evaluation Auto Form\DS Template\Unprocessed\")

    Set wdApp = New Word.Application
    wdApp.Visible = True
    For Each fsFile In fsDir.Files
    wdApp.Documents.Open (fsFile)
    Set myDoc = wdApp.ActiveDocument
    For Each vField In wdApp.Documents(myDoc).FormFields
    vField.Select
    vValue = vField.Result
    Workbooks("TemplateDS.xls").Activate

    Cells(vLastRow, vColumn).Select
    If vField.Type = 71 Then
    Select Case vField.Name
    Case "Check1"
    vColumn = vColumn - 1
    If vField.Result = "1" Then
    ActiveCell.Value = "YES"
    End If
    Case "Check2"
    If vField.Result = "1" Then
    ActiveCell.Value = "NO"
    End If
    End Select
    Else
    ActiveCell.Value = vValue
    End If
    vColumn = vColumn + 1
    Next
    vColumn = 1
    vLastRow = vLastRow + 1
    vFileName = wdApp.ActiveDocument.Name
    wdApp.ActiveDocument.Close
    Name fsFile As _
    "\\MEL19A001PPN\uam2a$\Evaluation\Evaluation Auto Form\DS Template\Processed\" & vFileName

    Next
    wdApp.Quit
    MsgBox "You have now uploaded unprocessed files."
    End Sub


    Thanks
    Last edited by nikki2503; 05-03-2017 at 07:17 PM.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    See, for example: http://www.vbaexpress.com/forum/show...l=1#post257696
    Although the code there is written around the extraction of data from text-containing content controls (checkbox content controls are only supported from Word 2010 onwards), and checkbox content controls would return TRUE/FALSE instead of 1/0 or Yes/No, the code could easily be modified to do that, too.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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