# Solved: VBA Word - Bookmarks to textbox in userform



## Lussemul (Aug 24, 2011)

Hello out there!

I really hope, that someone out there can help me out. Im working in Word, where I have created a document with bookmarks. I have also created a userform, that opens with the document. What I would like is the textboxes in the userform to be filled in with the bookmarks from the word document.

I have been able to get the info from the bookmarks and paste it into a mgsbox, but nok into e.g. TextBox1.

My code for this is:
Private Sub Document_Open()
UserForm1.Show
Dim strBookmark As String
If ActiveDocument.Bookmarks.Exists("test") = True Then
strBookmark = ActiveDocument.Bookmarks("test").Range.Text
MsgBox strBookmark
End If
End Sub

With this, the userform opens and the text in my bookmark in the document called "test" sis showing in a MsgBox. But, what code do I write to put in the text from "test" into Userform1.TextBox1 ?? I have spend two days figuring this out - without any luck 

Kind regards 
Lussemul


----------



## DataBase (Jan 15, 2004)

Can you attached your word documents please?


----------



## Lussemul (Aug 24, 2011)

Hi Database - nice to get a reply from you 

Narh, it's a bit difficult to attach the document, since it is a work document with sensitive information. 

But the document contains bookmarks - one of them is called "test". And when I run the code with the MsgBox strBookmark line (instead of something else that sends the text to userform1.textbox1) it's correctly showing the text from the bookmark in the Messagebox. So the connection between the Word document and the code is ok - but not the connection to the userform.

I hope you understand my English and attempt to explain. If you need the document to figure out what to do, maybe I can create a test-page with the same bookmarks and where there is no sensitive information in. But don't know if this is neccesary? There is no locked cells in the document and no protection.... 

Does this help?


----------



## DataBase (Jan 15, 2004)

Hi Lussemul i am glad i am able to at least try and help 

I understand that you cannot and should not attached sensitive data, but it would be much easier to understand your problem if i had the document infront of me. As you suggested, create another document with random data and attach that. iv got some time free at work  so will try work on it.


----------



## Lussemul (Aug 24, 2011)

Hi Database!

Im glad you'll try to look into my document - I've almost taken my hair out in frustration with this code 

I have attached the document here, deleted almost everything except the code, a userform and the bookmarks. When you open the document, the userform will open. When closing the userform at X, the mgsbox will appear, with the text from the Bookmark called "Test". The MgsBox shall not appear in the final document, it is just a test from me, to see if I got the bookmark selected and was able to work with the bookmark. Now the question is, how to I get the text from the Bookmark into userform1.textbox1 ? So when the user opens the document, there will be text in the textboxes....

I hope I make my self clear in English 

Kind regards,
Lussemul


----------



## DataBase (Jan 15, 2004)

Ok here it is, i think this is what you wanted?

Now when you open the document, the form loads and the firstbox has the text from the bookmark in it.

Hope this is what you were looking for? let me know.

thanks,


----------



## Lussemul (Aug 24, 2011)

YOU ARE MY HERO!

Thanks a lot Database, I don't know what to do without you. Sending you a lot of thoughts, and you'll be in my pray to night. No really, you made my day!!!


----------



## Lussemul (Aug 24, 2011)

Is this a forum, where I get you points?


----------



## DataBase (Jan 15, 2004)

Hi im glad i could help 

Im not sure about points and to be honest knowing that i could help is enough for me. i love programming and i love challenges so it was my pleasure 

im sure many other users would have been able to assist you too.

if you need any other help let us know.


----------



## Lussemul (Aug 24, 2011)

Im just so pleased and happy. And now I have many hours of finishing the forms and document with bookmarks. Eventhough I now know who can help me, I will not stock you . 

Impressed that you love programming... I only love it, when it works 

Have a great day


----------



## Lussemul (Aug 24, 2011)

Hi again - you didn't get rid of me .

A little question... When we take the text from the bookmark into the textbox1, then the bookmark dissapear. So it works the first time, but when I add new text into the bookmark (or change the text in textbox1), it goes fine into the document. But then the bookmark is gone, and I cannot use the form again. Is there a way, where I can add a bookmark to the same place, when I click a button to add text to the document?

Kind regards, and sorry to ask again...
Lussemul


----------



## DataBase (Jan 15, 2004)

No problem, ask as many questions as you need. 

My first question is, why are you using bookmarks? 

you could very easily have a list of data that you can read. is the bookmarks essential?


----------



## Lussemul (Aug 24, 2011)

Just wrote several pages, that it removed before posting :-(

Well.... Hi Database. It's a long story and I'll try to explain:
Im using the bookmarks in lack of something else. There is going to be a template for my users, where they can add text in the textboxes - placing the text at the specific bookmarks in the document. Then they save as a normal document, but often they need to change or add information to the document - therefore I needed your help with extracting the text from my bookmarks to the textboxes. 

But, when extracting text, the bookmarks dissapear. I know there is a way of creating bookmarks from VBA (activedocument.bookmarks.add range:=selection.range....) But it doesn't work, cause where to select? And there is perhaps 50 bookmarks....

Erh, does it make any sence? 

Kind regards
Charlotte


----------

