runeariala
Technical User
I am creating a document which utilizes a userform to have the user input some numbers which are then mathimatically manipulated, with the results output into a simple document. This is just a test document to be used as training to create some more complicated legal forms.
To start with, I ask the user to input 3 numbers. The first number is added to 3, and outputs an answer to a bookmark. The third number is subtracted from the second number, and outputs an answer to a different bookmark. The two answers are then multiplied, with THAT answer outputed to yet a different bookmark.
I can get the math to work flawlessly, but where I am getting stumped is having the two answers output to two separate locations. I know that Word will only allow one placement of a bookmark, and will require a cross-reference to that bookmark for each additional instance of the information. My issue is evidently that, when my code runs the calculations, the result REPLACES the bookmark, therefore invalidating the cross-reference to that bookmark further down the document.
This is what my template looks like (note, there is no text in my bookmarks, I just added it for clarification purposes):
First number: [ firstnumber ] + 3 = [ totaladd ]
Second number: [ secondnumber ] - [ subtract ] = [ totalsubtract ]
Both totals together: { REF totaladd } * { REF totalsubtract} = [ total ]
Here is the portion of my code that takes the userform info, makes calculations:
Am I just going about this the wrong way? Does anyone have an easier solution? I'm new at this, but it seems that there should be a reasonable solution that I'm just not seeing.
Thanks in advance!-Annie
To start with, I ask the user to input 3 numbers. The first number is added to 3, and outputs an answer to a bookmark. The third number is subtracted from the second number, and outputs an answer to a different bookmark. The two answers are then multiplied, with THAT answer outputed to yet a different bookmark.
I can get the math to work flawlessly, but where I am getting stumped is having the two answers output to two separate locations. I know that Word will only allow one placement of a bookmark, and will require a cross-reference to that bookmark for each additional instance of the information. My issue is evidently that, when my code runs the calculations, the result REPLACES the bookmark, therefore invalidating the cross-reference to that bookmark further down the document.
This is what my template looks like (note, there is no text in my bookmarks, I just added it for clarification purposes):
First number: [ firstnumber ] + 3 = [ totaladd ]
Second number: [ secondnumber ] - [ subtract ] = [ totalsubtract ]
Both totals together: { REF totaladd } * { REF totalsubtract} = [ total ]
Here is the portion of my code that takes the userform info, makes calculations:
Code:
' Takes information in text boxes to replace bookmarks
With ActiveDocument
.Bookmarks("firstnumber").Range _
.InsertBefore TextBox1
.Bookmarks("secondnumber").Range _
.InsertBefore TextBox2
.Bookmarks("subtract").Range _
.InsertBefore TextBox3
End With
Dim vTotalAdd As Integer
Dim vTotalSubtract As Integer
Dim vTotal As Integer
vTotalAdd = TextBox1.Text + 3
ActiveDocument.Bookmarks("totaladd").Range.Text = vTotalAdd
vTotalSubtract = TextBox2.Text - TextBox3.Text
ActiveDocument.Bookmarks("totalsubtract").Range.Text = vTotalSubtract
vTotal = vTotalAdd * vTotalSubtract
ActiveDocument.Bookmarks("total").Range.Text = vTotal
' Updates cross-reference fields upon hitting ok
With Options
.UpdateFieldsAtPrint = True
.UpdateLinksAtPrint = True
End With
ActiveDocument.Fields.Update
Am I just going about this the wrong way? Does anyone have an easier solution? I'm new at this, but it seems that there should be a reasonable solution that I'm just not seeing.
Thanks in advance!-Annie