PDA

View Full Version : VBA Very Newbie - updating bookmarks



elsteshepard
11-07-2008, 05:39 AM
Hi,
I am very new to VBA - in fact, i don't know any coding.
I am creating a template, with a userform to add text to the template. It's a document that sometimes repeats itself, so things like 'Project Manager name' are repeated several times.
I have so far managed to create a basic form, some action buttons, and it works....sort of.

The bookmarks in the template get updated as per the form, however, the repeating fields (i've used {REF "bookmark"} do not update.

I have put some code in the action button to say update document, but it doesn't work.

In fact, even if i view the document that i've created and try to update the field by F9 it still doesn't update????

Any ideas?

Thanks

CreganTur
11-07-2008, 06:31 AM
Take a look at the attached example. It shows a few different things, but one part shows how to fill in multiple bookmarks with the same data. Be sure to look at the UpdateFF Sub, and how it's called from the main Sub.

The example shows to name bookmarks that contain the same data with the same name, just tack on an incrementing number to the end... kind of like: Name1, Name2, Name3, etc.

HTH:thumb


Edit by Lucas: Attachment removed at Randy's request..

lucas
11-07-2008, 09:00 AM
Attached is another example of how to update formfields with the same info. Gerry provided this and I use it for this purpose.

The first field is a text formfield from the forms toolbar.

fumei
11-07-2008, 10:11 AM
elsteshepard, how EXACTLY you do this depends on your requirements.

If a formfield is set for Calculate on exit, then any field with the same name will automatically update when the formfield is exited.

Yes, you can use { REF bookmark }, but you can also do it as Steve (lucas) suggests. In many ways, it is better...well easier.

Cregan's userform (in Letter Example) is pretty slick. I like it. However, the doc file itself has some odd things in it. I am not sure what he is doing with the ASK and FILL-IN fields. I also think it is making things more complex than needed to have the same information in bookmarks of different names - i.e. Mkr1, Mkr2, Mkr3 etc.

Of course different bookmarks have to have different names, true. But I fail to see why the different bookmarks are there. Or rather, I do see why, but I am not sure the design is appropriate.

Further, there appears to be multiple letters in the document. This may, or may not, be what you are trying to do.

On my computer, one of the "letters" consists of many ASCII characters (ASCII 63 - "?")...I don't what they are doing there. Also I do not know why, as they are ASCII 63, they do not show as "?", but as little squares.

Finally, and CreganTur, please, I am not trying to be critical or negative...it is NOT (generally speaking) a good idea to have two procedures (Subs) with the same name in the same project.

There is a

Sub UpdateFF(Fld As String, Cnt As Long, Res As String)

in the ThisDocument module, and a

Sub UpdateFF(Fld As String, Cnt As Long, Res As String)

in the userform module.

Technically, VBA does allow this as procedures in a userform module are isolated within the Scope of the userform. Still, first of all, it is not needed to have two - if the procedure is in a standard module it can be called from anywhere (including the userform). Second, using the same name for procedures can, potentially, bring up the dreaded run-time error of "ambiguous name".

EricFletcher
11-09-2008, 03:44 PM
On my computer, one of the "letters" consists of many ASCII characters (ASCII 63 - "?")...I don't what they are doing there. Also I do not know why, as they are ASCII 63, they do not show as "?", but as little squares.
Interesting... on my screen, the other letters show up in Chinese (Taiwan) using the PMingLiU font; Chinese (PRC) using the Batang font; and then in Spanish and another language using "normal" fonts. I noticed these fonts had been installed on my system some time ago after an update, but hadn't had any reason to use them. I assume you don't have the fonts installed, so see just the ASCII characters instead.

CreganTur: I hope you are aware that the letters Gerry refers to are included in the example you posted -- and that the full content is visible if Word's "Show all" is turned on (because the font attribute is set to "hidden"). If you did not intend for that content to be seen, you might want to ask to have the file removed from this thread.

elsteshepard
11-10-2008, 03:27 AM
thanks for all the help

I had a look at Lucas' as it looked like the easiest. Still not sure why mine doesn't work. I noticed that the word doc is protected. Does that make a difference?

Requirements wise - basically it's a Specification document. So it has some user specific fields - free text and it as some Project specific fields that we require. Project name, document ref, Project Manager, etc. etc.

So the user uses the form to complete as much as we need using a userform, then they change some of the context of the document after it's been created.

So for example, Project Manager - the field {pm} is repeated a few times throughout the document, and is captured once using the form. But after being created from the template, it does not duplicate - using the {REF pm} field.

I will have a look at the other form posted too....

Update:
I have looked at the other example - completely over my head!
Also, I should mention that i have saved the document as a template - so it doesn't get changed. I presume that's the best practice?

fumei
11-10-2008, 12:00 PM
If the document is protected, yes that can make a difference.

"But after being created from the template, it does not duplicate - using the {REF pm} field."

You need to update the fields. To do so with the doucument protected for forms, you need to unprotect it first, update the other fields, and re-protect it. Demo attached. Click "Demo Me" on the top toolbar.

fumei
11-10-2008, 12:03 PM
BTW: I am a little confused. Are the bookmarks you mention actually the bookmarks of formfields? This is very important.

Or are they "normal" bookmarks?

elsteshepard
11-11-2008, 05:16 AM
I think they're normal bookmarks, see description below.

To create the template first I;

used insert>bookmarks... then typed in a name bkProjectManager

Then if that is used again in the template i've used {REF bkProjectManager}

The field bkProjectManager is itself derived from a userform textbox (txtProjectManager) - created with VBA.

When the userform is completed the bookmark field in the document is added with some VBA, but the {REF} is not updated. Even though i have included the documentupdate code.

In fact, in the produced document, if I select the field {REF} and do a manual update F9, it doesn't do anuything.

Am I getting fields/bookmarks/userform textboxes confused???

elsteshepard
11-12-2008, 02:37 AM
I actually looked at the problem again, and decided to cheat and do what another poster said.

Each time Project Manager appears in the template i've entered a new bookmark pm1, pm2, pm3 etc. and just referred to that in the code. All gets updated as I wanted.

Thanks for all the help