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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable/Array Text Formatting 1

Status
Not open for further replies.

glind

Programmer
Sep 10, 2004
2
US
I have a VBA program that reads all MS Word Document header, paragraph# & page# data to an array. The array is then written to a table in a new MS Word Document. Everything works fine, except that I obviously lose the text formatting when the data is passed via a string variable through the array.

A copy and paste loop for each individual paragraph works, but this method is extremely slow due to the number of times each document is called (especially on a 1,000 page document with roughly 10,000 paragraphs!).

How can I pass the text through the array, and maintain the original Document paragraph formatting (ie, bold text, font sizes, underline, etc, etc)?
 
The best way (as opposed to the easiest way), would be to create a class module and make objects of the entities. Make their fonts (or any other characteristic) into a property of the object.

Alternatively, have you tried to make an array that would hold the information of the format? If it is just font characteristics you could make boolean variables to match the format for each item. IsBold = False, IsUnderlined = True, and so on.

Maybe post some code of your array?

Gerry
 
Thank you fumei for responding to my question. I have spent many hours searching on ways to manipulate formatted text (MS Word) through arrays, and I have not had much luck.

I've stripped down my code, since my problem is pretty basic. As you can see, I'm just trying to read the MS Word paragraphs (say the first 8) from Doc1, and write them into cells in a table in Doc2. The data transfers fine though the array, but I obviously lose the formatting.

My goal is to read the Doc1 text as RTF code or some other language (HTML, TGML or ???) that includes the text formatting info as part of the text string, & then read the code through an array and into the Doc2 table as the formatted text.


Code:
Dim myDoc1 As Document, myDoc2 As Document
Dim MyTable2 As Word.Table
Dim myText1(8) As String
Dim pCount As Integer, tCount As Integer

Set myDoc1 = Documents("c:\ReadInfo.doc")
Set myDoc2 = Documents("c:\WriteInfo.doc")

Set MyTable2 = myDoc2.Tables(1)

For pCount = 1 To 8
    myText1(pCount) = myDoc1.Paragraphs(pCount).Range.Text
Next pCount

For tCount = 1 To 8
    MyTable2.Cell(tCount, 1).Range.Text = myText1(tCount)
Next tCount

End Sub

Any ideas???
 
Here you go. This copies the text over to a new document, format intact.

That was fun! Thanks for the bit of a challenge!

Code:
Sub CopyFormat()
Dim myDoc1 As Document, myDoc2 As Document
Dim MyTable2 As Word.Table
Dim myText1(8) As String
Dim myDup
Dim r As Range
Dim pCount As Integer, tCount As Integer

Set myDoc1 = Documents("c:\ReadInfo.doc")
Set myDoc2 = Documents("c:\WriteInfo.doc")
Set MyTable2 = myDoc2.Tables(1)

tCount = 1
For pCount = 1 To 8
    myText1(pCount) = myDoc1.Paragraphs(pCount).Range.Text
    Set r = myDoc1.Paragraphs(pCount).Range
    Set myDup = r.Paragraphs.Format.Duplicate
        myDoc2.Activate
     MyTable2.Cell(tCount, 1).Range.Text = myText1(tCount)
       Set r = Nothing
       Set r = MyTable2.Cell(tCount, 1).Range
       r.Select
       Selection.Paragraphs.Format = myDup
            tCount = tCount + 1
            Set r = Nothing
            Set myDup = Nothing
            myDoc1.Activate
Next pCount
Set myDoc1 = Nothing
Set myDoc2 = Nothing
End Sub

NOTE: This does not have Application.ScreenUpdating = False.....you may want to do that.

Gerry
 
Actually, it can have fewer lines. You do not the Range object.

Code:
Sub CopyFormat()
Dim myDoc1 As Document, myDoc2 As Document
Dim MyTable2 As Word.Table
Dim myText1(8) As String
Dim myDup
Dim pCount As Integer, tCount As Integer

Set myDoc1 = Documents("c:\ReadInfo.doc")
Set myDoc2 = Documents("c:\WriteInfo.doc")
Set MyTable2 = myDoc2.Tables(1)

tCount = 1
For pCount = 1 To 8
    myText1(pCount) = myDoc1.Paragraphs(pCount).Range.Text
    Set myDup = myDoc1.Paragraphs(pCount).Format.Duplicate
        myDoc2.Activate
     MyTable2.Cell(tCount, 1).Range.Text = myText1(tCount)
       MyTable2.Cell(tCount, 1).Select
       Selection.Paragraphs.Format = myDup
            tCount = tCount + 1
            Set myDup = Nothing
            myDoc1.Activate
Next pCount
Set myDoc1 = Nothing
Set myDoc2 = Nothing
End Sub

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top