• insert data from userform into bookmark

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » insert data from userform into bookmark

    Author
    Topic
    #461488

    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

    Viewing 3 reply threads
    Author
    Replies
    • #1171197

      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
      • #1171266

        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

        It looks like this is missing the code to set the oNewDocument variable so it points to a document.

    • #1171264

      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
      [Fmr MS MVP - Word]

    • #1171290

      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#

    • #1171293

      Indeed, oNewDocument should be replaced with ActiveDocument. Corrected.

    Viewing 3 reply threads
    Reply To: insert data from userform into bookmark

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: