PDA

View Full Version : Solved: Populate userform textboxes from bookmarks at startup



Saabeirik
10-29-2012, 12:51 AM
Hi!


I'm making a word template with a userform that pops up when opening the document.

I write what I want into the textboxes, and this is in turn put into bookmarks in my document. Sometimes I want to edit some of the userform info, and I call my userform with a keyboard shortcut to update some of the textboxes. I then have some code to populate the textboxes with the bookmarks in the text, so that I don't have to write everything again, just edit what I want.

All of this works just fine when I'm in the template file. The problem occurs when I start a new document with this template. The userform is never populated with the bookmarks from this document, so every time I need to update information in the userform, I have to type in all of information again.

I guess the problem is that the userform is populated with the bookmark-text from my template file. Is this possible to solve in an easy way so that the userform is always populated with the bookmark text from the actual document?

The code I use for populating the userform is this, written in my template.
Private Sub UserForm_Activate()
inndata_ks.Firmanavn.Text = ThisDocument.Bookmarks("firmanavn").Range.Text
inndata_ks.utarbeidet_dato.Text = ThisDocument.Bookmarks("Utarbeidet_dato").Range.Text
inndata_ks.revisjon.Text = ThisDocument.Bookmarks("revisjon").Range.Text

End Sub

gmaxey
10-29-2012, 04:58 AM
ThisDocument refers the the whatever contains the running code, not particularily the actual document you are working in. See:

http://gregmaxey.mvps.org/word_tip_pages/interactive_userforms.html

Saabeirik
10-29-2012, 06:44 AM
Thank you Greg. I have to admit I didn't take the time to study your site close enough, as I'm currently at work and need to produce something else other than just this productivity enhancing userform template.

But I've bookmarked it earlier, for further study! ;-)

I did although see a line of code that gave me an idea I decided to test, and it worked!

All I did with the code above was to change ThisDocument.Bookmarks to ActiveDocument.Bookmarks. In case anyone else need the same code.