Consulting

Results 1 to 4 of 4

Thread: Write data from content controls to txt file.

  1. #1

    Write data from content controls to txt file.

    I have a word document with a number of content controls (text fields, drop-down lists etc). I would like to write the content of all the fields, along with the filed titles, to a text file every time the word document is closed. If the file is opened and saved again I would like the contents appended to the file so that the original and additional data is recorded.

    Any ideas or guidance on how to go about this would be greatly appreciated.


    Thank you for any assistance.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Every time it's closed; or every time it's saved? Do you really want the text file updated just because someone opens & closes the file? Do you really want to ignore intermediate saves of changed content?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Hi Paul,

    Thank you for taking the time to reply, and you raise some very good points. The word document is a record of when actions in a process are completed. The text file is intended to be audit log of that record to show if and when previously entries have been amended.

    You are completely correct in suggesting that in reality I'm not interested in when the document is just opened and closed without making any changes, but due to the way the document is used this doesn't happen very much. The reason why I was suggesting using the action of closed is because I don't really want to record the data just because I hit save to prevent data loss. I suppose my intimate goal would be to have some code which would save the contents to the text file only if the document was amended in anyway before it was saved, but this looked like far too big of a task for me to overcome with my limited knowledge. So out of the saves and closed action, closed looked like the best fit.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case, you could use a Document_Close macro in your document's 'ThisDocument' code module, coded along the lines of:
    Private Sub Document_Close()
    Dim DataFile As String, StrData As String, CCtrl As ContentControl
    DataFile = "C:\Users\" & Environ("UserName") & "\Documents\Data.txt"
    StrData = "": Open DataFile For Append As #1
    StrData = Format(Now, "DD-MMM-YYYY hh:mm:ss")
    For Each CCtrl In ThisDocument.ContentControls
      With CCtrl
        Select Case .Type
          Case Is = wdContentControlCheckBox
            StrData = StrData & vbTab & .Checked
          Case wdContentControlDate, wdContentControlDropdownList, wdContentControlRichText, wdContentControlText
            StrData = StrData & vbTab & .Range.Text
          Case Else
        End Select
      End With
    Next
    Print #1, StrData: Close #1
    End Sub
    To use it, create a file named 'Data.txt' in your Documents folder, with the first line containing 'Date & Time' followed by each of your content control titles (in order) preceded by a tab in each case. Whenever you close the document, the 'Data.txt' file will be updated - each entry will have a date/time stamp.

    Short of disabling macros when you open the document, there really is no way of both preventing updates to the data file if the document is just opened & closed and, at the same time, ensuring saved updates you might do to prevent data loss are captured. The alternative, if you're prepared to have a prompt every time you close the file, is to insert something like:
    If MsgBox("Save Data?", vbYesNo) <> vbYes Then Exit Sub
    as the first code line.

    Change the DataFile details to suit your requirements.
    Last edited by macropod; 10-14-2017 at 11:58 PM.
    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
  •