PDA

View Full Version : Inserting text to bookmarks...without shifting text



igotgame
01-13-2016, 07:25 AM
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.

igotgame
01-13-2016, 08:15 AM
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.

gmayor
01-13-2016, 10:44 PM
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.

igotgame
01-14-2016, 07:16 AM
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

AnalystGuy
01-14-2016, 09:36 AM
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.

bbqq021
01-14-2016, 05:12 PM
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

gmayor
01-15-2016, 07:02 AM
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.

AnalystGuy
01-15-2016, 07:12 AM
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.

bbqq021
01-17-2016, 04:17 PM
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

gmayor
01-17-2016, 10:32 PM
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

gmaxey
01-18-2016, 05:44 AM
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_pages/content_control_tools.html