Consulting

Results 1 to 11 of 11

Thread: Inserting text to bookmarks...without shifting text

  1. #1

    Inserting text to bookmarks...without shifting text

    Guys I am struggling with something. I am trying to insert text to a bookmark via VBA, which I do all the time, but for some reason in this word document I cannot insert the text without it shifting all the other text over.

    I have tried the following:
    - Checking/Unchecking overtype
    - Manually taking insert on/off
    - Using the ruler and adding tab stops for each underline spot
    - Inserting form fields into the document


    How can I have it just write above the line where the bookmark is located without shifting all the other text?

    Crazy thing is I have another document, where the text doesn't shift, but I can't stop it from happening in the current document I am using.

    I've attached a sample page of the document for you to see.
    Attached Files Attached Files

  2. #2
    Just for more clarification. Here is a snippet from a word document I have that works perfectly. I just can't tell how it's setup so that you can type above the line and text doesn't shift at all.

    The section under "To Buyer:" is exactly what I am looking for. It doesn't appear to be a table either so I am not sure how to do this.
    Attached Files Attached Files

  3. #3
    The bookmarks are point locations. Inevitably if you write text to the bookmarks the text will push the following text to the right to accommodate it.
    The second document has a pair of legacy form fields, and the macro doesn't work because at least one of the named bookmarks is not present.
    You could call the FillBM function - http://www.gmayor.com/useful_vba_functions.htm to fill an existing bookmarks (and it will not crash if there is no matching bookmark).
    Bookmark the whole of the area to be filled, but there will still be movement unless you use e.g. a (borderless) table to lay out the form.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Thanks for the response gmayor. Yea I started realizing that yesterday so I wrote a work around. Kind of messy, but it works.

    Here is a snippet in case anyone ever needs it. This will type the information needed, determine how long it is, highlight was what typed, underline it, move back to the end of what was typed, then delete that number of spaces it added to the end. This essentially makes it look like it was typed on the line and nothing was shifted (although the underline was 100% match up with the underscore).

    Added the range declarations so you can see what's needed there and added the Screenupdating so people don't see all this crap going on while it's happening, they just see it filled in.
    Dim prng as Range
    Set prng = ActiveDocument.Range
    
    Application.ScreenUpdating = False
    
    'Seller Name
    ActiveDocument.Bookmarks("SellerName").Select
    Selection.TypeText SellerName
    prng.Start = prng.Bookmarks("SellerName").Range.Start
    EndLength = Len(SellerName)
    prng.End = (prng.Start + EndLength)
    prng.Select
    Selection.Font.Underline = wdUnderlineSingle
    prng.Start = prng.End
    prng.Select
    For X = 1 To EndLength
        Selection.Delete
    Next X
    
    Application.ScreenUpdating = True

  5. #5
    What is the benefit of using Bookmarks rather than richtext? Why not just use RichText controls and refer to them with the code .SelectContentControlsByTitle? I was having issues like you were with Bookmarks so switched to richtext and haven't had the problem since.

  6. #6
    VBAX Regular
    Joined
    Feb 2013
    Location
    Bangkok
    Posts
    9
    Location
    I have the same question.... sort of. I have written a code for a userform which will send text from the form to bookmarks in the document. This all works okay however when I want to protect the document or protect section 1, I get the error since the code is unable to modify the bookmark in the protected section. I believe you can edit content control in a protected section however I am not familiar with the syntax for this. Can someone point me in the right direction? Sorry I can't post my code, company security system is fully encrypted and doesn't allow copy / paste.

    Thanks

    Will

  7. #7
    If you want to write to a named content control them you would use code similar to

    Dim occ As ContentControl
    Set occ = ActiveDocument.SelectContentControlsByTitle("NameofControl").Item(1)
    occ.Range.Text = "Text to write"
    You can of course use code to unlock the form to write to bookmarks in a locked form, but to fill a formfield in a protected form you can call it by name also e.g.
    Dim ofld As FormField
        Set ofld = ActiveDocument.FormFields("name")
        ofld.Result = "Text to write"
    This doesn't require the form to be unlocked.
    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
    Quote Originally Posted by bbqq021 View Post
    I have the same question.... sort of. I have written a code for a userform which will send text from the form to bookmarks in the document. This all works okay however when I want to protect the document or protect section 1, I get the error since the code is unable to modify the bookmark in the protected section. I believe you can edit content control in a protected section however I am not familiar with the syntax for this. Can someone point me in the right direction? Sorry I can't post my code, company security system is fully encrypted and doesn't allow copy / paste.

    Thanks
    Will
    Again, just use RichText and you can check the option to have your rich text not deleted. Use the code

    .SelectContentControlsByTitle("FirstName").Item(1).Range.Text = Me.TextBox2
    where "FirstName" is the name of the RichText box and Me.Textbox2 is the TextBox name in the userform.

    To add RichText to your document:

    1)Go to developer tab
    2) Highlight the word you want to be changed by using the Userform
    3) Click design mode
    4) Right click your RichText Control and click on Properties
    5) Change the title to the name you want to refer to it by
    6) Check the box Content Control cannot be deleted and click OK

    Now use the line of code I provided and just replace the references.

  9. #9
    VBAX Regular
    Joined
    Feb 2013
    Location
    Bangkok
    Posts
    9
    Location
    Thanks guys, since I posted and as Graham suggests I have un-protectforforms the first section and then re-protected it once I have inserted the text from the userform. I prefer this option since I also have tables in this section which I don't want changing. Content control doesn't give me this kind of control. Having done this I have stumbled across another problem. I have 3 sections in my document, section 1 is the first page which include the title page and is the one I want to protect. Section 2 is for the body and section 3 for the appendices. I want the user to be able to insert text into Sections 2 and 3 however when I protectforforms section 1 the cross references and bookmarks are greyed out for sections 2 and 3. This is not going to work for me since I want the end user of this template to write the document and use cross references for headings, tables, figures etc.

    Anyway around this?

    Thanks

    Will

  10. #10
    You need to ensure only Section 1 is protected e.g.

    Dim bProtected As Boolean
        'Unprotect the file
        If Not ActiveDocument.ProtectionType = wdNoProtection Then
            bProtected = True
            ActiveDocument.Unprotect Password:=""
        End If
        
        'Do stuff
        
        'Reprotect the document.
        If bProtected = True Then
            ActiveDocument.Sections(1).ProtectedForForms = True
            ActiveDocument.Sections(2).ProtectedForForms = False
            ActiveDocument.Sections(3).ProtectedForForms = False
            ActiveDocument.Protect _
                    Type:=wdAllowOnlyFormFields, _
                    NoReset:=True, _
                    Password:=""
        End If
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Instead of protecting for forms you can use content controls and protect with Restrict Editing - No Changes and add editors to the CCs you want to allow changes in.

    My CC Tools Add-In makes adding editors very easy. http://gregmaxey.mvps.org/word_tip_p...rol_tools.html
    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
  •