Link to home
Start Free TrialLog in
Avatar of Aage
AageFlag for Norway

asked on

MS Word - populate fixed field

Hi,

We have a lot of different contract documents that we use for templates.

The documents contains much repeating text such as company name, contacts and so on.

Is it possible to make a fill in form that are like a sidebar sitting on the right of the main document, where a user fills in his information.

When the user fills in the fields in the form, the filled in text will auto populate the contract document.

Hope you understand.

Thanks!
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

There are several techniques that you could use.
For instance you could bookmark any text to be repeated and place Ref fields at the places that it is to be redisplayed.

You could use Form Fields or Content Controls, perhaps with the 'sub' form as a building block. It would be part of the document and not a sidebar.

The nearest that you could get to that would be as a VBA UserForm which could load text on to the document at locations defined by, say, pre-existing bookmarks.
Avatar of Aage

ASKER

Great thanks:)

I think it would be more user efficient to use a userform to populate the document.

Do you know of any example docs with a VBA userform that I could build it from?

Thanks!
The best approach will depend on your exact circumstance. In this one, you need to set Bookmarks in the template. Because Bookmark names must be unique, you will need to group all those expecting the same data, but differentiate between each instance. You can do this by appending a sequential number to each set of names.

E.g. CompanyName_01,  CompanyName_02, CompanyName_03

The code in the Userform would then be like this:
Option Explicit

Private Sub CommandButton1_Click()
    Dim bmk As Bookmark
    For Each bmk In ActiveDocument.Bookmarks
        Select Case Split(bmk.Name, "_")(0)
            Case "CompanyName"
                InsertTextInBookmark bmk, txtCompanyName.Text
            Case "Contact"
                InsertTextInBookmark bmk, txtContact.Text
            Case "Address"
                '...
            'Case ...
        End Select
    Next bmk
End Sub

Sub InsertTextInBookmark(bmk As Bookmark, strText As String)
    Dim rng As Range
    
    Set rng = bmk.Range
    rng.Text = strText
    're-add bookmark in case it was overwritten and
    'to ensure that it contains the text
    rng.Document.Bookmarks.Add bmk.Name, rng
End Sub

Open in new window

Avatar of Aage

ASKER

Hi,
Thanks, I have made a userform now in my template. I arranged the textfield as I wanted it in the form. I added a 'OK' button where I added the script above with modifications on my field names.

But I have some questions, does the Case "CompanyName" reflect the field name in the userform or the bookmark name?

Here is my code so far, but it don't work, nothing happends when I push "OK" button.

Option Explicit

Private Sub OK_Click()
    Dim bmk As Bookmark
    For Each bmk In ActiveDocument.Bookmarks
        Select Case Split(bmk.Name, "_")(0)

            Case "Radgiver10"
'Case "Radgiver10" is the name of the field in the Userform.

                InsertTextInBookmark bmk, txtRådgiver.Text
'"txtRådgiver.Text" is the bookmark name.

            Case "Radgiver10"
                InsertTextInBookmark bmk, txtRådgiver01.Text
            Case "Radgiver10"
                InsertTextInBookmark bmk, txtRådgiver02.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver02.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver03.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver04.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver05.Text
            Case "Oppdragsgiver10"
                InsertTextInBookmark bmk, txtOppdragsgiver06.Text
            Case "LederFast"
            InsertTextInBookmark bmk, txtStyrelederFast.Text
            Case "LederTime"
                InsertTextInBookmark bmk, txtStyrelederTime.Text
            Case "MedlemFast"
                InsertTextInBookmark bmk, txtStyremedlemFast.Text
            Case "MedlemTime"
                InsertTextInBookmark bmk, txtStyremedlemTime.Text
            Case "RadgiverTime"
                InsertTextInBookmark bmk, txtRadgiverTime.Text
            
        End Select
    Next bmk
End Sub

Sub InsertTextInBookmark(bmk As Bookmark, strText As String)
    Dim rng As Range
    
    Set rng = bmk.Range
    rng.Text = strText
    're-add bookmark in case it was overwritten and
    'to ensure that it contains the text
    rng.Document.Bookmarks.Add bmk.Name, rng
End Sub

Open in new window

If you have the same text to be inserted in several places, you will need to bookmark each place. You cannot use exactly the same name for more than one bookmark, but you can fill more than one bookmark from the same text box.

When a match is found in a Select Case block , the action will be done in the Case part, and the block is edited, so line 16 in you code will never be actioned. The same applies to lines 20 to 28, etc.

My suggestion is that you have bookmarks with names like "Radgiver_01",  "Radgiver_02",
"Oppdragsgiver_01",  "Oppdragsgiver_02",  "Oppdragsgiver_03". It doesn't matter what comes after the underscore, provided that the first parts are all the same and the last parts are all different, for any given first part.
Private Sub OK_Click()
    Dim bmk As Bookmark
    For Each bmk In ActiveDocument.Bookmarks
        Select Case Split(bmk.Name, "_")(0)

            Case "Radgiver"
                '"Radgiver" is the first part of the bookmark name on the document.
                InsertTextInBookmark bmk, txtRådgiver.Text
                '"txtRådgiver.Text" is the text from the text box on the user form
            Case "Oppdragsgiver"
                InsertTextInBookmark bmk, txtOppdragsgiver.Text
            Case "LederFast" 'if there is no "_" in the bookmark name, Split(bmk.Name, "_")(0) will return the whole name
                InsertTextInBookmark bmk, txtStyrelederFast.Text
            Case "LederTime"
                InsertTextInBookmark bmk, txtStyrelederTime.Text
            Case "MedlemFast"
                InsertTextInBookmark bmk, txtStyremedlemFast.Text
            Case "MedlemTime"
                InsertTextInBookmark bmk, txtStyremedlemTime.Text
            Case "RadgiverTime"
                InsertTextInBookmark bmk, txtRadgiverTime.Text
            
        End Select
    Next bmk
End Sub

Open in new window

Avatar of Aage

ASKER

Thank you very much, almost there I think, but get a error on the last line:

 rng.Document.Bookmarks.Add bmk.Name, rng

Open in new window


What does the  Next bmk do?

My code is now:

Private Sub OK_Click()

    Dim bmk As Bookmark
    For Each bmk In ActiveDocument.Bookmarks
        Select Case Split(bmk.Name, "_")(0)

            Case "Rådgiver"
                '"Radgiver" is the first part of the bookmark name on the document.
                InsertTextInBookmark bmk, txtRadgiver.Text
                '"txtRadgiver.Text" is the text from the text box on the user form
            Case "Oppdragsgiver"
                InsertTextInBookmark bmk, txtOppdragsgiver.Text
                'if there is no "_" in the bookmark name, Split(bmk.Name, "_")(0) will return the whole name
            Case "Kontaktperson"
                InsertTextInBookmark bmk, txtKontaktperson
            Case "StyrelederFast"
                InsertTextInBookmark bmk, txtLederFast.Text
            Case "StyrelederTime"
                InsertTextInBookmark bmk, txtLederTime.Text
            Case "StyremedlemFast"
                InsertTextInBookmark bmk, txtMedlemFast.Text
            Case "StyremedlemTime"
                InsertTextInBookmark bmk, txtMedlemTime.Text
            Case "RådgiverTime"
                InsertTextInBookmark bmk, txtRadgiverTime.Text
        End Select
    Next bmk
End Sub

Sub InsertTextInBookmark(bmk As Bookmark, strText As String)
    Dim rng As Range
    
    Set rng = bmk.Range
    rng.Text = strText
    're-add bookmark in case it was overwritten and
    'to ensure that it contains the text
    rng.Document.Bookmarks.Add bmk.Name, rng
End Sub

Open in new window


Sorry for my slow learning curv. Hope we can get it working.
What is the error message and number please?

The 'Next bmk' line matches this one:
    For Each bmk In ActiveDocument.Bookmarks

Open in new window


That is an instruction to walk through each member of the 'ActiveDocument.Bookmarks' collection, setting the variable bmk to that member. The Next instruction tells the code to loop back to the 'For Each' line and set the variable to the next member. The loop continues until the last member of the collection has been processed.
Avatar of Aage

ASKER

Ok, I see!

I get "Run time error 5825 - object deleted" on line 37. I'll attach the document.
Mal---exchange.doc
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aage

ASKER

Perfect! Thank you so much :)
Avatar of Aage

ASKER

Perfect! and thank you!