From: Graham Mayor on 6 Mar 2010 02:26 The macro and form would normally go in the document template, but for your own use it could work from the normal template. However your reply gives the clue to the problem You say you have used the macro to open the document and it seems likely that the wrong document is now being addressed by the userform and so nothing appears to happen. Open your document. Save the document as a template (DOT or DOTM - NOT DOTX) with the docvariable fields in place. Move the macro and the userform to the new template - see http://www.gmayor.com/installing_macro.htm Rename Greg's macro AutoNew thus Sub AutoNew() Dim oFrm As UserForm1 'or whatever name you gave to your form Set oFrm = New UserForm1 oFrm.Show Unload oFrm Set oFrm = Nothing End Sub Remove any reference you may have added to opening the document. Now create a new document from the template. The form will pop up, you can fill it and it will update the focvariable fields in the document (provided they are in the body of the document - if they are elsewhere you will need a better targeted update macro. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> "Mark" <Mark(a)discussions.microsoft.com> wrote in message news:0678923B-927C-4403-B2E7-6EFBCB99D3D5(a)microsoft.com... > Greg, > > That was very helpful. I was even able to add an open statement that first > loads my document containing the fields to be filled. > > The only problem is that after filling in all the info, when I keep > pressing > enter it only cycles back to textbox 1 and to the other textboxes without > putting the typed info in to the fields. > > Thanks, > > Mark > > "Greg Maxey" wrote: > >> Mark, >> >> You would call your UserForm using a procedure in a standard project >> module: >> >> Sub CallUF() >> Dim oFrm As UserForm1 'or whatever name you gave to your form >> Set oFrm = New UserForm1 >> oFrm.Show >> Unload oFrm >> Set oFrm = Nothing >> End Sub >> >> >>
From: Mark on 6 Mar 2010 02:51 For added info, here is my code for my userform: Private Sub UserForm_Click() Dim oVars As Variables Set oVars = ActiveDocument.Variables oVars("varDoctor1").Value = Me.TextBox1.Value oVars("varDoctor2").Value = Me.TextBox2.Value oVars("varStreetAddress").Value = Me.TextBox3.Value oVars("varCityStateZip").Value = Me.TextBox4.Value oVars("varPatient").Value = Me.TextBox5.Value oVars("varAge").Value = Me.TextBox6.Value oVars("varRaceSex").Value = Me.TextBox7.Value ActiveDocument.Fields.Update Unload Me End Sub And here is the code for the macro that brings up the userform with my document: Sub CallUF() Documents.Open FileName:="C:\Documents and Settings\Administrator\My Documents\A Consult Letter.doc", ConfirmConversions:= _ False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _ PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _ WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="" Selection.Find.ClearFormatting Dim oFrm As ConsultLetter 'or whatever name you gave to your form Set oFrm = New ConsultLetter oFrm.Show Unload oFrm Set oFrm = Nothing End Sub Mark
From: Greg Maxey on 6 Mar 2010 08:31 Mark, First, I agree with Graham that you should create a template and not recycle an existing document. However, if you want to continue that course you could use code something like this: In a standard module: Option Explicit Dim oDoc As Word.Document Sub CallUF() Dim oFrm As ConsultLetter Set oDoc = Documents.Open(FileName:="C:\Consult Letter") 'Use your own path of course. Set oFrm = New ConsultLetter oFrm.Show Unload oFrm Set oFrm = Nothing Set oDoc = Nothing End Sub Sub UpdateLetterFields(frmCL As ConsultLetter) Dim oVars As Variables Set oVars = oDoc.Variables oVars("varDoctor1").Value = frmCL.TextBox1.Value oVars("varDoctor2").Value = frmCL.TextBox2.Value oVars("varStreetAddress").Value = frmCL.TextBox3.Value oVars("varCityStateZip").Value = frmCL.TextBox4.Value oVars("varPatient").Value = frmCL.TextBox5.Value oVars("varAge").Value = frmCL.TextBox6.Value oVars("varRaceSex").Value = frmCL.TextBox7.Value oDoc.Fields.Update End Sub In the UserForm module CommandButton_Click use: Private Sub CommandButton1_Click() Me.Hide UpdateLetterFields Me End Sub Since you are just learning, you should learn to minimize UserForm code as much as possible to only the code required to manage the form itself. Here you are passing the form as an argument to a called procedure in the standard module. The called procedure does the work of updating the document fields with the form data. Mark wrote: > Greg, > > That was very helpful. I was even able to add an open statement that > first loads my document containing the fields to be filled. > > The only problem is that after filling in all the info, when I keep > pressing enter it only cycles back to textbox 1 and to the other > textboxes without putting the typed info in to the fields. > > Thanks, > > Mark > > "Greg Maxey" wrote: > >> Mark, >> >> You would call your UserForm using a procedure in a standard project >> module: >> >> Sub CallUF() >> Dim oFrm As UserForm1 'or whatever name you gave to your form >> Set oFrm = New UserForm1 >> oFrm.Show >> Unload oFrm >> Set oFrm = Nothing >> End Sub
From: Greg Maxey on 6 Mar 2010 08:55 Since you have Unload oFrm in the calling macro, change Unload Me to Me.Hide. Mark wrote: > For added info, here is my code for my userform: > > Private Sub UserForm_Click() > Dim oVars As Variables > Set oVars = ActiveDocument.Variables > oVars("varDoctor1").Value = Me.TextBox1.Value > oVars("varDoctor2").Value = Me.TextBox2.Value > oVars("varStreetAddress").Value = Me.TextBox3.Value > oVars("varCityStateZip").Value = Me.TextBox4.Value > oVars("varPatient").Value = Me.TextBox5.Value > oVars("varAge").Value = Me.TextBox6.Value > oVars("varRaceSex").Value = Me.TextBox7.Value > ActiveDocument.Fields.Update > Unload Me > End Sub > > > And here is the code for the macro that brings up the userform with my > document: > > Sub CallUF() > Documents.Open FileName:="C:\Documents and Settings\Administrator\My > Documents\A Consult Letter.doc", ConfirmConversions:= _ > False, ReadOnly:=False, AddToRecentFiles:=False, > PasswordDocument:="", _ > PasswordTemplate:="", Revert:=False, > WritePasswordDocument:="", _ WritePasswordTemplate:="", > Format:=wdOpenFormatAuto, XMLTransform:="" > Selection.Find.ClearFormatting > Dim oFrm As ConsultLetter 'or whatever name you gave to your form > Set oFrm = New ConsultLetter > oFrm.Show > Unload oFrm > Set oFrm = Nothing > End Sub > > > Mark
From: Mark on 6 Mar 2010 14:49 Greg, I tried your code but I get a compile error that "variable not defined" at the second line where I have the correct path for my document. Set oDoc = Documents.Open(FileName:="C:\Documents and Settings\Administrator\My Documents\A Consult Letter.doc") 'Use your own path of course. I am not sure what is wrong as that is the correct path to open the document. Mark "Greg Maxey" wrote: > Mark, > > First, I agree with Graham that you should create a template and not recycle > an existing document. However, if you want to continue that course you > could use code something like this: > > In a standard module: > > Option Explicit > Dim oDoc As Word.Document > > Sub CallUF() > Dim oFrm As ConsultLetter > Set oDoc = Documents.Open(FileName:="C:\Consult Letter") 'Use your own path > of course. > Set oFrm = New ConsultLetter > oFrm.Show > Unload oFrm > Set oFrm = Nothing > Set oDoc = Nothing > End Sub > > Sub UpdateLetterFields(frmCL As ConsultLetter) > Dim oVars As Variables > Set oVars = oDoc.Variables > oVars("varDoctor1").Value = frmCL.TextBox1.Value > oVars("varDoctor2").Value = frmCL.TextBox2.Value > oVars("varStreetAddress").Value = frmCL.TextBox3.Value > oVars("varCityStateZip").Value = frmCL.TextBox4.Value > oVars("varPatient").Value = frmCL.TextBox5.Value > oVars("varAge").Value = frmCL.TextBox6.Value > oVars("varRaceSex").Value = frmCL.TextBox7.Value > oDoc.Fields.Update > End Sub > > In the UserForm module CommandButton_Click use: > > Private Sub CommandButton1_Click() > Me.Hide > UpdateLetterFields Me > End Sub > > Since you are just learning, you should learn to minimize UserForm code as > much as possible to only the code required to manage the form itself. Here > you are passing the form as an argument to a called procedure in the > standard module. The called procedure does the work of updating the > document fields with the form data.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Bringing different windows to the front Next: Direct edit of Building Blocks |