Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    I created a template in Word 2007 that uses formfields (legacy). Every formfield has its own bookmark, which made it great to calculate further with these fields. The results of the calculation then again was used as a bookmark to show up somewhere else in the document.

    Problem for the people filling in the template: it wasnt clear which fields they should edit and which they should just leave alone and let them be calculated automatically.

    So i managed to create a userform in vba with only the necessary fields. This form replaces the bookmarks in the doc with the text values from the form. That way it removes the bookmarks and no more automatic calculations with the variables from the userform are done. Can I submit fields from the form into formfields of the word doc, rather than replace them?

    For example: in the userform txtDays is submitted to bkDays and txtFee is submitted to bkFee. Then in the word doc I have a field that calculates (bkDays*bkFee) which gives me bkTotal and that last bookmark is used in different locations around the doc. Problem is that bkDays and bkFee are replaced with the textvalue so no more calculations are done.

    I have no vba background so I was happy enough to toy the form together from tutorials around the web but now i'm a bit stuck and i have the feeling that i'm awefully close. This is what is used to pass the data from the userform to the worddoc when OK is clicked:
    With ActiveDocument
    .Bookmarks("bkDays").Range.Text = txtDays.Value

    Kind Regards Sven

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This function inserts text in a bookmark ensuring the bookmark is not removed:

    Code:
    Sub InsertText(ByVal sBookmarkName As String, sText As String, bMsg As Boolean)
    	Dim rRange As Range
    	On Error Resume Next
    	Set rRange = ActiveDocument.Bookmarks(sBookmarkName).Range
    	If rRange Is Nothing Then
    		If bMsg Then
    			MsgBox "the bookmark named:" _
    				   & vbNewLine & vbNewLine & sBookmarkName & vbNewLine & vbNewLine & _
    				   "Is absent from the current document.", vbOKOnly, sAppName & ", Error message"
    		End If
    		Exit Sub
    	End If
    	With rRange
    		.Delete Unit:=wdCharacter, Count:=1
    		.InsertAfter sText
    		.SetRange Start:=rRange.Start, End:=rRange.Start + Len(sText)
    	End With
    	ActiveDocument.Bookmarks.Add Range:=rRange, Name:=sBookmarkName
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Sven,

    Isn't this the same question you had already been given an answer for at microsoft.public.word.vba.general?

    Please see: http://www.excelguru.ca/node/7
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    [quote name='pieterse' post='786383' date='27-Jul-2009 14:09']This function inserts text in a bookmark ensuring the bookmark is not removed:

    Code:
    Sub InsertText(ByVal sBookmarkName As String, sText As String, bMsg As Boolean)
    	   Dim rRange As Range
    	   On Error Resume Next
    	   Set rRange = oNewDocument.Bookmarks(sBookmarkName).Range
       ...
     End Sub
    [/quote]
    It looks like this is missing the code to set the oNewDocument variable so it points to a document.

  5. #5
    New Lounger
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the Stuart

    i got an answer through the public vba newsgroups earlier yesterday. Sorry for the crosspost.

    http://groups.google.com/group/microsoft.p...7772b1ab9c4000#

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Indeed, oNewDocument should be replaced with ActiveDocument. Corrected.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •