PDA

View Full Version : Solved: Control which pages of doc are shown



CreganTur
04-10-2008, 06:27 AM
I've got a letter I'm automating with some VBA- it's a regular word doc.

The letter is 6 pages- each page is a different language saying the same thing- and the only thing that has to be changed is the name of the recipient (which I've already done). The letter can be mailed to a Primary customer only, or a Primary and Secondary customer.

The word doc currently has 12 pages in it- the Primary customer letter and the Secondary customer letter.

When the doc is opened, a MsgBox appears asking the User if this is a Primary customer only letter (vbYesNo). Here's what I want to accomplish:

is it possible with VBA to make it so that if the User says vbYes (this is a Primary customer only letter) that only the first 6 pages are shown, and that only the first 6 pages can be printed?

If they respond vbNo (there is a Primary and Secondary customer) then nothing would need to be done- all 12 pages (both letters) would be shown as normal.

Thanks for the help!

lucas
04-10-2008, 06:53 AM
There is a simple way to have all of your choices in one template file and through a selection on a userform you can decide what is kept in the newly created document from the template.

This thread discusses some other things but starting at post#11 Gerry starts a discussion of this method. See the last 2 paragraphs of post 11 in this thread:
http://vbaexpress.com/forum/showthread.php?t=13543

Download the file from that post and read later posts in the thread that apply to using the method described by Gerry in the last paragraph of post 11

post back here if you think it might work for your problem but you are having trouble understanding it or implementing it.

CreganTur
04-10-2008, 08:11 AM
post back here if you think it might work for your problem but you are having trouble understanding it or implementing it.

I think I see what he's doing in that example- he's deleting the range of the bookmarks that were not selected by the User- which makes sense.

I'm just having trouble getting this to work for me. I highlighted all of the text for the Secondary customer's part of the letter, and set it to a bookmark ("CmkrLetter"). I've tried a few things, but instead of deleting the text of the bookmark, the bookmark itself is deleted... not sure what to do.

lucas
04-10-2008, 10:24 AM
can you post the document?

hit post reply at the bottom left of the last post and then scroll down and look for the button that says "manage attachements"

fumei
04-10-2008, 10:54 AM
Are any parts of your bookmarks overlapping?

It should work. What exactly is your code?

CreganTur
04-10-2008, 11:10 AM
Sorry, I can't post the document: proprietary.

I'm not using your code exactly, Gerry, just the theory behind it. Here's the code:

Private Sub Document_Open()
Dim strMkr As String
Dim strCmkr As String
Dim strCmkrBkmrk As String
strBookmark = "CmkrLetter"
'Determine which pages need to be shown
If MsgBox("Is this a Mkr only letter?", vbYesNo, "Mkr Only?") = vbYes Then
'Enter Mkr name
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
'populate text fields with name
ActiveDocument.FormFields("Text1").Result = strMkr
ActiveDocument.FormFields("Text2").Result = strMkr
ActiveDocument.FormFields("Text3").Result = strMkr
ActiveDocument.FormFields("Text4").Result = strMkr
ActiveDocument.FormFields("Text5").Result = strMkr
ActiveDocument.FormFields("Text6").Result = strMkr
'ActiveDocument.Bookmarks(strBookmark).Select
'Selection.Delete

Else
'Enter both Mkr and Cmkr name
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
strCmkr = InputBox("Please enter the Cmkr's name", "Cmkr's Name")
'populate text fields with Mkr name
ActiveDocument.FormFields("Text1").Result = strMkr
ActiveDocument.FormFields("Text2").Result = strMkr
ActiveDocument.FormFields("Text3").Result = strMkr
ActiveDocument.FormFields("Text4").Result = strMkr
ActiveDocument.FormFields("Text5").Result = strMkr
ActiveDocument.FormFields("Text6").Result = strMkr
'populate text fields with Cmkr name
ActiveDocument.FormFields("Text7").Result = strCmkr
ActiveDocument.FormFields("Text8").Result = strCmkr
ActiveDocument.FormFields("Text9").Result = strCmkr
ActiveDocument.FormFields("Text10").Result = strCmkr
ActiveDocument.FormFields("Text11").Result = strCmkr
ActiveDocument.FormFields("Text12").Result = strCmkr
End If

'MsgBox "Mkr Name: " & strMkr & vbCrLf & "Cmkr Name: " & strCmkr, vbOKOnly, "Results"
End Sub

I did get the deletion to work by selecting the bookmark and then using the Selection.Delete command. The only problem with that is that the deleted section remains deleted even if I don't save the document on close.

This document is saves a a normal word doc.

fumei
04-10-2008, 11:59 AM
1. "I'm not using your code exactly, Gerry, just the theory behind it. "

No, not really. Perhaps you are not quite grasping the theory fully.

2. It would be easier to both read your code, and for the code to execute if you used objects, for example a FormField object, as well as arrays. So instead of:
ActiveDocument.FormFields("Text1").Result = strMkr
ActiveDocument.FormFields("Text2").Result = strMkr
ActiveDocument.FormFields("Text3").Result = strMkr
ActiveDocument.FormFields("Text4").Result = strMkr
ActiveDocument.FormFields("Text5").Result = strMkr
ActiveDocument.FormFields("Text6").Result = strMkr
You could use:



Dim FirstBunch()
Dim var, var2

Set DocFF = ActiveDocument.FormFields

FirstBunch = Array("Text1", "Text2", _
"Text3", "Text4", "Text5", "Text6")

For var = 0 To UBound(FirstBunch)
DocFF(FirstBunch(var)).Result = strMkr
Next


3. Are you using Option Explicit? You use:
strBookmark = "CmkrLetter"

but never declare strBookmark...or use it.

4. "The only problem with that is that the deleted section remains deleted even if I don't save the document on close."

Yes, of course it does...BECAUSE "This document is saves a a normal word doc."

I would suggest using a template.

5. You never mentioned that the chunks contained formfields. This is (or can be) significant. Why are you using formfields? They are for user input.

6. Using the default names (Text1, Text2, Text3....etc) is NOT a good idea.

CreganTur
04-10-2008, 12:24 PM
1. "I'm not using your code exactly, Gerry, just the theory behind it. "

No, not really. Perhaps you are not quite grasping the theory fully.

I'm only referring to the idea of using bookmarks to control chunks of text- and I'm not grasping it fully; I'm new to VBA and very new to Word VBA


2. It would be easier to both read your code, and for the code to execute if you used objects, for example a FormField object, as well as arrays.

yes, that looks a little easier- I wasn't aware of how to do that, so thanks.


3. Are you using Option Explicit?

No, I'm not using Option Explicit.


I would suggest using a template.

I'll look into creating a template for this and see how it workd.


5. You never mentioned that the chunks contained formfields. This is (or can be) significant. Why are you using formfields? They are for user input.

I used them because they seemed like the easiest way for me to enter in the name from the input message. Also it was similar to setting the value of a textbox on a form in Access, seemed like an easy way to accomplish what I want.


6. Using the default names (Text1, Text2, Text3....etc) is NOT a good idea.

I agree with you there- I was just too lazy to change them since all this work is a proof-of-concept. I'd change them for the finished product.

fumei
04-10-2008, 12:37 PM
It is never a good idea to start off lazy. Although I do understand the proof-of-concept idea. Fair enough.

Use Option Explicit. I can not stress that enough.

"I'm only referring to the idea of using bookmarks to control chunks of text"

Well, all I can say is that you do not seem to be grasping the theory of using bookmarks to control chunks of text. I know using Ranges in Word is not the most intuitive thing.

Re: formfields. "I used them because they seemed like the easiest way for me to enter in the name from the input message."

I can see how how you may think that if you equate it with "setting the value of a textbox on a form in Access"

Actually....a textbox in Access (or anything else) is the same...they are for user input.

If they are not being used for user input, then they are being used improperly.

Textboxes (definitely) and formfields (mostly) are for user input. NOT for display of text.

In any case, unless you can be a bit more specific I do not see how I can help further. You absolutely can control chunks of text via bookmarks. If you are having a problem with doing that, then you have to describe it better.

mdmackillop
04-10-2008, 01:19 PM
Rather than deleting text, why not change the bookmarked text font effect to Hidden. It's a lot more forgiving!

CreganTur
04-10-2008, 01:41 PM
Rather than deleting text, why not change the bookmarked text font effect to Hidden. It's a lot more forgiving!

That makes everything MUCH easier!:thumb

Maybe one day I'll be smart enough to use Gerry's idea, but for now I'll stick with this:

Code to Hide text (reduces 12 pages to 6)((partial code)):
If MsgBox("Is this a Mkr only letter?", vbYesNo, "Mkr Only?") = vbYes Then
strMkrOnly = 1
ActiveDocument.Bookmarks(strBookmark).Select
Selection.Font.Hidden = True

And I restore the document on Close with:
Dim strBookmark As String
strBookmark = "CmkrLetter"
ActiveDocument.Bookmarks(strBookmark).Select
If Selection.Font.Hidden = True Then
Selection.Font.Hidden = False
End If

lucas
04-10-2008, 01:53 PM
I see he has already responded to you Malcolm.

I see where cregan went wrong trying to use Gerry's code. Here's what he said after he tried it.


I'm just having trouble getting this to work for me. I highlighted all of the text for the Secondary customer's part of the letter, and set it to a bookmark ("CmkrLetter"). I've tried a few things, but instead of deleting the text of the bookmark, the bookmark itself is deleted... not sure what to do.

note the name of the bookmark he is using. CmkrLetter

now look at Gerry's code that is looking for which bookmark to not delete.

If objBookmark.Name <> _
Right(ctl.Name, Len(ctl.Name) - 3) Then
objBookmark.Range.Delete

It is looking for a bookmark named the same as the control minus the first 3 letters.

Gerry's controls name's are: optLetter1 optLetter2 optLetter3
Gerry's bookmarks names are: Letter1 Letter2 and letter3

so of course Cregans bookmark was deleted, it didn't match any of the radio buttons that were selected.......no matter which one it was.

to use this as a bookmark: CmkrLetter
You also need to rename one of your controls to optCmkrLetter

actually I think your control could be named 123CmkrLetter or ABCCmkrLetter as long as there are 3 charactors in front of what matches the bookmark name.

I hope that makes sense and Gerry and Malcolm will tell me if I'm off base here.

lucas
04-10-2008, 02:09 PM
If objBookmark.Name <> _
ctl.Name Then
objBookmark.Range.Delete

would work where your control and your bookmark would have the same name. The problem with this is that it doesn't allow you to give meaninful names to your option buttons. That is the reason that Gerry is using the length(len) method to not include the first 3 charactors in the match with the bookmark name.

CreganTur
04-10-2008, 03:31 PM
I didn't use any of gerry's code for my project. After I saw how he did what he did, I realized that I could use bookmarks to determine which letters need to be shown.

I only have 2 options:
1) Mkr letter: pages 1 - 6
2) Cmkr letter: pages 1-12

So I started with a vbYesNo MsgBox (yes = Mkr letter; no = Cmkr letter), and used an If expression to evaluate what happens. Under the Yes section originally I was trying to delete the bookmark that controlled pages 7 - 12. This deleted the bookmark reference, but not the text that made up the bookmark. I did find a way to delete the text by selecting the bookmark and then deleting the selection, but then I was posed with the problem of how to restore the deleted text, and recreate the bookmark (bookmark index was deleted with text).

I'm using md's idea of hiding the text now, and it's working better for me- mainly because it's much easier to restore the document by unhiding the hidden text (if MKr letter) using the Close event.

fumei
04-11-2008, 10:19 AM
"restore the document"

This is the result of using documents, rather than templates. if you used a template file properly, you would NEVER have to "restore" anything.

"I was trying to delete the bookmark that controlled pages 7 - 12."

Bookmarks do not "control" anything.

As you never actually posted any code, perhaps you did just that. Deleted the bookmark. To delete the text, you have to delete the bookmark Range.

This deletes both the bookmark AND anything in the bookmark.

Bookmarks are just named Ranges, they have a Start and an End. That's it. If you just delete the bookmark, then all you delete is those numbers. If you delete the bookmark Range then you delete everything between those numbers, AND that includes.....the bookmark.

lucas
04-11-2008, 10:28 AM
Poster is missing the point Gerry. If he had read my explaination at post 12 as to why his bookmark was deleted when he tried to use your code he would understand a little better.

Then we could have shown him how to convert the document to a .dot or template file and change the code to show the form from document open to document new.....then he could use it correctly.....

Sometimes a quick, simple fix is all they are interested in I guess.

mdmackillop
04-11-2008, 10:30 AM
Sometimes a quick, simple fix is all they are interested in I guess.

Yeah!:duel:

CreganTur
04-11-2008, 11:21 AM
First of all, I want to learn and I'm trying to learn. I appreciate that you both have a lot more knowledge and experience than I do, and I also appreciate the help I get.

Second, for the second time, I'm not using, nor have I ever used, gerry's code. Maybe I should, maybe I can adapt it to what I want to do and have it work a lot better.

Here's the exact code I'm using, and it works perfectly for me. It's not elegant, and it shows how new I am to this, but it works. Explinations will follow:

Private Sub Document_Open()
Dim strMkr As String
Dim strCmkr As String
Dim strCmkrBkmrk As String
Dim strBookmark As String
Dim strAnalyst As String
strBookmark = "CmkrLetter"
If MsgBox("Is this a Mkr only letter?", vbYesNo, "Mkr Only?") = vbYes Then
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
strAnalyst = InputBox("Please enter the Repo Analyst's name", "Analyst Name")
ActiveDocument.FormFields("Mkr1").Result = strMkr
ActiveDocument.FormFields("Mkr2").Result = strMkr
ActiveDocument.FormFields("Mkr3").Result = strMkr
ActiveDocument.FormFields("Mkr4").Result = strMkr
ActiveDocument.FormFields("Mkr5").Result = strMkr
ActiveDocument.FormFields("Mkr6").Result = strMkr
'Insert Analyst's Name
ActiveDocument.FormFields("Analyst1").Result = strAnalyst
ActiveDocument.FormFields("Analyst2").Result = strAnalyst
ActiveDocument.FormFields("Analyst3").Result = strAnalyst
ActiveDocument.FormFields("Analyst4").Result = strAnalyst
ActiveDocument.FormFields("Analyst5").Result = strAnalyst
ActiveDocument.FormFields("Analyst6").Result = strAnalyst
ActiveDocument.Bookmarks(strBookmark).Select
Selection.Font.Hidden = True
Else
ActiveDocument.Bookmarks(strBookmark).Select
If Selection.Font.Hidden = True Then
Selection.Font.Hidden = False
End If
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
strCmkr = InputBox("Please enter the Cmkr's name", "Cmkr's Name")
strAnalyst = InputBox("Please enter the Repo Analyst's name", "Analyst Name")
ActiveDocument.FormFields("Mkr1").Result = strMkr
ActiveDocument.FormFields("Mkr2").Result = strMkr
ActiveDocument.FormFields("Mkr3").Result = strMkr
ActiveDocument.FormFields("Mkr4").Result = strMkr
ActiveDocument.FormFields("Mkr5").Result = strMkr
ActiveDocument.FormFields("Mkr6").Result = strMkr
ActiveDocument.FormFields("Cmkr1").Result = strCmkr
ActiveDocument.FormFields("Cmkr2").Result = strCmkr
ActiveDocument.FormFields("Cmkr3").Result = strCmkr
ActiveDocument.FormFields("Cmkr4").Result = strCmkr
ActiveDocument.FormFields("Cmkr5").Result = strCmkr
ActiveDocument.FormFields("Cmkr6").Result = strCmkr
'Insert Analyst's Name
ActiveDocument.FormFields("Analyst1").Result = strAnalyst
ActiveDocument.FormFields("Analyst2").Result = strAnalyst
ActiveDocument.FormFields("Analyst3").Result = strAnalyst
ActiveDocument.FormFields("Analyst4").Result = strAnalyst
ActiveDocument.FormFields("Analyst5").Result = strAnalyst
ActiveDocument.FormFields("Analyst6").Result = strAnalyst
ActiveDocument.FormFields("Analyst7").Result = strAnalyst
ActiveDocument.FormFields("Analyst8").Result = strAnalyst
ActiveDocument.FormFields("Analyst9").Result = strAnalyst
ActiveDocument.FormFields("Analyst10").Result = strAnalyst
ActiveDocument.FormFields("Analyst11").Result = strAnalyst
ActiveDocument.FormFields("Analyst12").Result = strAnalyst
End If

Selection.GoTo What:=wdGoToBookmark, Name:="LetterTop"

MsgBox "Letter successfully generated" & vbCrLf & vbCrLf & _
"Please review the letter before printing", vbOKOnly, "Letter Generated"
End Sub
Private Sub Document_Close()
Dim strBookmark As String
strBookmark = "CmkrLetter"
ActiveDocument.FormFields("Mkr1").Result = ""
ActiveDocument.FormFields("Mkr2").Result = ""
ActiveDocument.FormFields("Mkr3").Result = ""
ActiveDocument.FormFields("Mkr4").Result = ""
ActiveDocument.FormFields("Mkr5").Result = ""
ActiveDocument.FormFields("Mkr6").Result = ""
ActiveDocument.FormFields("Cmkr1").Result = ""
ActiveDocument.FormFields("Cmkr2").Result = ""
ActiveDocument.FormFields("Cmkr3").Result = ""
ActiveDocument.FormFields("Cmkr4").Result = ""
ActiveDocument.FormFields("Cmkr5").Result = ""
ActiveDocument.FormFields("Cmkr6").Result = ""
'Clear Analyst's Name
ActiveDocument.FormFields("Analyst1").Result = ""
ActiveDocument.FormFields("Analyst2").Result = ""
ActiveDocument.FormFields("Analyst3").Result = ""
ActiveDocument.FormFields("Analyst4").Result = ""
ActiveDocument.FormFields("Analyst5").Result = ""
ActiveDocument.FormFields("Analyst6").Result = ""
ActiveDocument.FormFields("Analyst7").Result = ""
ActiveDocument.FormFields("Analyst8").Result = ""
ActiveDocument.FormFields("Analyst9").Result = ""
ActiveDocument.FormFields("Analyst10").Result = ""
ActiveDocument.FormFields("Analyst11").Result = ""
ActiveDocument.FormFields("Analyst12").Result = ""
ActiveDocument.Bookmarks(strBookmark).Select
If Selection.Font.Hidden = "9999999" Then
Selection.Font.Hidden = "0000000"
End If
'set save flag as true- disables save prompt on close
ActiveDocument.Saved = True
End Sub

FormFields: yes, I'm using Form Fields- I know how you feel about them gerry, but they're the only option I'm aware of right now that lets me do what I want to do. Also, since the VBA code only allows User Input through InputBox at Open event, if they make a mistake it leaves the fields open so that they can correct the mistake. If there's a better option, please explain it.

This section from Close event:
If Selection.Font.Hidden = "9999999" Then
Selection.Font.Hidden = "0000000"
End If
I changed the value from True/False to numerics because, for some reason, the True/False wasn't working. When I did a step through I saw that the Selection.Font.Hidden was returning the numeric values, and when I set them it made everything work.

Templates: using a template would work easier, I agree completely, but I don't know how to make them easily accessible for my Users.

If you want to critique what I've done, I would appreciate it- I'd be a fool to turn away help- if not, that's okay too.

NinjaEdit: included example document.

mdmackillop
04-11-2008, 12:02 PM
I'll not enter the FormField discussion, but if you find yourself writing dozens of similar lines of code, pass the routine to another sub. If you have to make a change, it's much simpler than changing 50+ lines


Private Sub Document_Open()
Dim strMkr As String
Dim strCmkr As String
Dim strCmkrBkmrk As String
Dim strBookmark As String
Dim strAnalyst As String
strBookmark = "CmkrLetter"
If MsgBox("Is this a Mkr only letter?", vbYesNo, "Mkr Only?") = vbYes Then
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
strAnalyst = InputBox("Please enter the Repo Analyst's name", "Analyst Name")
UpdateFF "Mkr", 6, strMkr
'Insert Analyst's Name
UpdateFF "Analyst", 6, strAnalyst
ActiveDocument.Bookmarks(strBookmark).Select
Selection.Font.Hidden = True
Else
ActiveDocument.Bookmarks(strBookmark).Select
If Selection.Font.Hidden = True Then
Selection.Font.Hidden = False
End If
strMkr = InputBox("Please enter the Mkr's name", "Mkr's Name")
strCmkr = InputBox("Please enter the Cmkr's name", "Cmkr's Name")
strAnalyst = InputBox("Please enter the Repo Analyst's name", "Analyst Name")
UpdateFF "Mkr", 6, strMkr
UpdateFF "Cmkr", 6, strCmkr
'Insert Analyst's Name
UpdateFF "Analyst", 12, strAnalyst

End If

Selection.GoTo What:=wdGoToBookmark, Name:="LetterTop"

MsgBox "Letter successfully generated" & vbCrLf & vbCrLf & _
"Please review the letter before printing", vbOKOnly, "Letter Generated"
End Sub

Sub UpdateFF(Fld As String, Cnt As Long, Res As String)
For i = 1 To Cnt
ActiveDocument.FormFields(Fld & i).Result = Res
Next
End Sub

fumei
04-11-2008, 12:12 PM
I am not sure what you think I think about formfields. I like formfields, and use them all the time. I think they are great.

I would like to help, or suggest, things to further your interest - " I want to learn and I'm trying to learn"

However, as you have mostly ignored my earlier suggestions, it seems rather pointless to suggest anything more. If what you have is working for you, then...shrug...it is working for you. And that is good. I certainly am not going to argue against something that is working.

Perhaps, when you are ready to consider making it work better, you can revive the question.

BTW: your code (which I take it as a clearing of all the formfields to blank):
ActiveDocument.FormFields("Mkr1").Result = ""
ActiveDocument.FormFields("Mkr2").Result = ""
ActiveDocument.FormFields("Mkr3").Result = ""
ActiveDocument.FormFields("Mkr4").Result = ""
ActiveDocument.FormFields("Mkr5").Result = ""
ActiveDocument.FormFields("Mkr6").Result = ""
ActiveDocument.FormFields("Cmkr1").Result = ""
ActiveDocument.FormFields("Cmkr2").Result = ""
ActiveDocument.FormFields("Cmkr3").Result = ""
ActiveDocument.FormFields("Cmkr4").Result = ""
ActiveDocument.FormFields("Cmkr5").Result = ""
ActiveDocument.FormFields("Cmkr6").Result = ""
'Clear Analyst's Name
ActiveDocument.FormFields("Analyst1").Result = ""
ActiveDocument.FormFields("Analyst2").Result = ""
ActiveDocument.FormFields("Analyst3").Result = ""
ActiveDocument.FormFields("Analyst4").Result = ""
ActiveDocument.FormFields("Analyst5").Result = ""
ActiveDocument.FormFields("Analyst6").Result = ""
ActiveDocument.FormFields("Analyst7").Result = ""
ActiveDocument.FormFields("Analyst8").Result = ""
ActiveDocument.FormFields("Analyst9").Result = ""
ActiveDocument.FormFields("Analyst10").Result = ""
ActiveDocument.FormFields("Analyst11").Result = ""
ActiveDocument.FormFields("Analyst12").Result = ""


uses 24 separate parsed and executed instructions. It could be done - as I have suggested - as:

For Each oFF In DocFF
oFF.Result = ""
Next



which is ONE instruction.

Done.

mdmackillop
04-11-2008, 12:14 PM
Here's one resource on templates. There are a lot more out there.
http://addbalance.com/word/templates.htm

CreganTur
04-11-2008, 12:28 PM
MD: that's awesome, thanks. I didn't know you could update FormFields like that.

gerry: In your example
For Each oFF In DocFF
oFF.Result = ""
Next
I can see that DocFF should be: Set DocFF = ActiveDocument.FormFields
but I can't figure out what you mean oFF to be set to.

fumei
04-11-2008, 12:34 PM
Using ActiveDocument.FormFields(Fld & i) will work as long as the formfield names are Name+number (Analyst1, Analyst2....)

Which is quite legitmate naming.

You can also use objects more. Say bookmark the chunk of the document that contains all the Mkr formfields. Name that bookmark MkrFF.

Sub UpDateMkr( strIn As String)
Dim oFF As FormField
Dim r As Range
Set r = ActiveDocument.Bookmarks("Mkr").Range
For Each oFF In r.FormFields
oFF.Result = strIn
Next
End Sub

Sub whatever()
Call UpDateMkr ("")
End Sub


which would make them all blank. Or,

Sub NotBlank()
Call UpDateMkr (InputBox("Give me something for the Mkr fields."))
End Sub



OR, to be more specific to your code.....

Sub BlahBlah()
Call UpDateMkr (InputBox("Please enter the Mkr's name", _
"Mkr's Name"))
End Sub

mdmackillop
04-11-2008, 12:39 PM
Any control with a name or an index can be maipulated by code. Gerry's For Each, in addition to clearing fields, might be used to mass fill them
eg

Sub test()
UpdateFF "Anal", stranalyst
End Sub

Sub UpdateFF(Fld As String, Res As String)
Dim ff As FormField
For Each ff In ActiveDocument.FormFields
If InStr(1, ff.Name, Fld) > 0 = Fld Then
ff.Result = Res
End If
Next
End Sub

fumei
04-11-2008, 12:42 PM
Sorry.

Dim DocFF As FormFields

Notice the plural.

Set DocFF = ActiveDocument.FormFields

This makes the DocFF object the entire document formfield Collection - i.e. ALL of them

Dim oFF As FormField

Notice the singular. This is make a formfield object.

So.....

For Each oFF In DocFF

means for each formfield object in the Collection.

oFF does not have to be set/named as you are processing all of them.

fumei
04-11-2008, 12:45 PM
"Gerry's For Each, in addition to clearing fields, might be used to mass fill them"

Shipping passing.... here.

Yes. This occurs in the Sub BlahBlah above. Except I fine tuned it to fill only the formfields in a specific range (i.e. a bookmark), rather than the entire document.

It is VERY flexible. You can set it up for just about anything.

mdmackillop
04-11-2008, 12:52 PM
Personally, if I were asking the user to input more than one item of data, I would create a UserForm containing a textbox, (listbox or combobox if apopropriate) for each of the fields to be inserted. Looks more "professional" and IMO is the better way to do it.

CreganTur
04-11-2008, 12:53 PM
Wow...information overload- it's going to take a while to sift through all this stuff you guys have provided, but I think it will help a lot!

gerry: I'm trying to use your
Dim oFF As FormField
Set DocFF = ActiveDocument.FormFields
For Each oFF In DocFF
oFF.Result = ""
Next
but I keep getting a '4120' Bad Parameter error. When I step through DocFF never has anything assigned to it, and oFF gets changed from a value of '70' to 'Nothing'- could this have something to do with the fact that it's a part of a Close event?

fumei
04-14-2008, 07:16 AM
1. I agree with Malcolm. A userform would be more professional.

2. Yes, it likely does have to do with it being in the Close event.

CreganTur
04-15-2008, 12:40 PM
I've made a lot of changes and improvements based on the suggestions I've gotten here, and they really do seem to be making a big improvement.

-userform for User to select letter type and to provide data entry
-using more optimal code to populate formfields on document with data
-still using Hidden Font option to control which letter is shown

I ended up using md's idea for clearing out formfields at the Close event- since that event seemed to muck up gerry's "for each formfield in formflield collection" code.

It's not a template yet- I'm still researching templates, but for the most part this project is done.

Thank you guys very much for your help:thumb