Problem Replacing Bookmark content

J

John Shanley

Hi All:

I am trying to create a document where the user must work with entire
sections at a time. I thought I could do it using bookmarks. I am
capturing the content destined for each bookmarked section via a
dialog book and am trying to replace the bookmark's placeholder text.
I've created a bookmark named "OverviewText" and assigned it to the
following placeholder text: "OverTextHere"

I read in Word 97 Macro & VBA Handbook, pg. 490-491 that you can
"change the content of a Bookmark" by using code like the following:

With ActiveDocument.Bookmarks("OverviewText")
.Select
Selection.TypeText myNewOverviewText
End With

While the text is being replaced OK, the bookmark is being deleted. So
I can't come back and use the bookmark again, such as when the user
would need to review and edit it's content.

Am I barking up the wrong tree trying to use bookmarks in an
inappropriate manner?

Any clue about how to accomplish this would be very much appreciated.

TIA
JohnS
 
J

John Shanley

Jay:
You can certainly use bookmarks, but the advice you got from the Handbook is
the wrong way to go about it. Look here instead:

http://www.mvps.org/word/FAQs/MacrosVBA/InsertingTextAtBookmark.htm

Thanks for the pointer to the article. As it turns out, I searched the
old messages here turned up a similar piece of code. So I modified
that and got things working.

I also found the answer to another question, here. This is a great
resource!

I have two more questions, one has to do with creating a userform
where you can enter text (currently I'm using a textbox control), but
I also want to assign styles to each paragraph. (Basically, mimicing
Word's document window.) I then need to transfer the text, along with
each paragraph's style assignment, to the Word doc when I press a
command button on the userform.

Any clues?

The other is about the intricasies of Word's autonumbering. But that's
not really a VBA question.

Thanks again,
JohnS
 
J

Jonathan West

I have two more questions, one has to do with creating a userform
where you can enter text (currently I'm using a textbox control), but
I also want to assign styles to each paragraph. (Basically, mimicing
Word's document window.) I then need to transfer the text, along with
each paragraph's style assignment, to the Word doc when I press a
command button on the userform.

Any clues?

Generally, if you have a separate new question (as opposed to a followup to
an existing one), it's better to start a new thread - it's easier for us to
realise that there is something new to look at and answer.

It sounds like you need to have a textbox for the text, and a dropdown
listbox (a form of ComboBox) for the list of possible styles. You then leave
the Enabled property of the CommandButton False unless and until both the
textbox contains text and the combobox has a selection. That way, the
commandbutton code knows that the text and style are both available.

VBA Userforms don't have a Rich text control where you can enter formatted
text. You could maybe obtain a third-party control, but then you will have
the fun of distributing it along with your template. Even a third-party
control will almost certainly not understand Word styles.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
J

Jay Freedman

Another issue: It sounds like you want to have more than one paragraph in
the text box, and be able to assign (possibly) a different style to each
paragraph, finally dumping the whole mess into the document in one go. I
don't think that's possible -- but if it is, it's going to be horribly
confusing for users.

It would be better to open a separate temporary document, let the user fill
that with whatever, and then transfer the result to the original document
(possibly with some validation before transfer). The temp document could be
based on a specific template (see the Documents.Add method's topic in VBA
help).

If you go this route, don't be tempted to use cut & paste to transfer the
text through the clipboard. It's faster to do it this way:

Dim MainRange As Range
Set MainRange = DocMain.Range
MainRange.Collapse wdCollapseEnd
MainRange.FormattedText = DocTemp.Range.FormattedText
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top