dariojalon
11-15-2012, 08:29 AM
Hi, I'm new to Word Macros and was requested to program a code that allows a user to imput certain data via a form that will later on create a contract document that attaches all the selected clauses (based on a drop down menu) with some variable fields in it based on the previously created form.
I hope that sumarizes well enough the code idea.
I was succesfull until this point, but I now want the code the extract all the data inserted in the form by the user each time it is executed and insert it in an excel sheet that stores this info as a log. Here is were the problem beggins! :banghead:
Maybe my vba code is not the best one there is. Until this point it is all stored in a UserForm and works very well. I will try to attach it below.
Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
'I assign the values of the Form to the DocVariables in my word document
oVars("RazonSocial").Value = Me.TextBox1.Value
oVars("Poliza").Value = Me.TextBox2.Value
oVars("PrimaMediaInicial").Value = Me.TextBox17.Value
oVars("CalleNumero").Value = Me.TextBox4.Value
oVars("Localidad").Value = Me.TextBox5.Value
oVars("CodigoPostal").Value = Me.TextBox6.Value
oVars("Provincia").Value = Me.TextBox7.Value
oVars("CUIT").Value = Me.TextBox8.Value
oVars("Ramo").Value = Me.TextBox9.Value
'Update the fields in the body of the document
ActiveDocument.Fields.Update
'Clear the variable
Set oVars = Nothing
'Unload the form
Unload Me
End Sub
Do you guys know how to extract this TextBox Values or oVars to an existing Excel?
Thanks a lot for your help! :bow:
Best Regards.
I hope that sumarizes well enough the code idea.
I was succesfull until this point, but I now want the code the extract all the data inserted in the form by the user each time it is executed and insert it in an excel sheet that stores this info as a log. Here is were the problem beggins! :banghead:
Maybe my vba code is not the best one there is. Until this point it is all stored in a UserForm and works very well. I will try to attach it below.
Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
'I assign the values of the Form to the DocVariables in my word document
oVars("RazonSocial").Value = Me.TextBox1.Value
oVars("Poliza").Value = Me.TextBox2.Value
oVars("PrimaMediaInicial").Value = Me.TextBox17.Value
oVars("CalleNumero").Value = Me.TextBox4.Value
oVars("Localidad").Value = Me.TextBox5.Value
oVars("CodigoPostal").Value = Me.TextBox6.Value
oVars("Provincia").Value = Me.TextBox7.Value
oVars("CUIT").Value = Me.TextBox8.Value
oVars("Ramo").Value = Me.TextBox9.Value
'Update the fields in the body of the document
ActiveDocument.Fields.Update
'Clear the variable
Set oVars = Nothing
'Unload the form
Unload Me
End Sub
Do you guys know how to extract this TextBox Values or oVars to an existing Excel?
Thanks a lot for your help! :bow:
Best Regards.