inserting autotexts into word document

D

daniel lie

Hi,

I have a question in regards to autotexts in word 03. At the moment, there
are 4 autotexts being used in my word document template. Each autotexts have
their own contents. For example,
we have autotexts for "methodology", autotexts for "executive summary",
"Bibliography", and "Glossary"

To insert these autotexts into word document is via a userform. User can
click the ones he/she wants to incorporate into his/her word report.

My question is there will be a case where user needs to add another
autotexts to the existing word report. For example, in the existing report,
there are methodology, executive summary, bibliography and glossary. Now, he
wants to add another autotexts called "Sections". This autotexts, by our
standard, should go between "executive summary" and "bibliography". My
headache is whether there're some way of inserting this new autotexts
somewhere between executive summary and bibliography?

Thank you in advance

url:http://www.ureader.com/gp/1022-1.aspx
 
J

Jean-Guy Marcil

daniel lie said:
Hi,

I have a question in regards to autotexts in word 03. At the moment, there
are 4 autotexts being used in my word document template. Each autotexts have
their own contents. For example,
we have autotexts for "methodology", autotexts for "executive summary",
"Bibliography", and "Glossary"

To insert these autotexts into word document is via a userform. User can
click the ones he/she wants to incorporate into his/her word report.

My question is there will be a case where user needs to add another
autotexts to the existing word report. For example, in the existing report,
there are methodology, executive summary, bibliography and glossary. Now, he
wants to add another autotexts called "Sections". This autotexts, by our
standard, should go between "executive summary" and "bibliography". My
headache is whether there're some way of inserting this new autotexts
somewhere between executive summary and bibliography?

What is the specific problem?

You already have a userform that is used to insert between 1 and 4 autotext
entries. Why can't you just modify it to add a 5th one?
 
F

fumei via OfficeKB.com

1. I am willing to bet that the userforms inserts the AutoText at the
Selection. It is the most common place. As in:

NormalTemplate.AutoTextEntries("MyBlue").Insert _
Where:=Selection.Range, _
RichText:=True

inserts an AutoText named "myBlue" at the Selection.

2. The key is that Where uses a range. Therefore, it can, in fact, be ANY
range.

NormalTemplate.AutoTextEntries("MyBlue").Insert _
Where:=ActiveDocument.Bookmarks("whatever").Range, _
RichText:=True

inserts an AutoText named "MyBlue" at the location of the bookmark "whatever"
(assuming there is one!).

Your question appears to demand logic to determine that there IS a chunk of
the document that WAS the AutoText "executivesummary", and there is a chunk
of the document that WAS the AutoText "bibliography".

That would be difficult. Once an Autotext is inserted. Word considers it as
just like any other content. There is no way to determine that chunk X was
inserted as an AutoText. Unless of course you designed something into either:


a) the code that inserted the AutoText. Manual AutoText insertion will do
not allow this.


OR

b) the AutoText itself.

To look at b): at the end of the chunk that is defined as "executivesummary",
you had a bookmark "ExecSummary", then inserting the AutoText would also
insert that bookmark. Ditto "bibliography"

Now, you CAN test. If ther a bookmark "executivesummary, AND there is a
bookmark "bibliography", THEN insert a new AutoText "Sections"

However...WHERE are you going to insert it????? You must state a Where.

Two paragraphs after ExecutiveSummary? Four? Count the paragraphs. Just
before Bibliography?

But IF the
Jean-Guy Marcil said:
[quoted text clipped - 14 lines]
headache is whether there're some way of inserting this new autotexts
somewhere between executive summary and bibliography?

What is the specific problem?

You already have a userform that is used to insert between 1 and 4 autotext
entries. Why can't you just modify it to add a 5th one?
 
A

Associates

Thank you, Jean-Guy and Fumei for your replies.

Fumei, you're right. I actually use that code to insert the AutoText at the
selection on the userforms. All you said was exactly what's on my mind. I
know that what I'm asking is not gonna be easy one (at least to me) to carry
out. One thing i know is that they are placed or inserted in the document in
the following order. For example,
1. executive summary
2. sections
3. bibliography
4. methodology
5. glossary

So, on the userform, I have them listed in that order. Users can tick the
ones they want to have in their report. However, there might be a case where
they may change their mind later after selecting executive summary, sections,
and methodology at the start. They can come back to the userform that would
disable those that are already ticked previously (to prevent them from
deleting the autotext - which could create a similar worse headache). They
would want to have bibliography to be included in the report. I agree with
Fumei that how the word would know that here is the chunk for executive
summary, the next chunk for sections. It would be nice to have some sort of
mark attached to this chunks here to indicate here is the beginning and the
end of the chunk of executive summary.

I'm curious about how to use bookmarks as indicator.

Worse comes to worst. I was thinking if there is a way i could use to
customise the whole word application so that user can only use the ones that
are available from the toolbar. I suppose the ultimate aim of this exercise
is to set up an standard environment where users must follow when they decide
to write up a report. This way, all reports created by different users will
be consistent across the board in terms of font type, margin space, color,
correct styles and bullet formatting. This looks more professional, i think.

I'm happy to hear any other approaches that might be easier but achieve the
aim.

Thank you in advance

fumei via OfficeKB.com said:
1. I am willing to bet that the userforms inserts the AutoText at the
Selection. It is the most common place. As in:

NormalTemplate.AutoTextEntries("MyBlue").Insert _
Where:=Selection.Range, _
RichText:=True

inserts an AutoText named "myBlue" at the Selection.

2. The key is that Where uses a range. Therefore, it can, in fact, be ANY
range.

NormalTemplate.AutoTextEntries("MyBlue").Insert _
Where:=ActiveDocument.Bookmarks("whatever").Range, _
RichText:=True

inserts an AutoText named "MyBlue" at the location of the bookmark "whatever"
(assuming there is one!).

Your question appears to demand logic to determine that there IS a chunk of
the document that WAS the AutoText "executivesummary", and there is a chunk
of the document that WAS the AutoText "bibliography".

That would be difficult. Once an Autotext is inserted. Word considers it as
just like any other content. There is no way to determine that chunk X was
inserted as an AutoText. Unless of course you designed something into either:


a) the code that inserted the AutoText. Manual AutoText insertion will do
not allow this.


OR

b) the AutoText itself.

To look at b): at the end of the chunk that is defined as "executivesummary",
you had a bookmark "ExecSummary", then inserting the AutoText would also
insert that bookmark. Ditto "bibliography"

Now, you CAN test. If ther a bookmark "executivesummary, AND there is a
bookmark "bibliography", THEN insert a new AutoText "Sections"

However...WHERE are you going to insert it????? You must state a Where.

Two paragraphs after ExecutiveSummary? Four? Count the paragraphs. Just
before Bibliography?

But IF the
Jean-Guy Marcil said:
[quoted text clipped - 14 lines]
headache is whether there're some way of inserting this new autotexts
somewhere between executive summary and bibliography?

What is the specific problem?

You already have a userform that is used to insert between 1 and 4 autotext
entries. Why can't you just modify it to add a 5th one?
 
J

Jean-Guy Marcil

Associates said:
Thank you, Jean-Guy and Fumei for your replies.

Fumei, you're right. I actually use that code to insert the AutoText at the
selection on the userforms. All you said was exactly what's on my mind. I
know that what I'm asking is not gonna be easy one (at least to me) to carry
out. One thing i know is that they are placed or inserted in the document in
the following order. For example,
1. executive summary
2. sections
3. bibliography
4. methodology
5. glossary

So, on the userform, I have them listed in that order. Users can tick the
ones they want to have in their report. However, there might be a case where
they may change their mind later after selecting executive summary, sections,
and methodology at the start. They can come back to the userform that would
disable those that are already ticked previously (to prevent them from
deleting the autotext - which could create a similar worse headache). They
would want to have bibliography to be included in the report. I agree with
Fumei that how the word would know that here is the chunk for executive
summary, the next chunk for sections. It would be nice to have some sort of
mark attached to this chunks here to indicate here is the beginning and the
end of the chunk of executive summary.

I'm curious about how to use bookmarks as indicator.

Here is some sample code to get you going. Use the code on a blank document.
Use the Subs in order (A, B C). I guess you could do it step by step while
checking what happens in the document to see the effect of some of the lines.

Option Explicit

Const strTextFiller As String = "Some text to insert so as to create " _
& "paragraphs. Even if they are short ones. "
Const strBookName As String = "Book_"

Sub A_InsertText()

Dim rngText As Range

Set rngText = ActiveDocument.Range

With rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "2", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "3", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "5", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
End With

End Sub

Sub B_DeleteText()

Dim rngDelete As Range

With ActiveDocument.Bookmarks
If .Exists("Book_3") Then
Set rngDelete = .Item("Book_3").Range
With rngDelete
.MoveEnd wdCharacter, 1
.Delete
End With
End If
End With

End Sub

Sub C_AddText()

Dim rngInsert As Range

With ActiveDocument.Bookmarks
If .Exists("Book_3") Then
Set rngInsert = .Item("Book_3").Range
ElseIf .Exists("Book_2") Then
Set rngInsert = .Item("Book_2").Range
ElseIf .Exists("Book_1") Then
Set rngInsert = .Item("Book_1").Range
Else
Set rngInsert = ActiveDocument.Range
rngInsert.Collapse wdCollapseStart
End If
End With

With rngInsert
.Collapse wdCollapseEnd
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "4", rngInsert
End With

End Sub


Of course, you would have to modify the code to work with AutoText and a
userform. The "A_..." sub would be run when clicking on OK the first time the
userform is run.
The "B_..." and "C_..." subs would be run every time after that.
I did not include code you would need to run before displaying the userform
the second time onward. This would check the existence of bookmarks (Like in
the "B_,.." sub) and for each bookmark found, the corresponding checkbox on
the userform would be set to True.

You need to be carful with bookmarks because as users work in the document,
they migth mess up bookmark boundaries... Make sure you have some visual
clues indicating the beginning and end of each section, and then train users
not to mess with those areas...

Just to be on the safe side, I would save the document before executing the
userform the second time around (and every time after that as well). So, if
the user has messed up the bookmarks, the garbge the code might produce can
be easily fixed by quitting without saving and reverting to the last saved
version...
 

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