Consulting

Results 1 to 8 of 8

Thread: How to overtype text at a bookmark in Word?

  1. #1
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    5
    Location

    How to overtype text at a bookmark in Word?

    Hi,

    I would like to insert some text in a Word document, at a bookmark, but have the text overwrite what is already there. I am using the following code snippet:

    Set objWord = CreateObject("Word.Application")
    objWord.ActiveDocument.Bookmarks("BookmarkName").Select
    objWord.Options.Overtype = True
    objWord.Selection.Text = "Some text"

    The line “objWord.Options.Overtype = True” doesn’t appear to do anything… the text just gets inserted at the bookmark. Can anybody please suggest a solution? I searched the web and toyed in VBA but can’t see where to go from here.

    Thanks.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,354
    Location
    Define a range and set it = the bm range
    Define the range text
    Recreate the BM.

    [VBA]Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    Dim oRng As Word.Range
    With ActiveDocument
    Set oRng = .Bookmarks("TestBM").Range
    oRng.Text = "Whatever"
    .Bookmarks.Add "TestBM", oRng
    End With
    End Sub
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    5
    Location
    Thanks for your reply, Greg. I copied the code inside of the sub definition, pasted it into mine and modified the bookmark name. I am not familiar with ranges and so am assuming that this is all that needs to be done. It seems straight forward.

    Sadly, when I run my code I still don’t get the text to overwrite existing characters.

  4. #4
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    5
    Location
    Greg,

    After some searching on the net I found that by adding two lines of code to your example makes things work:

    Set oRng = .Bookmarks("TestBM").Range
    oRng.start = oRng.start
    oRng.End = oRng.start + 8
    oRng.Text = "Whatever"

    I guess this is what you meant by defining a range.

  5. #5
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Ranges are kind of a tricky concept. But make sure you're showing your bookmarks, which will help you understand whether your bookmark "TestBM" looks like this:
    Isome text
    or like this
    [some text]

    On one case-- the bookmark is just like an insertion point before "some text" ... in the other, it is bracketing "some text." If it is already bracketing text, then Greg's code is perfect. If it isn't already bracketing text... then your question doesn't really make sense. There's nothing to "overwrite" if your bookmark is just an insertion point.

  6. #6
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    5
    Location
    Thanks for the information regarding bookmarks, Frosty. I had no idea that there were two kinds. My bookmarks look like this I, which I later came to know as “placeholder” bookmarks. That would also explain why I did not see the bookmark disappear after inserting the text and why Greg’s last line was not needed.

    In my application I have in a table cell a signature line, which is created by underlining a series of nonbreakable spaces, with a placeholder bookmark at the start of the line. The font is mono-spaced. I wanted to overwrite the spaces with the name of the signatory and not have the length of the line change.

  7. #7
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I'd have to see a mockup of your document to give a better answer. But rather than hard spaces, this is a good time to use an underlined tab character immediately after your bookmark and put the tab stop wherever you want your line to end. Probably not necessary to use hard spaces or coding for that effect.

    However, if you're in a table cell... You could also use a cell border.

    Or, you could use a paragraph border and adjust indents.

    Many ways to format a signature line without hardspace/underline formatting or a lot of underscore characters

  8. #8
    VBAX Newbie
    Joined
    Sep 2011
    Posts
    5
    Location
    Thanks for the suggestions! I'll try them out when I have some free time.

Posting Permissions

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