PDA

View Full Version : Change word in document depending on "x"



YellowLabPro
11-11-2008, 07:20 AM
I have a document that has a header in it. The document has one big table w/ questions in each row. On the first page there is a header, which one of the fields is for gender. If the term "he" is placed in the cell, then do nothing, all of the terms currently are "he", if the term "she" is placed in the header, then change all the terms of "he" to "she" throughout the document.
I only have experience w/ vba in excel, so I have no idea how to do this or where to place the code.
Can someone help w/ this?

Thanks,

Doug

ps. This sheet will be sent out to others to fill in, so the code will need to travel w/ the sheet.

lucas
11-11-2008, 10:16 AM
Hi Doug, there are several ways to do this.

Are you going to use formfields or a userform for data input?

For formfields check out this thread. I attached a file that Gerry provided for duplicating what is in one formfield throughout the document.

http://www.vbaexpress.com/forum/showthread.php?t=23383

The attachment is in post #3.

............................
Another way would be to have a userform open and when you put he or she in a textbox and submit it, the result could be returned to multiple bookmarks in the document.


It's really a matter of choice and I am not a Word person so I lean towards the userform and bookmarks.

Can you post the document?

YellowLabPro
11-11-2008, 10:31 AM
I am not sure lucas... <grin>, I have no idea. I have uploaded the file so you can see what I am working with.

I know absolutely nohthing about Word, or how to get code in it. The term in the header should be the trigger. But something I discovered when implementing this was that the user does not have access to the header in the document, so they cannot fill in the term female in the header... so I have ask how this can be accomplished or how to re-set up my document to allow for this interactivity between the recepient and the form.

So to recap... I have no idea, and the advice here from the board will influence the direction and two... how and where the code goes in Word.

thanks,

Doug

lucas
11-11-2008, 10:42 AM
So if the user puts female in the header you want it to reflect her in various places in the document.....is that correct?

lucas
11-11-2008, 11:13 AM
Doug, I just addressed the header part in this example. If this looks like it might work for you then it is easy to understand and uses bookmarks.

I don't fully understand what else you wish to do so if you could be specific it would help.

lucas
11-11-2008, 11:36 AM
Doug, I worked on the male/female thing. If you put male in the sex box you will see the word he appear in line 6 otherwise it will say she.

I'm just trying to head you in a direction Doug so if you see any problems, yell out.

lucas
11-11-2008, 11:43 AM
With a combobox for male/female selection.

added bookmark to line 10 so you should see he or she added to lines 6 and 10

YellowLabPro
11-11-2008, 02:55 PM
Hey Lucas,
Yes you are on the right track. I like both ways.... probably the second w/ the combo box.

There are four items I would like to ask you to address; 1) Goals is not something I want them to fill in here 2) your dog would be replaced w/ the dog's name from the header and each letter would be an upper case, so if someone entered mary ann in the header, the form would rewrite it to Mary Ann, 3) I would like the tab button or the enter button to move the cursor to the next field and then once all the fields are filled in, then enter or tab would place the data in the header; currently tab or enter jumps to the "continue" button, 4) How the heck do I find and read the code <grin>... I am in the dark here.

Mostly though, a very big thank you for setting this up and handling it on your own accord...

Thanks,

Doug

lucas
11-11-2008, 03:05 PM
Doug, the code is in the userform........in the vbe

this sub is really a function for handling the bookmarks.

Private Sub wb(ByVal BName As String, ByVal inhalt As String)
If ActiveDocument.Bookmarks.Exists(BName) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(BName).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add BName, r
Else
Debug.Print "Bookmark not found: " & BName
End If
End Sub


To view the bookmarks go to insert-bookmark

to change the order when you hit enter....in the userform select each textbox, combo, button and set its tabindex property.....

I don't understand question 2

YellowLabPro
11-12-2008, 04:39 AM
Lucas,
I made a couple of changes to reflect the direction of the final document. I copied the module from your document to this one, but the dialog box does not load upon opening, can you identify my err and explain what it is too please?

The previous post you posted you did not understand #2. In the document there is a phrase: "your dog" w/out quotes, I would like the code to identify "your dog" and replace it w/ the dog's name from the header. I might be able to accomplish this now, now that you have setup the framework, but since I cannot get the dialog box to load I cannot test.

Is it possible to put a button on the sheet like excel to load the dialog box in case the user wants to make a change?

The new document is attached.

Thanks,

Doug

YellowLabPro
11-12-2008, 05:12 AM
Lucas:
I went in and did some reading on bookmarks to see what the term describes and the way you are handling it.
I made a few changes, but still the form is not loading.

I have uploaded the new document. The other questions still apply... but may become more visible after you help me get the form to load and I can see what is and what is not working.

Thanks,

Doug

lucas
11-12-2008, 09:05 AM
Doug, the code to open the form when you run the file goes in the thisdocument module and is basically just a document open procedure.


Option Explicit
Private Sub Document_Open()
UserForm1.Show
End Sub

see picture at bottom of this post
As far as a menu, it is much easier to add a menu to Word than it is to Excel. Click here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm) for a flash presentation that shows how to do it step by step.

lucas
11-12-2008, 09:20 AM
Here's your file back. You have some problems in the userform but you seem to be working that out so I didn't mess with it.

menu added and on open event added.

YellowLabPro
11-13-2008, 03:47 AM
Okay... making some progress.
I understand now a little more; opening the dialog box, the command button, tab order and even an inkling regarding bookmarks. But I cannot get some things to work smoothly.

The dialog box; it opens and the cursor defaults to the second text box and not the first.

Bookmarks; I have set a couple, but rather than replacing it is adding... he turns into heshe rather than she.
Do I have to set a bookmark for each instance of the word?

Thanks for the help,

Doug

lucas
11-13-2008, 07:06 AM
Use setfocus to get it to start in the correct textbox:
Private Sub UserForm_Initialize()
TextBox2.SetFocus
With ComboBox1
.AddItem "Male"
.AddItem "Female"
End With
End Sub

remove the word he or she from the document and just insert the bookmark, evenly spaced between the other two words.

When you run the code, the correct word will be inserted.

YellowLabPro
11-13-2008, 10:10 AM
Okay, got the focus set and the proper tab order set, thanks.

I also was able to set the bookmark and replace the term. But, do I have to do it for each instance of the word, i.e. s1, s2, s3, s4 etc.... for every instance? This is what appears to be the case.

And a follow up question- can the form be reopened after it has been closed by the user w/out having to do so from w/in the code?

thanks,

doug

lucas
11-13-2008, 11:06 AM
do I have to do it for each instance of the word, i.e. s1, s2, s3, s4 etc.... for every instance?

I don't know of an easier way to do it Doug. Maybe Gerry or Malcolm will know of a method like find and replace that might work but it also might replace he with she in words like health, etc. so I'm not sure.

This seems to be the most reliable way to handle it and remember you will only have to set it up once and it will be ready for many uses.



can the form be reopened after it has been closed by the user w/out having to do so from w/in the code?



If you mean can you set it up to where the userform doens't open each time the file is opened, Sure, but you will have to add a menu button at the top of the page to run the macro.

just remove the on open procedure that is in the thisdocument module, or comment it out.

lucas
11-13-2008, 11:07 AM
see here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm) for how to set up your file menu item just to the right of help on the main menu.

YellowLabPro
11-13-2008, 11:40 AM
Hi Lucas,
thanks. Okay... I just wanted to make sure I was approaching your solution correctly and not missing something.

Second: I do want it to open each time... what I was thinking was how the user might need to open it if there was a typo and they wanted to edit something and not to have to open the vb editor. Something intutive and maybe on the page like a button on an excel sheet. Is this doable in word?

lucas
11-13-2008, 11:59 AM
I don't like the buttons on a page but you could. Did you check my link in post 18?

fumei
11-13-2008, 12:58 PM
"if there was a typo and they wanted to edit something and not to have to open the vb editor"

Having user open the VB Editor is NOT a good idea.

You can show the userform by anything that calls a procedure. This can be a keyboard shortcut (e.g. Alt-u), a bit of text on any toolbar (e.g. "Show Form"), a button on a toolbar, or a menu item. Your choice.

YellowLabPro
11-13-2008, 03:07 PM
Lucas, I did look at it and I agree w/ you. The button is not really feasible for the users of this sheet. I did not know if there was a way to make a button on the sheet that overlayed, again as in Excel.

But we made great strides with this so far, thank you very much. I will keep plugging away.

Cheers,

Doug

YellowLabPro
11-14-2008, 04:51 AM
Thanks fumei and lucas.
I assigned a button and a keyboard stroke for the user. Not ideal :-), but I put a message in a highly visible area to alert the user how and where to access.

Cheers,

Doug

TonyJollans
11-14-2008, 05:25 AM
OK, I'm late to the party on this one - and haven't read the whole thread properly - but I think you will find out how to do what you ask, here (http://gregmaxey.mvps.org/Repeating_Data.htm)