Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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?
0
npaun
Asked:
npaun
  • 4
  • 4
1 Solution
 
Rgonzo1971Commented:
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
0
 
npaunAuthor Commented:
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...
0
 
Rgonzo1971Commented:
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
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
npaunAuthor Commented:
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?
0
 
Rgonzo1971Commented:
Q1

pls 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 Or _
            chrRange.OMaths.Count <> 0) Then
        ' do something
    End If
Next
Application.ScreenUpdating = True
End Sub

Q2 the Range of a Field is found in

Fields(i).InlineShape.Range


Regards
0
 
npaunAuthor Commented:
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...
0
 
Rgonzo1971Commented:
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
0
 
npaunAuthor Commented:
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now