PDA

View Full Version : [SOLVED:] Copying Bookmarks content and pasting to UserForm



nmgmarques
02-13-2020, 10:27 AM
Hi all.

So I know you usually use a form to get input data to paste to a document, but I need to do the inverse. I have a Word document, restricted editing, with a few text form fields. Of these, 4 are bookmarked. I need to print these 4 fields instead of the entire document to create a kind of stub. Meaning I can hand the stub to someone and at a later date, if they give me the stub, I can refer back to the original document.

Is there a way, once I press an ActiveX Button, to use VBA to copy the contents of the bookmarked field and copy it to a UserForm and then print said UserForm? The button is so that I can select which documents I need to print out the stub.

Any help greatly appreciated.

gmaxey
02-13-2020, 10:42 AM
Lets say you have a dotx template file in the same folder as your main document named "Stub.dotx" In that template you have four bookmarks "A, B, C and D." In your main form you also have four bookmarks "A, B, C and D."

Using this code you can print a stub document containing the data in the main form bookmarks:


Option Explicit
Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.Bookmarks("A").Range.Text
txtB.Text = ActiveDocument.Bookmarks("B").Range.Text
txtC.Text = ActiveDocument.Bookmarks("C").Range.Text
txtD.Text = ActiveDocument.Bookmarks("D").Range.Text
lbl_Exit:
Exit Sub
End Sub

Private Sub CommandButton1_Click()
Dim oDoc As Document
Set oDoc = Documents.Add(ThisDocument.Path & "\Stub.dotx")
With oDoc
.Bookmarks("A").Range.Text = txtA.Text
.Bookmarks("B").Range.Text = txtB.Text
.Bookmarks("C").Range.Text = txtC.Text
.Bookmarks("D").Range.Text = txtD.Text
.PrintOut
.Close wdDoNotSaveChanges
End With
Unload Me
End Sub

nmgmarques
02-13-2020, 11:02 AM
Lets say you have a dotx template file in the same folder as your main document named "Stub.dotx" In that template you have four bookmarks "A, B, C and D." In your main form you also have four bookmarks "A, B, C and D."

Using this code you can print a stub document containing the data in the main form bookmarks:


Option Explicit
Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.Bookmarks("A").Range.Text
txtB.Text = ActiveDocument.Bookmarks("B").Range.Text
txtC.Text = ActiveDocument.Bookmarks("C").Range.Text
txtD.Text = ActiveDocument.Bookmarks("D").Range.Text
lbl_Exit:
Exit Sub
End Sub

Private Sub CommandButton1_Click()
Dim oDoc As Document
Set oDoc = Documents.Add(ThisDocument.Path & "\Stub.dotx")
With oDoc
.Bookmarks("A").Range.Text = txtA.Text
.Bookmarks("B").Range.Text = txtB.Text
.Bookmarks("C").Range.Text = txtC.Text
.Bookmarks("D").Range.Text = txtD.Text
.PrintOut
.Close wdDoNotSaveChanges
End With
Unload Me
End Sub
Thanks for taking the time to reply. I have tried the code but get a Variable not defined.
25977

My VBA skills are close to non existent and only as good as my Google prowess (which is also pretty abismal). I tried defining the variables in both subs by doing

Dim txtA As Variable
which of course didn't work.

gmaxey
02-13-2020, 11:42 AM
You need to add a userform to the main document. In that userform add four text fields and title them txtA, txtB, txtC and txtD. You also need a command button titled CommnadButton1

The code I sent you earlier goes in the UserForm module.

In your main document add a macro in a standard module:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub


Add a button on your QAT to call ScratchMacro

nmgmarques
02-13-2020, 12:18 PM
You need to add a userform to the main document. In that userform add four text fields and title them txtA, txtB, txtC and txtD. You also need a command button titled CommnadButton1

The code I sent you earlier goes in the UserForm module.

In your main document add a macro in a standard module:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub


Add a button on your QAT to call ScratchMacro
Ok. I am trying to follow you on this but I am a bit confused. So I have created the user form, and this is the userform code:
25979

Then I created a module (Module1) and pasted the ScratchMacro code.

However, I don't know how to call the ScratchMacro. I have the button, I can click view code. Do I paste the code here and rename the button to ScratchMacro?
25980

Sorry for the questions. I am pretty sure I am missing something. Do I have to create a second button, maybe? Since the first code seems to be linked to my current button.

gmaxey
02-13-2020, 12:23 PM
Don't use an activeX button in your document. Modify your Quick Access Toolbar and add a button that calls SratchMacro.

nmgmarques
02-13-2020, 12:50 PM
Don't use an activeX button in your document. Modify your Quick Access Toolbar and add a button that calls SratchMacro.
Thanks for your patience. I have been playing around a bit with your take on this, and I managed to get the form to pop up and populate and print using the first portion of your text and using the aciveX button. So right now I have no module. Just the UserForm1.
25981

Then assigning this code to the button, results in the form popping up and printing (when not commented out, for sure).
25982
Now all I would need is to get rid of the "FORMTEXT " from each field and I'd be set. Any ideas?

gmaxey
02-13-2020, 01:11 PM
Well a bookmark that wraps a formfield is going to return that. So:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
MsgBox Replace(ActiveDocument.Bookmarks("Description").Range.Text, "FORMTEXT ", "")
MsgBox ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub

nmgmarques
02-13-2020, 02:00 PM
Well a bookmark that wraps a formfield is going to return that. So:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
MsgBox Replace(ActiveDocument.Bookmarks("Description").Range.Text, "FORMTEXT ", "")
MsgBox ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub

Thanks.

After tinkering around I have it good enough for my requirements. So in the end, the UserForm1 code is as follows:

Option ExplicitPrivate Sub UserForm_Initialize()


txtA.Text = Replace(ActiveDocument.Bookmarks("Number").Range.Text, "FORMTEXT ", "")
txtA.Text = ActiveDocument.FormFields.Item("Number").Result
txtB.Text = Replace(ActiveDocument.Bookmarks("Year").Range.Text, "FORMTEXT ", "")
txtB.Text = ActiveDocument.FormFields.Item("Year").Result
txtC.Text = Replace(ActiveDocument.Bookmarks("Author").Range.Text, "FORMTEXT ", "")
txtC.Text = ActiveDocument.FormFields.Item("Author").Result
txtD.Text = Replace(ActiveDocument.Bookmarks("Description").Range.Text, "FORMTEXT ", "")
txtD.Text = ActiveDocument.FormFields.Item("Description").Result


lbl_Exit:
Exit Sub
End Sub

And the button code:

Private Sub CommandButton1_Click()Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Set oFrm = Nothing
UserForm1.PrintForm
lbl_Exit:
Exit Sub
End Sub

gmaxey
02-13-2020, 03:04 PM
Well you are repeating yourself four times. You can use either of the two methods I showed you but you don't need both.

Use:
txtA.Text = ActiveDocument.FormFields.Item("Number").Result

gmaxey
02-13-2020, 03:15 PM
So I am assuming that despite the advice otherwise, you are using a ActiveX button to execute your code. Okay. In your ActiveX code:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.PrintForm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub

In your userform:


Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.FormFields.Item("Number").Result
txtB.Text = ActiveDocument.FormFields.Item("Year").Result
txtC.Text = ActiveDocument.FormFields.Item("Author").Result
txtD.Text = ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub

nmgmarques
02-13-2020, 04:58 PM
So I am assuming that despite the advice otherwise, you are using a ActiveX button to execute your code. Okay. In your ActiveX code:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.PrintForm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub

In your userform:


Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.FormFields.Item("Number").Result
txtB.Text = ActiveDocument.FormFields.Item("Year").Result
txtC.Text = ActiveDocument.FormFields.Item("Author").Result
txtD.Text = ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub
Sorry, believe me that if I was able to troubleshoot and doo any better, I would. I have always been a tinker kind of guy, and pulling at the threads of your code gave me a pretty workable result. Nonetheless, I do appreciate you still trying to help me. I will give this a try for sure.

If all else fails, at least what I have now is "workable". I still have an issue with a phantom userform1 that doesn't seem to want to go away after printing and only disappears when I exit the word document, but other than that, the current solution isn't half bad.

I have now integrated your code into the file. I had to change ScratchMacro to CommandButton1_Click. It works great, but I still get the aforementioned phantom window.

In any case, honestly, I do very much appreciate your input. Please don't think I mean any disrespect. It's just that my limited knowledge of VBA tends to make me choose the simplest path, even when it's not elegant. Hell, most of the files I have cobbled together VBA scripts to suite my needs would probably make an experienced programmer cry in anger. But I am always learning something with all this info and through trial and error.

This is the file as it currently stands after your feedback:
https://www.dropbox.com/scl/fi/q3p0riowmttizew6e65n2/AMB-007-Ficha-controlo-Rev07.docm?dl=0&rlkey=so2l2e0ycrmo37reb3cua618k

And I created a video with the phantom form that lingers on top of everything making an appearance at second 44 on the top left corner.
https://youtu.be/zTxJNPKjky8

I made the video before your current changes, though the issue still happens.

Edit: using your ScratchMacro code and replacing my print button code with it made the phantom window go away. So now everything is working great!

Thanks for your help. No way I would have gotten this done in such short time without you.

gmaxey
02-13-2020, 06:54 PM
In your userform, use this (there is no need to print the command buttons)


Option Explicit
Private Sub CommandButton2_Click()
CommandButton2.Visible = False
CommandButton3.Visible = False
PrintForm
Hide
lbl_Exit:
Exit Sub
End Sub
Private Sub CommandButton3_Click()
Hide
lbl_Exit:
Exit Sub
End Sub

Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.FormFields.Item("Number").Result
txtB.Text = ActiveDocument.FormFields.Item("Year").Result
txtC.Text = ActiveDocument.FormFields.Item("Author").Result
txtD.Text = ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub

In your document module, use this:


Private Sub CommandButton1_Click()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Unload oFrm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub

nmgmarques
02-17-2020, 03:04 AM
In your userform, use this (there is no need to print the command buttons)


Option Explicit
Private Sub CommandButton2_Click()
CommandButton2.Visible = False
CommandButton3.Visible = False
PrintForm
Hide
lbl_Exit:
Exit Sub
End Sub
Private Sub CommandButton3_Click()
Hide
lbl_Exit:
Exit Sub
End Sub

Private Sub UserForm_Initialize()
txtA.Text = ActiveDocument.FormFields.Item("Number").Result
txtB.Text = ActiveDocument.FormFields.Item("Year").Result
txtC.Text = ActiveDocument.FormFields.Item("Author").Result
txtD.Text = ActiveDocument.FormFields.Item("Description").Result
lbl_Exit:
Exit Sub
End Sub

In your document module, use this:


Private Sub CommandButton1_Click()
'A basic Word macro coded by Greg Maxey
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Unload oFrm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub
Thanks for that. It's printing fine now, and without the buttons. I appreciate your efforts.