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!

Field codes to automate document - may need VBA

Status
Not open for further replies.

NIA2

Technical User
Aug 30, 2006
137
AU
Hi everyone,

I'm trying to automate a proposal document by using field codes. I'm basically using the Fillin field, eg. {FILLIN "ENTER THE NAME OF THE PROPOSAL" \* MERGEFORMAT} to prompt the user for information. If this information is used elsewhere in the document, I've inserted {REF Name_of_Proposal } at each particular point. I also bookmarked the name of the fields that would be populating the other fields as described above, in order to get it working.

The problem is that there's a page in the document where there's a table with two columns - milestone and date. As part of the automation process, I need to create a fillin box for both the milestone and date cells. This is no problem as it'd just work like all the other fillins and the information would populate the cell under "milestone" and the cell under "date". The thing is that there may be more than one row in the table, ie. a necessity to create more than one milestone and date pair. I thought about creating separate fillins labelled milestone1, date1, then milestone2, date2, but I don't know in advance how many of these rows there will be - in fact it will vary for each proposal. So I wondered if someone could tell me if there's any field codes I could use to allow the user to enter all the information needed in the one fillin box but have the ability to somehow separate the different milestone/date pairs that would then get populated consecutively into different table rows. Is this beyond the capabilities of field codes or would I have to use VBA to accomplish this? If VBA is required can someone tell me where I could get some code that would do this, as my knowledge of VBA is very limited.

My other issue is that I'd like a message box to pop-up before a particular fillin dialogue appears. This is because there's a section in the proposal where helpful instructions need to be given to the user before filling in the dialogue. Is there a way to incorporate such a pop-up and customise it so that it appears in the correct place? Would VBA be needed for this also?

Appreciate any help offered.
 
If Me.txtClient_Company_Name.Text = "" does not work, even though you know there is a object with that name...go through and check very very carefully the name syntax is precisely accurate.

I can not see how you can get that error unless there is something wrong with the name. "Variable not defined" means just that.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks for all the tips. For some reason I actually had to backspace through all the blank spaces before each line of code and then put in a hard return, then after doing that and changing all the null lines of code to:

Me.txtClient_Company_Name.Text = ""

It worked! So I'm one step forward, but with an initial problem. I noticed that when the information gets put into the bookmarks, and then I go back and do another test on the same text book / bookmark pair, when I press OK it just adds in the new entry before the old one. I mean it doesn't delete what was already there. So how would I set it up so that once everything's entered, the user could somehow call up the dialogue box again, enter new information and have these new values overwrite the old ones. Is this possible, and if so can you point me to the right code?

Thanks for the continued help.
 
Hi gwh2,

You can use code like this to update bookmarked text:
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
where you pass both the bookmark name (BmkNm) and text (NewTxt) to the macro. Note the in-built error checking.

Cheers

[MS MVP - Word]
 
Thanks for the code to update the bookmark.

I've been able to move forward but have since run into a coding problem trying to set up a wizard interface on the userform. I have the code below but it was originally written for a 3-page multi-page userform only, whereas I have about 11 pages. I was able to adapt the code a little bit by disabling all pages except for the first page but when it comes to coding the procedures for the "back" and "next" buttons I'm getting a bit lost. Because I have so many pages if I use the type of code used below I thought I'd end up with bloated code. Instead, I was thinking about declaring a variable called current page and having this somehow keep track of the current page in order to get it to work. Problem is I don't know how to adapt the code to do this.

I wondered if someone could give me a hand?


Code:
Private Sub UserForm_Initialize()

With MultiPage1
' The next 2 lines disable all pages except Page1.
.Pages(1).Enabled = False
.Pages(2).Enabled = False
.Pages(3).Enabled = False
.Pages(4).Enabled = False
.Pages(5).Enabled = False
.Pages(6).Enabled = False
.Pages(7).Enabled = False
.Pages(8).Enabled = False
.Pages(9).Enabled = False
.Pages(10).Enabled = False
' Make Page1 the active page.
.Value = 0
End With

' Set the caption on the CommandButtons.
cmdBack.Caption = "Back"
cmdBack.Enabled = False
cmdForward.Caption = "Next"

End Sub

' Procedure for the "Back" button
Private Sub cmdBack_Click()

' Declare variables

Dim currentPage As String
currentPage = "Pages(0)"

Select Case MultiPage1.Value
Case 1                                                                ' If Page2 is active...
With MultiPage1
.Pages(0).Enabled = True                                     ' Enable Page1.
.Value = MultiPage1.Value - 1                               ' Move back 1 page.
.Pages(1).Enabled = False                                     ' Disable Page2.
End With

cmdBack.Enabled = False                                      ' Disable Back button.

Case 2                                                                 ' If Page3 is active...
With MultiPage1
.Pages(1).Enabled = True                                     ' Enable Page2.
.Value = MultiPage1.Value - 1                               ' Move back 1 page.
.Pages(2).Enabled = False                                    ' Disable Page3.
cmdForward.Caption = "Next"
End With
End Select

End Sub
 
 ' Procedure for the "Next" button
Private Sub cmdForward_Click()

Select Case MultiPage1.Value
Case 0                                                                ' If Page1 is active...
With MultiPage1
.Value = MultiPage1.Value + 1                              ' Move forward 1 page.
.Pages(1).Enabled = True                                     ' Enable Page2.
.Pages(0).Enabled = False                                    ' Disable Page1.
End With
cmdBack.Enabled = True                                      ' Enable Back button.

Case 1                                                                ' If Page2 is active...

With MultiPage1
        .Value = MultiPage1.Value + 1                              ' Move forward 1 page.
        .Pages(2).Enabled = True                                     ' Enable Page3.
        .Pages(1).Enabled = False                                    ' Disable Page2.
End With

cmdForward.Caption = "Finish"                            ' Change Next button to Finish.

Case 2                                                                  ' If Page3 is active...
MsgBox "Finished!"                                                 ' User is Finished.
Unload Me                                                              ' Unload the UserForm.
End Select

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top