Link to home
Start Free TrialLog in
Avatar of npaun
npaun

asked on

How to know that MS Word Range section is from Embedded object and not from text?

I need to parse MS Word document text, character by character using the Range object, i.e.  something like
Do
    i=i+1
   strChar=MyRange.SetRange i, i+1
Loop
and this works fine for pure text. However, if in text there are some embedded objects, such as pictures, formulas, shapes, hyperlinks, etc., there are problems, as in the places after text where the object comes, there are additional characters, or "EMBED Package" strings, or other artifacts which are not present in the pure text.
So the question is: is there a way to know if the current section of the Range is not related to text but to another object in the Word document? Any way to distinguish such parts from pure textual parts?
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try something like this

Set doc = ActiveDocument
Set myRange = doc.Range

For i = 1 To myRange.Characters.Count
    If Not (myRange.Characters(i).InlineShapes.Count <> 0 Or myRange.Characters(i).Fields.Count <> 0) Then
        ' do something
    End If
Next

Regards
Avatar of npaun

ASKER

hi, thanks, it seems to be working, at least partially (there are some problems with hyperlinks and email addresses)...
but now I see that this approach will terribly slow down the iteration, if have to check this properties for each and every character that increases iteration time about 10 times... I will have to test if it can be done faster...
HI,

then try

Sub Macro()
Application.ScreenUpdating = False
Set doc = ActiveDocument
Set myrange = doc.Range
For i = 1 To myrange.Characters.Count
    Set chrRange = myrange.Characters(i)
    If Not (chrRange.InlineShapes.Count <> 0 Or _
            chrRange.Fields.Count <> 0 Or _
            chrRange.Hyperlinks.Count <> 0) Then
        ' do something
    End If
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
Avatar of npaun

ASKER

Hi, thanks, this solved problems with hyperlinks
1) Is there some other properties besides InlineShapes, Fields, Hyperlinks which would be informative about such objects? For instance, if there is a formula (equation) object inserted, this also gives problems, and it is not recognized by any of these three properties. Is there maybe something as ".NonTextObject" or ".OLEobject" which could indicate all such part in the range? In order to worry about each and every type of insertable object that word can handle, which is big...
2) I'm testing to increase speed by not checking these properties for each and every character, but using top-to-down approach, i.e. something as

    Set MyRange= doc.Range
    For i = 1 To doc.Range.InlineShapes.Count
        X1= MyRange.InlineShapes.Item(i).Range.Start,
        X2=MyRange.InlineShapes.Item(i).Range.End
    Next i

and to quickly get range sections which should be omitted from text iteration. This seems that can work ok for InlineShapes and Hyperlinks, but not for Fields, as .Fields.Item(i) does not have Range property which would enable this... Any suggestion or remedy regarding this approach?
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of npaun

ASKER

Thanks! that was very helpful!
Just one more minor question if I can: when I iterate Fields collection, some item in it does not have the InlineShape property i.e. it is Nothing, and hence there is no handy Range object... Do you maybe know why, and for which objects this happen? Just as a bonus question, as I consider this problem solved, but if can shed some light on this, that would be perfect...
Hi

in the documentation InlineShape

Returns an InlineShape object that represents the picture, OLE object, or ActiveX control that is the result of an INCLUDEPICTURE or EMBED field.

http://msdn.microsoft.com/en-US/en-en/library/office/ff193083(v=office.15).aspx

Regards
Avatar of npaun

ASKER

Thanks!