PDA

View Full Version : [SLEEPER:] Userform to DocVariables and file saveas command?



enigmaes
07-26-2014, 09:35 PM
Hi, all.

Using Word 2013. Trying to get a userform to fill in DocVariable formfields. Not having any luck. :)

Following programming is in place:
On the Userform:

Option Explicit

Private Sub cmdOK_Click()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
Me.Hide
With ActiveDocument
oVars("First") = Me.TextBox1.Text
oVars("Last") = Me.TextBox2.Text
oVars("DOB") = Me.TextBox3.Text
oVars("ID") = Me.TextBox4.Text
End With
ActiveDocument.Fields.Update
Set oVars = Nothing
Unload Me
End Sub

Private Sub TextBox1()
End Sub

Private Sub TextBox2()
End Sub

Private Sub TextBox3()
End Sub

Private Sub TextBox4()
End Sub

On the Document Module 1:


Option Explicit

Sub AutoNew()
Call ShowMyForm
End Sub

Sub ShowMyForm()
MyFormm.Show
End Sub

Sub CallUF()
Dim oFrm As MyFormm
Set oFrm = New MyFormm
oFrm.Show
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub

Help?

I also would like to have the file open, accept the information, plug it in, and then save it using a concatenated text string with directory\Last-First ID blah.doc, so that any further changes have already been saved to a new document name, generated by input from the userform.

Thanks in advance!

macropod
07-27-2014, 12:05 AM
Your first code block appears to do all that is required to update the variables and they're corresponding fields in the document. However, none of this code is relevant:

Private Sub TextBox1()
End Sub

Private Sub TextBox2()
End Sub

Private Sub TextBox3()
End Sub

Private Sub TextBox4()
End Sub

As for populating the filename from these variables, see, for example: http://www.msofficeforums.com/word-vba/21863-refering-msofiledialogueopen-variants.html#post67200

enigmaes
07-27-2014, 05:33 AM
As for populating the filename from these variables, see, for example:

Thanks for this. Glanced, and will read shortly.

Any suggestions for having the userform displayed? It is not displaying, and I thought with that code it should be.

enigmaes
07-27-2014, 01:14 PM
Userform is not displaying on what I have for the Autonew. Any ideas how to get it to display? Thanks!

macropod
07-27-2014, 05:22 PM
What is your Userform's name? You seem to be referencing it as MyFormm, but you also have this code, which implies you're going through some bizarre ritual to try to define it as something entirely new:

Sub CallUF()
Dim oFrm As MyFormm
Set oFrm = New MyFormm
oFrm.Show
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub

enigmaes
07-27-2014, 07:03 PM
I *finally* got the userform to populate.

I do have a couple of questions on the autosave portion, though.

The code needs to pass public variables in order to move from the UserForm and into the Module.

Since I am using the

ActiveDocument.Variables "stuff".value = textbox1.text

How would I assign a string that then could be used in the main module?

Looked at several examples, different sites, and couldn't get it together.

Thanks!!

macropod
07-27-2014, 07:38 PM
You could reference whatever variable you're using via its module name. For example:
MsgBox UserForm1.MyStr
might be use in the document's 'ThisDocument' module, where 'MyStr' is a public variable in the UserForm1 module.