PDA

View Full Version : Solved: Generate filename based on date/user initials



RoyLiam
08-30-2006, 07:34 PM
Hi there

I am creating a userform to input standard information into a document which needs to be created fairly regularly and saved.

What I would like to do is in a textbox prepopulate it with the current date and user initials leaving it blank for the user to fill in the rest (eg "06-08-29-lp-testdoc-m01" where lp are the user initials and the balance of the text is completed by the user in the textbox).

When the OK button is clicked after having filled in the whole form, the 'Save as' dialogue box would come up with the file name in as the default so that the user can easily save the document. If the directory path could also be established within the form that would be helpfull

I can see that the code 'Format(Now, "yy-mm-dd") would give part of the filename but i cannot see how to get the equilavent of "USERINITIALS \* Lower " fieldcode to extract the users initials from the user settings.

Additionally i cannot see how to bring up the 'save as' screen in a way that works; i have been able to get it to display using 'show' but it wouldn't save when this was clicked

Any comments most appreciated.

Regards

RoyLiam

fumei
08-30-2006, 11:18 PM
Sample file attached (as ZIP).

1. You can call the userform with the "Test The Form" icon on the menu bar. This simply calls the procedure TryFormSub TryForm()
UserForm1.Show
End Sub

2. The userform uses userForm_Initialize to prefill the textbox - default name Textbox1. Private Sub UserForm_Initialize()
TextBox1.Text = Format(Now, "yy-mm-dd") & " " _
& Application.UserInitials
End SubIt is prefilled with the formatted Now, two spaces, then the UserInitials.

3. The commandbutton (default name Commandbutton1) takes the text from the textbox1 - whatever it is - and puts it into the Name field of the SaveAs dialog. You may change as you like.Private Sub CommandButton1_Click()
With Dialogs(wdDialogFileSaveAs)
.Name = TextBox1.Text
.Show
End With
Unload Me
End SubNote the commandbutton unloads the form.

fumei
08-30-2006, 11:24 PM
The point being is that you CAN prefill the textbox.

You CAN change the text in the textbox.

You CAN send the textbox text (whatever it is) to the default name in the SaveAs dialog.

You CAN change that name in the the dialog, and save the file with that change.

RoyLiam
08-31-2006, 06:00 AM
Thanks Gerry this works a treat.

Is it possible to jump the prompt to a certain place in the textbox when you tab into it so that some doc specific text can be added in to the standard initials/date? (or in your example <Shift><Tab> To jump back to it)

I tried some code along the lines of the following to move the cursor to (say) 5 chars from the right when the box is entered:


Private Sub TextBox1_Enter()
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=(Len(TextBox1.Text) - 5)
End Sub


But it didn't seem to jump the cursor at all, and the whole of the default text remains highlighted when you tab into the cell. Any thoughts?

The Saveas dialogue box works great. I tried adding a ChDir "C:\" (say) to bring up the correct directory when the Saveas dialogue box opens, but it didn't seem to move the directory. Any ideas?


Private Sub CommandButton1_Click()

ChDir "C:\"

With Dialogs(wdDialogFileSaveAs)
.Name = TextBox1.Text
.Show
End With
Unload Me

End Sub


With best regards

Roy