none
How to set the field results manually? RRS feed

  • Question

  • Hi!

    I'm making a field related Word addin program. Now I want to set the field results programatically. The content of field result will be two inline shapes and any text like following:

    Dim activeDocument As Word.Document =Globals.xxx.Application.ActiveDocument
    Dim fd As Field
    Dim selection As Selection = Globals.xxx.Application.Selection
    fd = activeDocument.Fields.Add(Range:=selection.Range,
         Type:=WdFieldType.wdFieldQuote, Text:="DUMMY RESULT",
         PreserveFormatting:=False)

    'Make two inline shape programatically.
    Dim shp1 As inlineShape
    Dim shp2 As inlineShape

    'Make the filed result like this:
    fd.Result.FormattedText= shp1 + "ABC" + shp2

    Making inline shape function is already made. But I want to know how to combine them and set it as filed result. Any ideas?

    Saturday, September 3, 2011 2:23 PM

Answers

  • Just for something to do on a rainy Saturday afternoon, I took Cindy's pseudocode and made a functioning example routine. To test in a document add a Quote field with an "ABC" value and change the picture paths to your appropriate setting.

    Sub InsertImagesInFieldCodes()
        Dim doc As Word.Document
        Dim rngFld As Word.Range
        Dim rngInsert As Word.Range
        Dim InlineShapeIndicator As String
        
        InlineShapeIndicator = "ABC"
        Set doc = Word.ActiveDocument
        Set rngFld = doc.Fields(1).Code
        rngFld.TextRetrievalMode.IncludeFieldCodes = True
        Set rngInsert = rngFld.Duplicate
        rngInsert.TextRetrievalMode.IncludeFieldCodes = True
        rngInsert.Find.Execute FindText:=InlineShapeIndicator
        doc.InlineShapes.AddPicture _
            filename:="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg", _
            Range:=rngInsert
        rngInsert.Collapse Word.wdCollapseEnd
        doc.InlineShapes.AddPicture _
            filename:="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg", _
            Range:=rngInsert
        doc.Fields(1).Update
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, September 3, 2011 8:20 PM
  • Hi all,

    Finally I changed my program to edit directly the field result. The debug result in Word2010 is fine. It works both outside the field and inside the filed. Thank you for the help.

        Sub InsertParagraph()
            'Add a new Empty field
            Dim activeDocument As Word.Document =Globals.xxxAddIn.Application.ActiveDocument
            Dim fd As Field
            Dim selection As Selection = Globals.xxxAddIn.Application.Selection
            fd = activeDocument.Fields.Add(Range:=selection.Range,
                 Type:=WdFieldType.wdFieldQuote, Text:="###Paragraph"" ""text$$$",
                 PreserveFormatting:=False)

            'Set end inline shape
            Dim startIShp As inlineShape
            Dim endIShp As inlineShape

            endIShp = makeIShape("/para", MsoAutoShapeType.msoShapeChevron)
            endIShp.Range.Cut()
            Dim rngFld As Range
            Dim rngInsert As Range
            rngFld = fd.Result
            rngInsert = rngFld.Duplicate
            rngInsert.Find.Execute(FindText:="$$$", ReplaceWith:="^c")

            'Set start inline shape
            startIShp = makeIShape("para", MsoAutoShapeType.msoShapePentagon)
            startIShp.Range.Cut()
            rngFld = fd.Result
            rngInsert = rngFld.Duplicate
            rngInsert.Find.Execute(FindText:="###", ReplaceWith:="^c")

            ' Change the field to Addin type
            Dim codeRange As range
            codeRange = fd.Code
            codeRange.Text = "ADDIN " + "\elem ""para"""
            fd.Data = "elem=""para"""
            fd.Update()
        End Sub

    Regards,

    • Marked as answer by tmakita Monday, September 5, 2011 10:06 PM
    Monday, September 5, 2011 10:04 PM

All replies

  • Hi tmakita

    This is tricky. You can't use Field.Result for this because what you want is for these InlineShapes to become part of the Quote field code. That means you have to insert them directly into the Quote field.

    The Field.Code property returns a RANGE object:
      fldRange = Field.Code

    So what you need to do is locate the position within that Range where you want an InlineShape, assign that to a Range object, then use this Range as the target Range for the InlineShape.

    When I've done something like this what I've usually done (pseudocode!) resembles:

      Dim rngFld as Word.Range
      Dim rngInsert as Word.Range
      Dim InlineShapeIndicator as String
      InlineShapeIndicator = "XX"
      Set rngFld = doc.Fields(1).Code
      rngFld.TextRetrivealMode.IncludeFieldCodes = true
      rngInsert = rngFld.Duplicate
      rngInsert.TextRetrivealMode.IncludeFieldCodes = true
      rngInsert.Find.Execute(Text:=InlineShapeIndicator)
      doc.InlineShapes.AddPicture(fileName:="xxx", Range:=rngInsert)

    InlineShapeIndicator is a string inside the Quote field that you want to replace with the InlineShape. If Find is successful, rngInsert will change from the entire field code to the found text.

    It will probably take fiddling and testing, but the described approach should work, in the end...


    Cindy Meister, VSTO/Word MVP
    Saturday, September 3, 2011 4:36 PM
    Moderator
  • Just for something to do on a rainy Saturday afternoon, I took Cindy's pseudocode and made a functioning example routine. To test in a document add a Quote field with an "ABC" value and change the picture paths to your appropriate setting.

    Sub InsertImagesInFieldCodes()
        Dim doc As Word.Document
        Dim rngFld As Word.Range
        Dim rngInsert As Word.Range
        Dim InlineShapeIndicator As String
        
        InlineShapeIndicator = "ABC"
        Set doc = Word.ActiveDocument
        Set rngFld = doc.Fields(1).Code
        rngFld.TextRetrievalMode.IncludeFieldCodes = True
        Set rngInsert = rngFld.Duplicate
        rngInsert.TextRetrievalMode.IncludeFieldCodes = True
        rngInsert.Find.Execute FindText:=InlineShapeIndicator
        doc.InlineShapes.AddPicture _
            filename:="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg", _
            Range:=rngInsert
        rngInsert.Collapse Word.wdCollapseEnd
        doc.InlineShapes.AddPicture _
            filename:="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg", _
            Range:=rngInsert
        doc.Fields(1).Update
    End Sub
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Saturday, September 3, 2011 8:20 PM
  • Hi Cindy and Rich,

    Thank you for your reply. I'm very glad to get the excellent sample codes.

    However it was my mistake not to inform you the program preconditions.

    1. The inline shape is made from auto shape by the ConvertToInlineShape() methods.

    2. My last goal is to make "ADDIN" field from "QUOTE" field.

    Following is the code that was modified according to your suggestion. This code works fine as long as the cursor is outside the field. If the cursor is inside the field that I made previously, range.Find.Execute(FindText:="$$$", ReplaceWith:="^c") does not work at all. So I am still struggling with VB codes. Do you have any ideas?

        Sub InsertParagraph()
            'Add a new Empty field
            Dim activeDocument As Word.Document =Globals.xxxAddIn.Application.ActiveDocument
            Dim fd As Field
            Dim selection As Selection = Globals.xxxAddIn.Application.Selection
            fd = activeDocument.Fields.Add(Range:=selection.Range,
                 Type:=WdFieldType.wdFieldQuote, Text:="###Paragraph"" ""text$$$",
                 PreserveFormatting:=False)

            'Set end inline shape
            Dim startIShp As inlineShape
            Dim endIShp As inlineShape

            endIShp = makeIShape("/para", MsoAutoShapeType.msoShapeChevron)
            endIShp.Range.Cut()
            Dim rngFld As Range
            Dim rngInsert As Range
            rngFld = fd.Code
            rngFld.TextRetrievalMode.IncludeFieldCodes = True
            rngInsert = rngFld.Duplicate
            rngInsert.TextRetrievalMode.IncludeFieldCodes = True
            rngInsert.Find.Execute(FindText:="$$$", ReplaceWith:="^c")

            'Set start inline shape
            startIShp = makeIShape("para", MsoAutoShapeType.msoShapePentagon)
            startIShp.Range.Cut()
            rngFld = fd.Code
            rngFld.TextRetrievalMode.IncludeFieldCodes = True
            rngInsert = rngFld.Duplicate
            rngInsert.TextRetrievalMode.IncludeFieldCodes = True
            rngInsert.Find.Execute(FindText:="###", ReplaceWith:="^c")
            fd.Update()

            ' Change the field to Addin type
            Dim codeRange As range
            codeRange = fd.Code
            codeRange.Text = "ADDIN " + "\elem ""para"""
            fd.Data = "elem=""para"""
            fd.Update()
        End Sub

        Function makeIShape(ByVal text As String, ByVal shpType As MsoAutoShapeType) As inlineShape
            Const stProcedure = "makeIShape"
            On Error GoTo lbl_err
            Dim shp As Microsoft.Office.Interop.Word.Shape
            Dim activeDocument As Word.Document = Globals.xxxAddIn.Application.ActiveDocument
            shp = activeDocument.Shapes.AddShape(1, 0, 0, 30, 11)
            shp.AutoShapeType = shpType
            shp.Fill.ForeColor.RGB = RGB(50, 222, 222)
            With shp.Line
                .Weight = 1
                .ForeColor.RGB = RGB(0, 0, 0)
            End With
            With shp.TextFrame
                .MarginBottom = 0
                .MarginLeft = 0
                .MarginRight = 0
                .MarginTop = 0
                .TextRange.Text = text
                With .TextRange.Font
                    .Name = "Arial"
                    .Size = 9
                    .ColorIndex = WdColorIndex.wdBlack
                End With
                With .TextRange.ParagraphFormat
                    .DisableLineHeightGrid = True
                End With
            End With

            Dim ishp As inlineShape
            ishp = shp.ConvertToInlineShape()
            Return ishp

    lbl_err:
            MsgBox("Fatal Error" & stProcedure & vbCrLf & _
                     "Error number" & CStr(Err.Number) & vbCrLf & _
                     "Error description" & Err.Description & vbCrLf & _
                     "Error source" & Err.Source, vbOKOnly + vbExclamation)
        End Function

    Best regards,

     

     

     

    Monday, September 5, 2011 11:27 AM
  • Hi all,

    Finally I changed my program to edit directly the field result. The debug result in Word2010 is fine. It works both outside the field and inside the filed. Thank you for the help.

        Sub InsertParagraph()
            'Add a new Empty field
            Dim activeDocument As Word.Document =Globals.xxxAddIn.Application.ActiveDocument
            Dim fd As Field
            Dim selection As Selection = Globals.xxxAddIn.Application.Selection
            fd = activeDocument.Fields.Add(Range:=selection.Range,
                 Type:=WdFieldType.wdFieldQuote, Text:="###Paragraph"" ""text$$$",
                 PreserveFormatting:=False)

            'Set end inline shape
            Dim startIShp As inlineShape
            Dim endIShp As inlineShape

            endIShp = makeIShape("/para", MsoAutoShapeType.msoShapeChevron)
            endIShp.Range.Cut()
            Dim rngFld As Range
            Dim rngInsert As Range
            rngFld = fd.Result
            rngInsert = rngFld.Duplicate
            rngInsert.Find.Execute(FindText:="$$$", ReplaceWith:="^c")

            'Set start inline shape
            startIShp = makeIShape("para", MsoAutoShapeType.msoShapePentagon)
            startIShp.Range.Cut()
            rngFld = fd.Result
            rngInsert = rngFld.Duplicate
            rngInsert.Find.Execute(FindText:="###", ReplaceWith:="^c")

            ' Change the field to Addin type
            Dim codeRange As range
            codeRange = fd.Code
            codeRange.Text = "ADDIN " + "\elem ""para"""
            fd.Data = "elem=""para"""
            fd.Update()
        End Sub

    Regards,

    • Marked as answer by tmakita Monday, September 5, 2011 10:06 PM
    Monday, September 5, 2011 10:04 PM