Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting/Identifying a Paragraph 1

Status
Not open for further replies.

eveCalypso

Programmer
Apr 29, 2003
134
GB
Hello All!

I am currently inserting text into a document - from a form (more complex than that, but that is all that is needed for the question to make sense!).
I have a bookmark in my form where this text goes.
It may or may not be a few paragraphs - I build that in the background from user input before inserting into the document.

However!
Sometimes the user have to choose from a list of options. At present I Tab indent this list into the document. The problem is that the text can run over to a next line in some instances.
In which case I have a problem, as it then displays as:
Here is a sentence which is hopefully long enough to go over two lines!

Any indent is document specific (as opposed to be able to somehow let the string handle hanging indents - no functionality for that such as Chr(10) or similar).

I see I can change the hanging indent PER Paragraph though... which will help me immensely.
But I do not know how to access paragraph/ranges for my text.

E.g. I know selection.paragraphs(1).TabHaningIndent 1 is possible, but I can not guarentee the index.
So, here are my questions (phew - finally!):

1) Is there any way to apply a hanging indent to the document afterwards? I.e. "indent where there are tabs" - if I do it the above way, then it indents everything!

2) is there a programmatic way in which I can apply the indent at time of insertion?

3) Is there some way I can perhaps go through text and pick
up where there is a Tab in the sentence? (nasty, but perhaps my only option!)

4) autoformat (afterwards) - removes all my tabs! Is it possible to programmatically insert bullet points instead? That way, it should format correctly?

5) Any other suggestions?

Thank you in advance!!!
Best Regards
EvE (using Word 2000)
 
Hi EvE,

You should be able to apply paragraph formatting as you insert. You might have to play around a bit depending on the exact format of yor data but I just did this to an empty document and it I think it does what you want.

Code:
Sub Hang()

Dim Para1 As String
Para1 = "A longish string so that I can see what is happening of course it takes a lot of letters to do that"
Dim Para2 As String
Para2 = vbTab & "Another longish string so that I can see what is happening of course it takes a lot of letters to do that and I want this line to spread further than the other to confirm the hang"
Dim Para3 As String
Para3 = "And a third longish string so that I can see what is happening of course it takes a lot of letters to do that"

Selection.InsertAfter Para1
Selection.Collapse wdCollapseEnd
Selection.TypeParagraph

Selection.ParagraphFormat.TabHangingIndent 1
Selection.InsertAfter Para2
Selection.Collapse wdCollapseEnd
Selection.TypeParagraph

Selection.ParagraphFormat.TabHangingIndent -1
Selection.InsertAfter Para3
Selection.Collapse wdCollapseEnd
Selection.TypeParagraph

End Sub

Enjoy,
Tony
 
Dear Mr Jollans,

Thank you for your reply! Yes, I understand that that will work nicely, but I have two major issues which is different to the scenario you have put forward:

1) I build a String which contains all paragraphs and sublists etc up front and insert it once with Selection.TypeText theString
That means each of my paragraphs are NOT individual selections (partly due to the complexity of maintaining that).

2) I have written a "collate control information" function which does what it says on the tin, i.e. it collates the exact wording off the controls which have been bundled with a frame if they can make up a logical paragraph (as the users select by means of checkboxes which values they want in the letter). So it is a problem if the indent applies the "indent" to the second line the main text as well.

In an ideal world, I would *somehow* like to run through the String text or inserted text, pick up where there are tabs and apply hanging indents as applicable, OR I would like to apply a style that will always make the letters line up if indented OR even better, I would like to run an autoformat and see it all come out perfectly :)

The problem is that absolutely everything works and is as object driven as I could get it, but now I have this horrible Word-side issue, which is not easily solvable ito code.

ANY help or suggestion would be fantastic - I am not that familiar with the Word Object Model, and I might have missed something!

Regards,
EvE
 
Hi EvE,

As the formatting cannot be done before insertion into Word, you must do it afterwards. Your problem seems simply to be identifying the inserted text after inserting it.

Let’s assume that your bookmark (insertion point) is immediately after a paragraph mark, Let’s also assume that your final inserted paragraph ends in a paragraph mark. We then know that when working with the inserted text we are not picking up anything that was in the document before.

With that caveat, we can set a range to be the inserted text and then process each paragraph within it, something like this:

Code:
 Selection.TypeText
Code:
TextVar
Code:
Code:
 ’ Assume this is how you insert text
                           ‘ Selection is now at end of it
Code:
Dim
Code:
YourRange
Code:
 As Range
Dim
Code:
YourPara
Code:
 As Paragraph

Set
Code:
YourRange
Code:
 = ActiveDocument.Range(ActiveDocument.Bookmarks("
Code:
YourBookmark
Code:
").End, Selection.End)

For Each YourPara in YourRange.Paragraphs
    If Left(YourPara.Range.Text, 1) = vbTab Then
Code:
 ‘ Do Your paragraph formatting here
Code:
    End If
Next

Ranges can be a bit confusing in Word at times, so if you want any more help just ask.

Enjoy,
Tony
 
Dear Mr Jollans,

That looks exactly like what I am looking for! I will try it and see if it is sufficient for my needs (and if I can get it working!).
Just as a sidenote.
Would it be advisable to perhaps create a style from this code and then apply the style when I am done?
Is that bad practise?

Regards,
EvE
 
Hi EvE,

I'm not really a Word expert, but I would generally advocate the use of styles over fomatting paragraphs. That said, it rather depends on what you want to achieve.

If you put them in the template they would only exist on the machine you were working on, and users might change them so you would have to decide what to do with them next time you created a document.

If you put them in the document you would have to do it every time which might be an overhead for no great gain. What happens with the documents after you have done your stuff? Are they manually edited and would a user want to apply the same formatting to other paragraphs? I guess what I'm really saying is that it's your call based on your situation.

Enjoy,
Tony
 
Mr Jollans,
Fantastic!
This works perfectly.
Not sure it will work for all scenarios yet, but so far so good - *bow*
Mr Range Object :)

Regards,
EvE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top