Range.Text produces unexpected result for image

Hi there,

I am processing a document in a Word Add-In that I have written.  I find that a certain range, which as I found includes a certain image, results in a string with a forward slash character "/" when I print the range.text corresponding to that range.  When I delete the image, the "/" is no longer printed.

I cannot understand why this is happening and it is throwing off the character offsets that I am using.

Thanks for any answers you can provide.

foreach (Paragraph para in document.Paragraphs) {
      file.Write(para.Range.Text);
}
FYI I am using Word 2010, not 2013.


August 20th, 2015 10:43pm

HI sdray

This is the "character placeholder" for an InlineShape, converted to an ANSI character. You need to literally workaround it. Here's an example that concatenates the text in a given range, leaving out the InlineShapes:

Sub TestRangeWoGraphic()
    Dim rng As word.Range
    Set rng = Selection.Range
    Debug.Print RangeTextNoGraphic(rng)
End Sub

'InlineShapes return a / in Range.Text so workaround it
'Also check for Shapes embedded as "inline" 
Function RangeTextNoGraphic(rngContent As word.Range) As String
    Dim rngText As String
    Dim para As word.Paragraph
    Dim ils As word.InlineShape, shp As word.Shape
    Dim ilsPos As Long, rngPos As Long
    
    rngText = ""
    If rngContent.InlineShapes.Count <= 0 Then
        rngText = rngContent.Text
    Else
        rngPos = 1
        For Each ils In rngContent.InlineShapes
            ilsPos = ils.Range.Start
            rngText = rngText & Mid(rngContent, rngPos, ilsPos - 1 - rngPos)
            rngPos = ilsPos + 1
        Next
        For Each para In rngContent.Paragraphs
            For Each shp In para.Range.ShapeRange
                If shp.WrapFormat.Type = wdWrapInline Then
                    ilsPos = shp.anchor.End
                    rngText = rngText & Mid(rngContent, rngPos, ilsPos - 1 - rngPos)
                    rngPos = ilsPos
                End If
            Next
        Next
        rngText = rngText & Mid(rngContent, rngPos)
    End If
    
    RangeTextNoGraphic = rngText
End Function

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 10:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics