Get formatted characters

A

Ash

Hi,
I want to build a list of contiguous set of characters which shares the same
formatting (runs) from a Word 2003 document. What are the efficient ways of
doing it? I tried using several methods - Find, single character parsing (out
of desparation). I am not satisfied with my methods as they do not produce
the correct results or are slow, what is the efficient way of doing it.
Thanks in advance.
 
H

Helmut Weber

Hi Ash,

haven't we discussed this already?

Pseudocode
x = 1
' repeat this
get the format of character(x)
search for that format
remember the end position of the first find
put the found text somewhere
search again
while found
put the found text somewhere
wend
x = the remembered end position
' repeat

Of course, use range, not selection.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Ash

Hi Helmut,
I think you mistook me for another Ash. But thanks for your reply. I tried
that earlier but it takes very long time. And this becomes a problem when the
doc goes large. The number of iterations the loop has to run becomes very
large and so does the method calls buried inside the loop.

Ash
 
T

Tony Jollans

Although Word stores formatting as runs it doesn't expose it that way to VBA
and there isn't a lot of scope for doing anything other than something along
the lines that Helmut suggests, which, as you have found out, can be very
slow. The only other possibilities are to work with HTML or XML versions of
your document.

What exactly do you need this detail for?
 
H

Helmut Weber

Hi Tony,

I thought about this one,
to collect all fonts in the doc,
and process the fonts in the collection later:

Sub Test345()
Dim sDcm As String
Dim cFnt As Collection
Dim x As Long
Set cFnt = New Collection
sDcm = ActiveDocument.FullName
ActiveDocument.Save
ActiveDocument.SaveAs "c:\test.doc"
Dim rDcm As Range
Dim sFnt As String
Set rDcm = ActiveDocument.Range
While Len(rDcm.Text) > 1
sFnt = rDcm.Characters(1).Font.NameAscii
cFnt.Add sFnt
With rDcm.Find
.Font.NameAscii = sFnt
.Format = True
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
Set rDcm = ActiveDocument.Range
End With
ActiveDocument.UndoClear ' just in case
ActiveDocument.Save ' just in case
Wend
ActiveDocument.Save
ActiveDocument.Close
Documents.Open sDcm

For x = 1 To cFnt.Count
MsgBox cFnt(x)
Next

End Sub

NameAscii is most important,
thanks to Klaus Linke.

See:
http://tinyurl.com/mfkx6
 
H

Helmut Weber

hmm...

lots of redundant and erraneous code in it.

Cleaning up is left to the OP.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

But fonts are only part of formatting - there is still size, colour, weight,
etc.

Might be there be any way of scanning the formatting (as per the task pane)
if Word is set to keep track of formatting? I might have a play later when I
can get on 2003.
 
A

Ash

Hi Tony,
I want to select a portion of a document and only convert that slection into
XML on the fly. So speed is a maor issue here. I am trying to develop this
using add-ins. However, working with saved files is not a option here.
 
T

Tony Jollans

I'm no expert here but I would cut and paste it to a new document and save
that as xml - let Word do the work, it is far more efficient than you'll be.
I'm sure some xml expert can tell you how to use your own schema and/or a
transform to get what you want.
 

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