Consulting

Results 1 to 8 of 8

Thread: Newbie Userform Text box question

  1. #1

    Newbie Userform Text box question

    Hi All
    I am very new to Word 2000 VBA and I have created a template that has a custom userform. In one of the fields on the userform, I have a blank text box for the the user's name to be inserted. I would like her name to be inserted, by default, from a tiny text file located on the c drive, however, on a few occassions, the user has to type one of her boss's names in this field, so will I be able to do this and if so can someone give me a sample of some code to tell me how or point me in the right direction? Thanks.

  2. #2
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Let us know what the format of your file is, and we can supply you with some code. Also, you can also populate the textbox with the current user's name using the code:
    [vba]
    textbox1.value = Application.UserName
    [/vba]

    This code will use the Name tab from User Information box under Tools | Options.

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  3. #3
    Thanks for the quick response! I have the user's name in a notepad .txt file. When you say I can populate the text box, can I populate it with the user's name as well as all of her bosses? I thought I would need a combo box for that.

  4. #4
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    You application can determine the user's name automatically, but not a list of her supervisors. If her supervisor information is stored in a database or Windows Active Directory, you can programmatically retrieve rather than use a text file.

    Once you have the information, it is simple to place it in separate textboxes or a listbox or combobox. You just need to determine what owrks best in your application.

    I assume your notepad.txt file simply contains the user's name without any other formatting, and that there is no other text in the file. If so, you can use the following sample to read the name from the file.

    Just replace the path and filename in the example with what you are using.

    [vba]
    Sub ReadCSV()

    'Opens the txt file and reads it's contents

    Dim strFileName, strName As String
    Dim fd As FileDialog
    Dim i As Integer

    On Error Resume Next


    strFileName = "C:\MyPath\MyNotepadFile.txt"

    Open strFileName For Input As #1

    If Err.Number = 0 Then
    Input #1, TextBox1.Value
    Close #1

    Else
    MsgBox "There was a problem opening the file."
    Exit Sub

    End If


    End Sub
    [/vba]

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    James, what is the purpose of fd as FileDialog, i as Integer, or strName as String - if you do not use them?

  6. #6
    VBAX Regular Kelly's Avatar
    Joined
    Jun 2004
    Location
    Los Angeles, California
    Posts
    84
    Hi Chukesgirl!

    I just spent a few minutes putting together a template that MAY be similar to yours. I will zip my template into a ".zip" file and attach it below (here in this forum thread). Unzip it and save it as a ".dot" template, then create documents based on it and try it out.

    I have "deviated" from your parameters slightly. I have not used a text box or a combo box on my form. Instead, I have used a MACROBUTTON field. If you have not used a MACROBUTTON field before, then for starters you could use Word's "Help" and just type in MacroButton.

    IMPORTANT NOTE: Before attempting to use the file that I have attached, you will need to create a TEXT FILE called "namefile.txt" and save it in the path "C:\namefile.txt"

    My macro is set to look for such a text file at EXACTLY the path I have described. This "namefile.txt" is where you can store the user name if you wish. (although, jamescol is correct that you can automatically get the user name from Word's internally stored information. of course, that assumes that the name found in the User Information box under Tools | Options is the name that you are attempting to insert into the form)

    This is the macro code that runs when the user double-clicks the macrobutton:

    [vba]Sub GetNameInfo()

    Dim myCurrentDoc As String
    Dim OriginalProtection As Integer 'Remember if the doc was protected. If so, then we will "re-protect" at the end
    Dim myUserInput As Byte
    Dim TheName As String 'the name that is stored in the text file on "C:\"
    Dim ManualName As String 'a string for user input of any name they wish

    myCurrentDoc = ActiveDocument.Name
    OriginalProtection = Documents(myCurrentDoc).ProtectionType

    myUserInput = MsgBox("Use default name?" & vbCr & vbCr & _
    "If you choose NO, then you will be prompted" & vbCr & _
    "to manually enter a name.", vbYesNo, "Use default name?")

    If myUserInput = 6 Then

    '******* OPEN THE FILE THAT CONTAINS THE DEFAULT NAME **********
    On Error Resume Next
    Documents.Open "C:\namefile.txt"
    If Err.Number > 0 Then GoTo Whoops

    '****** HIDE THE NEW WINDOW ***********************************
    Windows("namefile.txt").Visible = False

    '****** ASSUME THE NAME IS THE FIRST LINE OF TEXT IN THE FILE *****
    TheName = Documents("namefile.txt").Paragraphs(1).Range.Text

    Windows("namefile.txt").Close wdDoNotSaveChanges

    On Error Resume Next
    Documents(myCurrentDoc).Unprotect
    On Error GoTo 0

    Documents(myCurrentDoc).Fields(1).Select
    Selection.TypeText TheName

    Documents(myCurrentDoc).Activate

    Else

    '****** DO THIS IF THE USER OPTED NOT TO ACCEPT THE DEFAULT NAME *********

    ManualName = InputBox("Enter any name you wish:", , "Santa Claus")

    On Error Resume Next
    Documents(myCurrentDoc).Unprotect
    On Error GoTo 0

    Documents(myCurrentDoc).Fields(1).Select
    Selection.TypeText ManualName

    Documents(myCurrentDoc).Activate

    End If

    If OriginalProtection > -1 Then Documents(myCurrentDoc).Protect OriginalProtection

    End

    Whoops:
    MsgBox "The macro could not open namefile.txt" & vbCr & vbCr & _
    "The default name cannot be inserted without access to ""C:\namefile.txt""" & vbCr & vbCr & _
    "The macro has ended unsuccessfully."

    End Sub[/vba]

    Actually, now that I think about it....
    jamescols choice of:
    Open strFileName For Input As #1
    is obviously superior to my use of the "documents" collection for accessing the data in the text file.

    I knew there was a better way, but I was too lazy to look it up.
    (all I could think of was:
    open(DAT,$datafile);
    @raw_data=<DAT>;
    close(DAT);

    ...but that's PERL for crying out loud! )

    However, I still think that my template as a whole is a good way to do what you wish to do. (I'll re-write my macro tomorrow to incorporate Jamescol's great stuff)

    Feel free to bombard me with questions. I'm too bleary-eyed right now to anticipate any questions and "pre-empt" them, so I'll just come back later to see if I confused anyone.

    See you all again soon!!!
    Cheers to all!!!
    Kelly


  7. #7
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Quote Originally Posted by fumei
    James, what is the purpose of fd as FileDialog, i as Integer, or strName as String - if you do not use them?
    Gerry,
    Good catch! These variables are not needed for this example. I just wasn't careful enough when editing another sample I had that did use these variables.

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  8. #8
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    I use an ini file to save the user name, etc. Just popping into this question, or you could write to the registry. Would that help?

    Something like this for the ini file (obviously this is just an example):

    [VBA]
    Public Sub AutoNew()
    On Error GoTo ErrorMsg
    Dim sDesigPath As String
    Dim sIniFile As String
    sDesigPath = Options.DefaultFilePath(Path:=wdUserTemplatesPath)
    sIniFile = sDesigPath & "\Personal.ini"
    Load frmFax
    frmFax.TextBox1.Text = ""
    frmFax.TextBox2.Text = ""
    frmFax.TextBox3.Text = ""
    frmFax.TextBox4.Text = ""
    frmFax.TextBox5.Text = ""
    frmFax.TextBox6.Text = System.PrivateProfileString(sIniFile, "Personal",
    "From")
    frmFax.TextBox7.Text = System.PrivateProfileString(sIniFile, "Personal",
    "Extension")
    frmFax.TextBox8.Text = System.PrivateProfileString(sIniFile, "Personal",
    "SenderID")
    frmFax.TextBox9.Text = ""
    frmFax.TextBox10.Text = ""
    frmFax.TextBox11.Text = Format(VBA.Date, "mmmm d, yyyy")
    If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveWindow.ActivePane.View.Type = wdPageView
    Else
    ActiveWindow.View.Type = wdPageView
    End If
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100

    frmFax.Show
    ErrorMsg:
    frmFax.Hide
    Unload frmFax
    End Sub
    [/VBA]
    Joanne

Posting Permissions

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