Multiple lines in a word document

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Multiple lines in a word document

Post by Pat »

I have a requirement to cease using txt files to put text onto a word document and use instead a couple of tables (header and Lines).
On the word document there is a field called <ConformanceText> that is used to store the text from a selection of text files.
In the text file there are tab characters (09).
The maximum number of characters that can fit into this field is 255 (256?) and i am trying to fit in 273. My text doesn't include the tab characters, i have spaced them out.
The code to read data from the file is:

Code: Select all

    
    Open Conformance For Input As #1
    Do While Not EOF(1)
        conformancetext = conformancetext + Input(1, 1)
    Loop
    Close #1
The code to show this field in the document is:

Code: Select all

    
    WORDapp.ScreenUpdating = True
    WORDapp.ScreenRefresh
    
    With WORDapp.ActiveDocument.Content.Find
        .ClearFormatting
        .Forward = True
        .Wrap = 1   'wdfindcontinue
        .Execute "<ConformanceText>", , , , , , , , , conformancetext, 2       'wdReplaceAll
    End With
Is there a way to show each of the lines in the <ConformanceText> field, or do i have to put the tab characters in my text?

Or maybe there is another way around this.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

I'm sorry, I don't understand your question. What do tab characters have to do with "lines in the <ConformanceText> field"?
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

I have 5 lines to show, can i somehow put these into the field <conformancetext> one at a time, at the moment they are concatenated together?

The tab characters are in the text file and only take up about 230 characters. In the table i have substitued blanks for the tab characters so this blows the number of chars out to 273 in this case.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

You can use the Carriage Return character vbCr to indicate a new paragraph:

conformancetext = line1 & vbCr & line2 & vbCr & line3 & vbCr & line4 & vbCr & line5
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12623
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Multiple lines in a word document

Post by StuartR »

Pat,

I am also a bit confused here. What kind of field are you talking about?

Do you just have to insert this text once, or do you need to be able to replace it with updated text in the future? Why not just use a bookmark to specify the starting location for the inserted text and place it after the bookmark? Or use an IncludeText field and allow word to automatically include the text from the external document?
StuartR


Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

HansV wrote:You can use the Carriage Return character vbCr to indicate a new paragraph:
Yes i know i can do that. The problem with that is that the string is too long (273 chars).
What I want to do is show 5 lines from the text table at and under the tag <ConformanceText>, almost like a subform with multiple lines.

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

StuartR wrote:Pat,

I am also a bit confused here. What kind of field are you talking about?

Do you just have to insert this text once, or do you need to be able to replace it with updated text in the future? Why not just use a bookmark to specify the starting location for the inserted text and place it after the bookmark? Or use an IncludeText field and allow word to automatically include the text from the external document?
At the moment the string ComformanceText (inside VBA) is populated with 5 lines and then shown at the tag <ConformanceText>.
I need to show N (could be any number) lines after the tag <ConformanceText>. How would i do that?
I have no idea what the VBA code needs to be.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

You can replace with a text of any length by using code like this:

Code: Select all

    WORDapp.Selection.HomeKey 6 ' wdStory
    With WORDapp.Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = 0 ' wdFindStop
        .Text = "<ConformanceText>"
        Do While .Execute
          WORDapp.Selection.TypeText conformancetext
          WORDapp.Selection.Collapse 0 ' wdCollapseEnd
        Loop
    End With
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

Do i use this after populating the string variable conformancetext?
Ignore this question, i have tested it, Thank you Hans.

Would you explain what the Do While loop does.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

This version first moves to the start of the document (WORDapp.Selection.HomeKey 6 ' wdStory).
.Execute tries to find the next instance of the placeholder text "<ConformanceText>"; if found, it selects that instance and returns True, otherwise it returns False. So

Code: Select all

Do While .Execute
  ...
Loop
selects each instance in turn. Within the loop, the contents of the conformancetest variable is written over the selected placeholder text, then the selection is collapsed to an insertion point at the end of that text. This is necessary to make the Find continue; if the text were still selected the Find would be confined to the selection.
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

Does that mean if i have many lines in a table that i could do the following:

Code: Select all

Set rs = currentdb.OpenRecordset (.....
Do while Not rs.eof
   WORDapp.Selection.HomeKey 6 ' wdStory
    With WORDapp.Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = 0 ' wdFindStop
        .Text = "<ConformanceText>"
        Do While .Execute
          WORDapp.Selection.TypeText conformancetext
          WORDapp.Selection.Collapse 0 ' wdCollapseEnd
        Loop
    End With
    rs.moveNext
Loop
or, am i limited to concatening these lines into a string variable and using your code.
I don't mind either way, i am just interested.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

You don't do anything with the recordset except looping through it - you don't refer to any fields. So I don't know what you want to accomplish.
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

My bad, I meant to populate comformancetext with rs!Text from the recordset.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

Do you want to concatenate all the values of rs!Text into conformancetext (with line breaks), and then replace all instances of "<ConformanceText>" with the value of the variable?
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

No it was more of a question of doing it a different way.

What if the string variable go to greater then 65536 characters? I know this is very unlikely, but...

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

I still don't understand what exactly you're trying to accomplish.
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

Just seeing what the alternatives are if the string variable gets to 65536 or larger, i ssue it will bomb at that stage.
Or if i have many records to read and setup at a certain point in a word document.
I appreciate your support.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple lines in a word document

Post by HansV »

It works with strings up to and including 32,767 (=2^15-1) characters in length.
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Multiple lines in a word document

Post by Pat »

Thank you.