3 Replies Latest reply: Sep 19, 2011 10:36 AM by Michael Maier RSS

    Macro to remove and add new fields to a group


      I could use a macro to remove all fields from a group and add new fields to same group.

      The amount of fields already in group is variable. So hard coded "group.RemoveField 1", "group.RemoveField 2" etc.

      will not do the job.


      In the example my "Test Group" has 3 fields.

      if i use "group.RemoveField i" --> 1st and 3rd field are removed while 2nd remains.

      if i use "group.RemoveField 0" --> only 1st field is removed


      Any ideas how to modify my code?


      Macro code and QVW you will find below.







      sub EmptyGroup
            set group = ActiveDocument.GetGroup("Test Group")
            set gp = group.GetProperties
            set vars = gp.FieldDefs

            for i = 0 to vars.Count -1
                 group.RemoveField i


            call AddGroup
      end sub


      sub AddGroup
            if ActiveDocument.Variables("vFY_CY").GetContent.String = "FY" then
                  group.AddField "FY1"
                  group.AddField "FY2"
                  group.AddField "FY3"
                  group.AddField "CY1"
                  group.AddField "CY2"
                  group.AddField "CY3"
            end if
      end sub