Angyl,
No school and I have never cracked the cover of a VBA textbook. What little
I know I have picked up by reading and responding to newsgroup questions
like yours and asking lots of questions of this same newsgroups when I got
stumped.
The command button code is simply setting a value for the userform .Tag
member and then hidding the form from view. It is the procedure (I guess
you would call it a macro) "addrow" that is called in the Sub CallUF()
procedure that actually adds the field.
Open a new blank document and then press ALT+F11
This opens the VB editor. If not showing, show the Project Explorer window
by pressing CTRL+r
In the directory of the Project Explorer you should see Project (Document1)
Click on it and then press Insert>Module
Modules
Modules 1 will be added to the directory tree.
Double click on Module 1
In the big code window on the right type - Option Explicit
Click Tools>Options>Editor and check everything in that dialog.
Now paste the macro text I sent you:
Sub CallUF()
Dim myFrm As UF
Set myFrm = New UF
myFrm.Show
If myFrm.Tag = "Add Row" Then
AddRow
End If
Unload myFrm
Set myFrm = Nothing
End Sub
See the section "Technical details" at
http://gregmaxey.mvps.org/Custom_MsgBox.htm for a bit of discussion how the
userform is declared, initialized, shown, hidden, unload and finally killed.
You can see from the code above, when the user presses the command button in
the Userform then the .Tag value is going to be read in the code and call
the macro AddRow which is follows:
To see all of this happen, just step through the code using the F8 key.
Sub AddRow()
Dim oDoc As Document
Dim myRng As Word.Range
Dim myFormField As FormField
Dim i As Long
Dim pFormulaStr As String
Set oDoc = ActiveDocument
oDoc.Tables(1).Rows.Add
i = oDoc.Tables(1).Rows.Count '(+/- whatever to return the actual new
row number)
pFormulaStr = "=A" & i & " + B" & i & " + C" & i
Set myRng = oDoc.Tables(1).Cell(i, 4).Range
myRng.Collapse wdCollapseStart
Set myFormField = oDoc.FormFields.Add(Range:=myRng, _
Type:=wdFieldFormTextInput)
myFormField.TextInput.EditType Type:=wdCalculationText, _
Default:=pFormulaStr, _
Format:="0.00", Enabled:=False
End Sub
Sub CallUF()
Dim myFrm As UF
Set myFrm = New UF
myFrm.Show
If myFrm.Tag = "Add Row" Then
AddRow
End If
Unload myFrm
Set myFrm = Nothing
End Sub
Sub AddRow()
Dim oDoc As Document
Dim myRng As Word.Range
Dim myFormField As FormField
Dim i As Long
Dim pFormulaStr As String
Set oDoc = ActiveDocument
oDoc.Tables(1).Rows.Add
i = oDoc.Tables(1).Rows.Count '(+/- whatever to return the actual new
row number)
pFormulaStr = "=A" & i & " + B" & i & " + C" & i
Set myRng = ActiveDocument.Tables(1).Cell(i, 4).Range
myRng.Collapse wdCollapseStart
Set myFormField = ActiveDocument.FormFields.Add(Range:=myRng, _
Type:=wdFieldFormTextInput)
myFormField.TextInput.EditType Type:=wdCalculationText, _
Default:=pFormulaStr, _
Format:="0.00", Enabled:=False
End Sub