Consulting

Results 1 to 19 of 19

Thread: Userform/bookmarks problem. !st try not going well

  1. #1
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location

    Userform/bookmarks problem. !st try not going well

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    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?

  5. #5
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    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


    Untitled.jpg

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

    Untitled2.jpg
    Attached Images Attached Images

  7. #7
    Post the template.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    Thanks for looking
    Account Form2.docmAccount Form2.docm
    Took me a while to figure out how to post the template. Fingers crossed its as an attachment to this post

  9. #9
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    Account Form 3.docmHi 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!)

    Untitled3.jpg


    Cheers

    Colin

  11. #11
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    As Paul has identified the document we need to see is the BUPA Account Form template, which is where the macros and form are.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  14. #14
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    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
    HELP.docm

  15. #15
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  16. #16
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    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

  17. #17
    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.
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  18. #18
    VBAX Regular
    Joined
    Jul 2018
    Posts
    8
    Location
    Thanks Graham,

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

    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?

  19. #19
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •