none
Capture the position of text in a word document RRS feed

  • Question

  • You can capture the position of text in a word document? 

    I can now find the text in the document, missing just get your position to add an image on the text, would identify the employee's name and put his signature on the text

    foreach (Section section in document.Sections)
    {
       foreach (Microsoft.Office.Interop.Word.HeaderFooter wordFooter in section.Footers)
       {
       	Microsoft.Office.Interop.Word.Range docRange = wordFooter.Range;
    	docRange.Find.Text = "text";
    	//get position	
        }
    }

    t is a Windows Forms C # 4.5 application, in this application I download the documents by WCF and make the necessary modifications in the case is missing only add a signature on behalf of the employee

    To add image'm doing this:

    Depending on the document's positions are wrong

    foreach (Section section in document.Sections)
    {
                        float left = 50;
                        float top = 750;
    
                        string imageName = string.Format("{0}.png", employee.ToString());
                        var path = Path.Combine(System.Windows.Forms.Application.StartupPath, "assinaturas", imageName);
                        FileInfo file = new FileInfo(path);
                        if (file.Exists)
                        {
                            Microsoft.Office.Interop.Word.HeaderFooter header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                            Microsoft.Office.Interop.Word.Shape nShape = header.Shapes.AddPicture(path,
                                                                                                  Microsoft.Office.Core.MsoTriState.msoCTrue,
                                                                                                  Microsoft.Office.Core.MsoTriState.msoCTrue, left, top, 85, 60);
    
                        }
    }

    • Edited by Pedro Ogm Wednesday, February 26, 2014 5:03 PM code
    Wednesday, February 26, 2014 4:23 PM

Answers

  • Are you sure the signature is in the page footer?

    Aside from that you could use something along the lines of the following VBA code (I don't do C#):

    Sub Demo()
    Dim Rng As Range, iShp As InlineShape
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Some Name"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      If .Find.Found Then
        Set Rng = .Duplicate
        MsgBox Rng.Text
        With Rng
          .InsertBefore Chr(11)
          .Collapse wdCollapseStart
          Set iShp = .InlineShapes.AddPicture(FileName:="Filepath\filename", Linktofile:=False)
        End With
        With iShp
          .LockAspectRatio = True
          .Height = 60
          .Width = 85
        End With
      End If
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Marvin_Guo Wednesday, March 5, 2014 3:10 PM
    Wednesday, February 26, 2014 11:04 PM
  • Holà Pedro

    When you use Find.Execute and Find is successful, the Range object (docRange) will change to include the found term

      docRange.Find.Text = "text";

    I think that's what you need, if I correctly understand your question? You can then use docRange as the Anchor to insert the graphic?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Marvin_Guo Wednesday, March 5, 2014 3:10 PM
    Tuesday, March 4, 2014 4:07 PM
    Moderator

All replies

  • You can capture the position of text in a word document? 

    I can now find the text in the document, missing just get your position to add an image on the text, would identify the employee's name and put his signature on the text
    foreach (Section section in document.Sections)
    {
       foreach (Microsoft.Office.Interop.Word.HeaderFooter wordFooter in section.Footers)
       {
       	Microsoft.Office.Interop.Word.Range docRange = wordFooter.Range;
    	docRange.Find.Text = "text";
    	//get position	
        }
    }


    Wednesday, February 26, 2014 2:56 PM
  • Are you building a Task Pane App?  Or is this a Document or Application level VSTO add-in?

    Eric Legault (MVP: Outlook; About me)
    Outlook Appins: Store social media fields in your Outlook Contacts!
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Wednesday, February 26, 2014 3:33 PM
  • It is a Windows Forms C # 4.5 application, in this application I download the documents by WCF and make the necessary modifications in the case is missing only add a signature on behalf of the employee
    Wednesday, February 26, 2014 3:51 PM
  • This forum is for building Apps for Office, which is a very different platform.  You'll want to post here: http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

    But also see:

    Returning Text from a Document
    http://msdn.microsoft.com/en-us/library/office/ff837330(v=office.15).aspx


    Eric Legault (MVP: Outlook; About me)
    Outlook Appins: Store social media fields in your Outlook Contacts!
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Wednesday, February 26, 2014 4:13 PM
  • Are you sure the signature is in the page footer?

    Aside from that you could use something along the lines of the following VBA code (I don't do C#):

    Sub Demo()
    Dim Rng As Range, iShp As InlineShape
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Some Name"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      If .Find.Found Then
        Set Rng = .Duplicate
        MsgBox Rng.Text
        With Rng
          .InsertBefore Chr(11)
          .Collapse wdCollapseStart
          Set iShp = .InlineShapes.AddPicture(FileName:="Filepath\filename", Linktofile:=False)
        End With
        With iShp
          .LockAspectRatio = True
          .Height = 60
          .Width = 85
        End With
      End If
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Marvin_Guo Wednesday, March 5, 2014 3:10 PM
    Wednesday, February 26, 2014 11:04 PM
  • Holà Pedro

    When you use Find.Execute and Find is successful, the Range object (docRange) will change to include the found term

      docRange.Find.Text = "text";

    I think that's what you need, if I correctly understand your question? You can then use docRange as the Anchor to insert the graphic?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Marvin_Guo Wednesday, March 5, 2014 3:10 PM
    Tuesday, March 4, 2014 4:07 PM
    Moderator
  • Yes it would even need to use the Find text as an anchor that position and add a picture, I searched and found no example
    Tuesday, March 4, 2014 5:09 PM
  • The code I posted 'anchors' the signature to the text by inserting it in-line, but on a new line above. If you require the image to not be formatted in-line:
    • delete '.InsertBefore Chr(11)'
    • insert '.ConvertToShape' after '.Width = 85'.
    You may also need to do some more work with positioning relative to the text, but the image will already be anchored there (you might also want to lock the anchor).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 4, 2014 10:31 PM