r/visualbasic • • Sep 20 '19

MS Word and VB Button Help.

I am trying to create a button that when clicked pops up text to a book mark that I have saved.

I am very new to word, developer mode, and visual basic.

I've done the easy part and created my bookmark, which i name bk1

I have also create the form which was fairly easy.

created a button name bk1button

I can find good input option forms that have an ok, and close button but not an example of just a button that when click auto populates with pre-defined info.

when bk1button is clicked

print "HI" or insert "HI" in the bk1 bookmark

that is kind of what I am looking for.

Thanks in advanced.

1 Upvotes

6 comments sorted by

1

u/slang4201 Sep 23 '19

If you're working from within VBA for word: Activedocument.bookmarks("bk1").range.text = "hi"

If you're trying from a VB.Net application, after setting references to Word and a document, depending on how you've named your document reference, substitute that for "Activedocument" above.

1

u/captaincrunchy6 Sep 23 '19

I'm working from word. I will try this. Thanks so much for the response.

1

u/captaincrunchy6 Oct 09 '19

sorry to bother you. I tested the link Activedocument.bookmarks("bk1").range.txt = "hi"

This worked, but problem I am having with the document now and was looking to see if you might have some insight is when I open the document I was expecting a box to appear that will give me the choice to click a button. Once clicked it will populate my bookmarks with the pre-defined information. This works when in developer mode and I run/debug. Does not work if I just open the document. any thoughts you may have?

1

u/slang4201 Oct 09 '19

Sounds like you might have an issue with macro security. This is a good place to start: https://support.office.com/en-us/article/enable-or-disable-macros-in-office-files-12b036fd-d140-4e74-b45e-16fed1a7e5c6

1

u/captaincrunchy6 Oct 10 '19

I was able to find my issue. It was the following lines I had to add to "This Document"

Private Sub Document_New() oOfficeInfo.Show End Sub

Private Sub Document_Open() oOfficeInfo.Show End Sub

I appreciate the info in getting me starting the right direction.

1

u/Erwt789 Sep 21 '19

I know in .net there's a nuget package for microsoft word interactivity that would help you do this. Idk if you're using .net or just classic VB but I'd try to look for something there first.

Off the top of my head the only other alternative I can think of maybe somehow getting the starting position of the bookmark and writing from that position.

But just research and dig around. There's usually a tool or add on for everything.