PDA

View Full Version : [SOLVED:] Userform/bookmarks problem. !st try not going well



JJPHG
07-29-2018, 01:15 PM
Apologies if I'm cross-posting (https://social.msdn.microsoft.com) but I'm not getting any help from the other forums to date.


Sure you've heard this many time before - I'm struggling with my first userform.

I've created the form, no problem, its a very simple textbox only form (no options/drop-downs etc) with the hope of populating the bookmarks in a word template with the entries in the userform. However after a couple of days fidling around, trying to find tutorials that can explain to me what to do (so many start off fine but then throw in terms/code that I have no idea what they mean and there is no explaination).

Anyway, what I've done so far is as follows

Private Sub Cancel_Click()
ActiveDocument.Close SaveChanges:=False

End Sub

Private Sub UserForm_Click()
AccountForm.Show
End Sub

Private Sub Submit_Click()

With ActiveDocument
.Bookmarks("AccountDate").Range.Text = AccountForm.txtAccountDate.Value
.Bookmarks("Ref").Range.Text = AccountForm.txtRef.Value
.Bookmarks("PatientAddress").Range.Text = AccountForm.txtPatientAddress.Value
.Bookmarks("Patientdob").Range.Text = AccountForm.txtPatientdob.Value
.Bookmarks("PatientID").Range.Text = AccountForm.txtPatientID.Value
.Bookmarks("PatientMedicareNo").Range.Text = AccountForm.txtPatientMedicareNo.Value
.Bookmarks("PatientName").Range.Text = AccountForm.txtPatientName.Value
.Bookmarks("ItemNo1").Range.Text = AccountForm.txtItemNo1.Value
.Bookmarks("Description1").Range.Text = AccountForm.txtDescription1.Value
.Bookmarks("NoofPat1").Range.Text = AccountForm.txtNoofPat1.Value
.Bookmarks("Date1").Range.Text = AccountForm.txtDate1.Value
.Bookmarks("Charge1").Range.Text = AccountForm.txtCharge1.Value
.Bookmarks("NoAcc").Range.Text = AccountForm.txtNoAcc.Value
End With

Unload Me


End Sub

Now when I click submit in the form when firing up the template nothing happens. The form just sits there and I have to force it to close.

I have come accross this code but I've no idea where I would put it and how I would get it to run/try it after clicking on submit.

Private Sub FillBD(strBMName As String, strValue As String)
Dim orng As Range
With ActiveDocument
Application.ScreenUpdating = False
On Error GoTo lbl_Exit
Set orng = .Bookmarks(strBMName).Range
orng.Text = strValue
orng.Bookmarks.Add strBMName
End With
Application.ScreenUpdating = True
lbl_Exit:
Set orng = Nothing

Exit Sub

Can anybody help me please?

Thanks

macropod
07-29-2018, 03:06 PM
Also cross-posted at: http://www.msofficeforums.com/word-vba/39899-struggling-1st-userform-bookmarks.html

Paul_Hossler
07-29-2018, 03:11 PM
I used CODE tags around your macros -- you can use the[#] icon to insert them next time

I usually have a Standard Module macro load and show the form ...






Sub ShowMyForm()
Load AccountForm


AccountForm.Show


End Sub




… and button events to do what they gotta do, and then hide the form




Private Sub Cancel_Click()


ActiveDocument.Close SaveChanges:=False
AccountForm.Hide
Unload AccouuntForm

End Sub












Private Sub Submit_Click()



With ActiveDocument


.Bookmarks("AccountDate").Range.Text = AccountForm.txtAccountDate.Value


.Bookmarks("Ref").Range.Text = AccountForm.txtRef.Value


.Bookmarks("PatientAddress").Range.Text = AccountForm.txtPatientAddress.Value


.Bookmarks("Patientdob").Range.Text = AccountForm.txtPatientdob.Value


.Bookmarks("PatientID").Range.Text = AccountForm.txtPatientID.Value


.Bookmarks("PatientMedicareNo").Range.Text = AccountForm.txtPatientMedicareNo.Value


.Bookmarks("PatientName").Range.Text = AccountForm.txtPatientName.Value


.Bookmarks("ItemNo1").Range.Text = AccountForm.txtItemNo1.Value


.Bookmarks("Description1").Range.Text = AccountForm.txtDescription1.Value


.Bookmarks("NoofPat1").Range.Text = AccountForm.txtNoofPat1.Value


.Bookmarks("Date1").Range.Text = AccountForm.txtDate1.Value


.Bookmarks("Charge1").Range.Text = AccountForm.txtCharge1.Value


.Bookmarks("NoAcc").Range.Text = AccountForm.txtNoAcc.Value


End With


AccountForm.Hide
Unload AccountForm



End Sub

JJPHG
07-29-2018, 04:45 PM
Thanks Paul,

I now get a run-time error 424 - oblect required at the firast bookmark. I assume I'm missing something on the userform?

gmayor
07-29-2018, 08:40 PM
Using the alternative code, that you posted, the errors should be trapped. Note that the fieldnames and the bookmark names should exist for it to work.


Private Sub Submit_Click()
FillBM "AccountDate", txtAccountDate.Text
FillBM "Ref", txtRef.Text
'etc
Unload Me
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
Dim orng As Range
With ActiveDocument
Application.ScreenUpdating = False
On Error GoTo lbl_Exit
Set orng = .Bookmarks(strBMName).Range
orng.Text = strValue
orng.Bookmarks.Add strBMName
End With
Application.ScreenUpdating = True
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

JJPHG
07-29-2018, 09:23 PM
Thanks

Obviously I'm missing something fundamental here as still getting the 424 error. I've checked the active doc and there are bookmarks there


22639

and the textbox appear to be labelled ok so what I'm I missing?

22640

gmayor
07-29-2018, 10:12 PM
Post the template.

JJPHG
07-29-2018, 10:36 PM
Thanks for looking
2264122641
Took me a while to figure out how to post the template. Fingers crossed its as an attachment to this post : pray2:

gmayor
07-30-2018, 03:35 AM
The document you have attached does not contain macros or userform. It does have bookmarks. It also has formfields which require the document to be locked, preventing editing of the document and probably accounts for the error. You would be better with content controls which don't require the document to be locked.

The macros and userform need to be moved to the document from your normal template to make this viable.

JJPHG
07-30-2018, 04:14 AM
22644Hi Graham,

Thanks loads for trying to help me.

Don't know why the doc doesn't have the userform or macro in it. I had to amend my original to remove sensitive info and probably messed things up when saving it. I've attached another version. If its still not there and in the normal template how do I go about moving them?

Here is a screen shot hopefully showng that the userform is with the doc (although its not firing up now when I load the template!)

22643


Cheers

Colin

macropod
07-30-2018, 04:51 AM
Your screenshot shows that your userform & code are in the attached template, not in the document itself - which may as well be saved in the docx or doc format. FWIW, you can update formfields in a protected document with code like:

ActiveDocument.Bookmarks("AccountDate").Range.Fields(1).Result.Text = AccountForm.txtAccountDate.Value

gmayor
07-30-2018, 05:03 AM
As Paul has identified the document we need to see is the BUPA Account Form template, which is where the macros and form are.

gmaxey
07-30-2018, 10:06 AM
Considering you are using a .docm extension file, why are you bothering with bookmarks or formfields in the in the first place. Using a content control as the document text range is much easier. See attached.

JJPHG
07-30-2018, 12:20 PM
ok

Big thanks to everybody for trying to help me. Don't understand the difference between dotx and docm - it's 15 years since I messed around with templates and userforms and in those days it was just a simple dot - or at least that's all I remember.

Anyway, I have tried to rebuild from scratch and the attached is my latest version. Hopefully the userform is part of this file. As it stands the userform doesn't fire up or autnew a doc when I open the template!!

I have added the control content lines but as the form doesn't show (and don't know why) I can't tell if this works or not.

Cheers

Colin
22650

gmaxey
07-30-2018, 05:14 PM
dotx and dotm introduced with Word 2007 replaces dot. dotx can't contain macros. dotm can.

If you want the form to load when a the document is opened or when a new document is created from the template then you need an AutoOpen or AutoNew event procedure.

The attached shows how to write userform content to a document CC and document bookmark. You need more than code to write to a CC. You need the CCs in the document.

JJPHG
08-01-2018, 11:29 PM
Thanks Greg

I'm guessing that copy and paste your code into my original docm isn't the correct thing to do as when I did that nothing happens - no userform at startup. If I manually run the userform nothing happens when I click submit. I can see your docm works a treat - that's exactly what I want. But transfering it and it all falls over again :(

gmayor
08-02-2018, 05:43 AM
There are no content controls in your document, so it would be a good start to include them, and you have form field checkboxes rather than content controls also and of course your code doesn't work.

I have modified the code so that it works, (and replaced the check boxes) but you will have to add the rest of the content controls and the code that filles them following the examples shown.

https://www.gmayor.com/insert_content_control_addin.htm will help with the controls

Create a new document from the template to display the form.

JJPHG
08-03-2018, 04:17 AM
Thanks Graham,

Took me a while but finally worked out what you were trying to explain and figured it out and got it working :yes

Only thing is I can't personally see the point or advantage of using content control in this situation. I would be just as quick to have a blank template and tab/click to the areas I want to fill in and type away. Am I missing something?

gmaxey
08-03-2018, 05:39 AM
You started this tread looking for assistance populating document bookmarks with data entered on a userform. I suggested writing the data to content controls vice bookmarks and provided an example of how to do either.

The advantages of CCs over a bookmarks are that CCs can be defined as editable regions in protected documents and CCs can be mapped to a customXML part which is useful if the data entered in one CC needs to be repeated in other locations in the document.

As for this situation, the use of the CCs as Graham provided does make it possible to forego the userform altogether. You couldn't do that with plain bookmarks.