none
c# word.introp FindAndReplace the whole document RRS feed

  • Question

  • Hello everybody,
    What I mean by the whole document is the word document I work with is covered with msoCanvas around it so it'll be easier to move shapes around.
    so, if I want to get the content inside I somehow need to loop trough the shapes inside of it (the Msocanvas shape type)
    or execute the function on the file with some magic property that will find the text I wanted. For example, while using the FindAndReplace in word 365 there is an option to search down or up the document or all of it, the last one works for me but I don't know how to implement it trough c#.
    I hope I made myself clear
    Thank you in advance!
    Friday, October 4, 2019 12:20 PM

Answers

  • I'm not particularly familiar with this area, but to loop through the shapes on a canvas, you need something like the following - I leave you to translate into C#

    Dim canvas As Shape
    Dim shp As Shape
    ' look for the first canvas
    For Each shp In ActiveDocument.Shapes
      If shp.Type = msoCanvas Then
        Set canvas = shp
        Exit For
      End If
    Next
    If Not (canvas Is Nothing) Then
      For Each shp In canvas.CanvasItems
        ' then you could try to find text within the shape
        With shp.TextFrame.TextRange.Find
        '...
        End With
      Next
    End If
    FWIW you seem to have to use the TextFrame.TextRange.Find, which is an object, rather than TextFrame2.TextRange.Find, which is a function that does not obviously work.

    I don't see an obvious way to use a Find object associated with the canvas to filter the Shapes.

    Peter Jamieson

    • Marked as answer by SaharWasTaken Sunday, October 6, 2019 6:27 PM
    Saturday, October 5, 2019 8:32 AM

All replies

  • I'm not particularly familiar with this area, but to loop through the shapes on a canvas, you need something like the following - I leave you to translate into C#

    Dim canvas As Shape
    Dim shp As Shape
    ' look for the first canvas
    For Each shp In ActiveDocument.Shapes
      If shp.Type = msoCanvas Then
        Set canvas = shp
        Exit For
      End If
    Next
    If Not (canvas Is Nothing) Then
      For Each shp In canvas.CanvasItems
        ' then you could try to find text within the shape
        With shp.TextFrame.TextRange.Find
        '...
        End With
      Next
    End If
    FWIW you seem to have to use the TextFrame.TextRange.Find, which is an object, rather than TextFrame2.TextRange.Find, which is a function that does not obviously work.

    I don't see an obvious way to use a Find object associated with the canvas to filter the Shapes.

    Peter Jamieson

    • Marked as answer by SaharWasTaken Sunday, October 6, 2019 6:27 PM
    Saturday, October 5, 2019 8:32 AM
  • my savior!
    Thank you very much :)

    Sunday, October 6, 2019 6:28 PM