Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Macro to remove and add new fields to a group

Hi,

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.

Regards,

Michi

********************

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
      next

      call AddGroup
end sub

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

1 Solution

Accepted Solutions
s_uhlig
Partner - Creator
Partner - Creator

Hi Michi,

for i = vars.Count -1 to 0 step -1

        group.RemoveField i

next

seems to work.

Regards

Sven

PS.: to immediately reflect the changes - call

group.Cycle 1

group.Cycle -1

after: call AddGroup

View solution in original post

3 Replies
Not applicable
Author

Hi,

so nobody can help me?

Or is it too trivial?


Regards

s_uhlig
Partner - Creator
Partner - Creator

Hi Michi,

for i = vars.Count -1 to 0 step -1

        group.RemoveField i

next

seems to work.

Regards

Sven

PS.: to immediately reflect the changes - call

group.Cycle 1

group.Cycle -1

after: call AddGroup

Not applicable
Author

Hi Sven,

thanks for your help.

It works like i hoped.

Regards

Michael