Page 1 of 3 123 LastLast
Results 1 to 15 of 39
  1. #1
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello,

    I have made a form in word. For simplicity lets say the form is the following:
    Name: Your Name
    Where it says Your Name you would click on it and then type in your name.
    That part of the form works great, but now I would like to add a submit button that would save it as a .txt file.
    I would like the text file to look like this:
    Name: John Smith

    I realize I can just save the form as a text file, but I will have other people using this form, and I want to make it very simple for them.

    Thanks as always,
    Handy Andy
    Time can fix anything.....even a broken clock. - Handy Andy

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    By 'form', do you mean a form in a Word document (using formfields?), or a userform created in the Visual Basic Editor?

  3. #3
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    A Word Document using form fields.
    Time can fix anything.....even a broken clock. - Handy Andy

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    See the attached demo document.

    [attachment=87115:Form.doc]

    It contains two form fields; the first has bookmark name YourName. It also contains a command button cmdSave from the Control Toolbox toolbar, with code:

    Code:
    Private Sub cmdSave_Click()
      Dim strPath As String
      Dim strFile As String
      Dim strName As String
      Dim strLine As String
      Dim f As Integer
    
      strName = ActiveDocument.FormFields("YourName").Result
      If strName = "" Then
        MsgBox "Please enter your name", vbExclamation
        Exit Sub
      End If
    
      strLine = "Your name: " & strName
      f = FreeFile
      strPath = Application.Options.DefaultFilePath(wdDocumentsPath)
      If Not Right(strPath, 1) = "\" Then
        strPath = strPath & "\"
      End If
      strFile = "Data.txt"
      Open strPath & strFile For Output As #f
      Print #f, strLine
      Close #f
    End Sub
    You can modify the code to suit your needs.
    Attached Files Attached Files

  5. #5
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks, but where does it save the .txt file?
    Time can fix anything.....even a broken clock. - Handy Andy

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The code saves the text file in the folder specified by the variable strPath. In the demo document, strPath is set to the user's default document folder for Word. As I mentioned, you can modify it to suit your needs - just set the value of strPath to the folder where you want to save the text file.

  7. #7
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    ok, I'm sorry. I am new with using macros, and I don't know how to pull up the code to change the file path.
    Time can fix anything.....even a broken clock. - Handy Andy

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Press Alt+F11 to activate the Visual Basic Editor.

  9. #9
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    I tried putting in C:\ after strPath =
    and after clicking on the submit button I can't find a Data.txt file in C:\

    What am I doing wrong?
    Time can fix anything.....even a broken clock. - Handy Andy

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Did you use

    strPath = "C:\"

    Text values in VBA must be enclosed in double quotes.

  11. #11
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yes, I did.
    And is there supposed to be a space before and after the =
    Time can fix anything.....even a broken clock. - Handy Andy

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by HandyAndy View Post
    And is there supposed to be a space before and after the =
    The VBA editor will automatically add a space before and after = if you don't type them.

    If the code doesn't work, check your macro security level in Tools | Macro | Security...
    If it is set to High or Very High, macros will be disabled.
    You should set the level to Medium, then open (or reopen) the document.
    You should then be asked whether you want to enable macros - answer Yes.

  13. #13
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    After setting the macro security to low, I still had no luck.
    Then I went in to edit the macro to look everything over again, and when I clicked on Run Macro I get the following error:
    BASIC runtime error. Object Variable not set.
    The error points to this line: strName = Activedocument.FormFields("YourName").Result
    I don't know what this means.
    Time can fix anything.....even a broken clock. - Handy Andy

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The code expects that the form field that you want to use for the text file has been named YourName.
    You can do this as follows:
    - Unprotect the document.
    - Double-click the relevant form field.
    - Click in the 'Bookmark name' box and enter YourName
    - Click OK.
    - Protect the document for forms again.

    Of course, you can give the form field a different name. If so, you must change the line

    strName = Activedocument.FormFields("YourName").Result

    accordingly.

  15. #15
    3 Star Lounger
    Join Date
    Mar 2009
    Location
    Somewhere Over the Rainbow
    Posts
    232
    Thanks
    0
    Thanked 1 Time in 1 Post
    I'm sorry to be a pest, but everything I try doesn't seem to work correctly.

    Could you upload a new document that will save data.txt to C:\
    This way I will know that it worked for you and I can learn how to modify it by seeing the correct code.

    Thanks again.
    Time can fix anything.....even a broken clock. - Handy Andy

Page 1 of 3 123 LastLast

Posting Permissions

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