PDA

View Full Version : macro to display userform, insert data at bookmarks



Anotherwayne
04-24-2023, 09:22 AM
I'm on the struggle bus. This is my first macro. I haven't coded since I made my highschool sweetheart a website back in '02. I am trying to automate a legal document. Essentially, I want the end user to open the template and have it automatically bring up a userform that inserts the answers into the document at the bookmarks. I had it all good, except that it didn't autolaunch, but if I started the macro it would do what I wanted. I also tried to figure out a way to preserve the template and have it open a new document for the end user to save. I then managed to goober it all up. Now when I open the template, it opens a new document which has the text, but not the bookmarks. So the macro can't input the userform data at the bookmarks because they do not exist.

I am a noob, so please pretend that I will have no idea what you're talking about unless you explain it like I've never coded before.

Here is my beginner level code:
This document:

Sub AutoNew()
UserForm1.Show
End Sub

Userform1:

Private Sub CommandButton1_Click()


Dim BM1 As Range
Set BM1 = ActiveDocument.Bookmarks ("BM1").Range
BM1.Text = Me.TextBox1.Value


(there are 23 more of these)


UserForm1.Hide
Me.Repaint

End Sub

Help me... I have spent 10 hours trying to write automate a single template. Save me.

Dave
04-24-2023, 12:05 PM
Hi Anotherwayne and Welcome to this forum. I'm fairly sure that you want to ensure the "Template" document and the document generated by the "Template" both have the bookmarks already placed in the document. ie. the .dot, or dotx document, must have the bookmarks already placed in it when it is converted to a template doc. To create and then use templates, here's a link that looks like it might help. HTH. Dave
Word Templates - Code - VBA (bettersolutions.com) (https://bettersolutions.com/word/templates/vba-code.htm)

Aussiebear
04-24-2023, 01:21 PM
As Dave has indicated, welcome to the VBAX forum Anotherwayne. We wrap our code with tags here to improve readability. To do so simply highlight your submitted code and click the # icon in the bar above when posting.