Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross-Referencing Calculations in Word 2003

Status
Not open for further replies.

runeariala

Technical User
Jul 31, 2008
3
US
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:
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
 
Hi Annie,

Use the following code, with which you pass the bookmark's name (BMkNm) and contents (NewTxt) to update the bookmark:
Code:
Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
If Documents.Count > 0 then
	If ActiveDocument.Bookmarks.Exists(BmkNm) Then
		Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
		BmkRng.Text = NewTxt
		ActiveDocument.Bookmarks.Add BmkNm, BmkRng
	End if
End if
Set BmkRng = Nothing
End sub
Cheers

[MS MVP - Word]
 
If by "contents" you mean to replace NewTxt with the integer variable I had previously calculated, then this does not work. The line "BmkRng.Text = NewTxt" replaces the bookmark, thus creating a run-time error on the next line. If you mean to replace your variable NewTxt with something else, please let me know.

Thanks!-Annie
 
Hi Annie,

I missed the 'integer' bit. As coded, the macro works with strings. For your purposes, change 'NewTxt As String' to 'NewTxt As Integer'.
to test:
. Insert a bookmark named "BkMrk" into a test document.
. Run the following code
Code:
Sub Test()
Dim StrBkMrk As String, BkMrkVal As Integer
StrBkMrk = "BkMrk"
BkMrkVal = 300
UpdateBookmark StrBkMrk, BkMrkVal
End Sub
As you can see from the test sub, the variable names used in the calling sub don't have to match those in the UpdateBookmark sub - they just have to be of the same type.

Cheers

[MS MVP - Word]
 
Thanks! That worked. I guess I was overlooking that part.
 
Hi Annie,

Another approach, which would be useful if you've got multiple data types to pass, would be to change 'NewTxt As String' to 'NewTxt'.

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top