PDA

View Full Version : Automated field compilator



zonorox
04-01-2016, 02:09 PM
Hi all,

i need some help to automate a word document. Basically i have a word document that i use like a template, so there are some fields that are different every time. something like this:
-------------
Name: Marco
Surname: Barbaro



This is a report generated by %Marco perfermed today, 01/04/2016.

-------------

I want a popup when i start the document where i can insert the paramenter Name, Surname, generated by and will fill up the empty parts ( Name, surname and generated by) for me.


Hopefully i was clear enough :)

Thanks in advance

gmayor
04-01-2016, 08:51 PM
This is best achieved with a userform in the template called from an autonew macro. At its simplest this comprises a form with a few text fields and you write the data from those to docvariables or bookmarked locations. The basics are covered at http://www.gmayor.com/Userform.htm

zonorox
04-02-2016, 04:06 AM
This is best achieved with a userform in the template called from an autonew macro. At its simplest this comprises a form with a few text fields and you write the data from those to docvariables or bookmarked locations. The basics are covered at

Hi gmayor, thanks for your time. I managed to follow your tutorial and i'm almost near of what i want :)

The only issue is when im going to click the button "OK" that will insert me the fileds, i have the following error " Wrong Number of arguments or invalid property assigment" . This is what i did, and the first line
Private Sub CommandButton1_Click() is coloured in yellow.


Private Sub CommandButton1_Click()
'Definiamo qui le variabili oVars
Set oVars = ActiveDocument.Variables
'Nascondi the userform
Me.Hide
'Assegniamo qui le 3 variabili create precedentement
oVars("var1").Value = Me.TextBox1.Value
oVars("var2").Value = Me.TextBox2.Value
oVars("var3").Value = Me.TextBox3.Value
ActiveDocument.Fields.Update


End Sub


Can you help me? Thanks in advance

zonorox
04-02-2016, 04:14 AM
No worries, i managed. There was typo error :) Thanks a lot!!

gmaxey
04-02-2016, 06:09 AM
zonorox,

There is nothing wrong with docvariable fields or bookmarks. Just for your information though if you are using Word 2007 or higher, you can use content controls for your data fields and code in the form something like this:


Private Sub CommandButton1_Click()
With ActiveDocument
.SelectContentControlsByTitle("Name").Item(1).Range.Text = TextBox1
.SelectContentControlsByTitle("Surname").Item(1).Range.Text = TextBox2
.SelectContentControlsByTitle("Generated by").Item(1).Range.Text = TextBox3
End With
Hide
lbl_Exit:
Exit Sub
End Sub

zonorox
04-02-2016, 10:35 AM
Hi Gmayor,

you have any idea why i cannot write inside the footer?

I can write everywhere into the document but not inside the footer. There is some extra code to add?

Thanks

gmaxey
04-02-2016, 06:34 PM
If you are writing to docVariables then you are probably writing just fine but the ActiveDocument.Fields.Update is not updating fields in the footer.

You could add code to loop through the storyranges and update the footer storyranges or you could just toggle in and out of Print Preview:


Dim lngView As Long
With ActiveDocument
lngView = .ActiveWindow.View.Type
.PrintPreview
.ActiveWindow.View.Type = lngView
End With

The content control method I showed you doesn't have this issue.