Frame index

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Frame index

Post by jstevens »

Using VBA how does one return a "frame" index?

I have a number of frames that I would like to assign code to and have the code select specific code based on the frame selected. Similar to Excel's "select case".

Your advice is appreciated.
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Frame index

Post by HansV »

The selected frame is Selection.Frames(1)
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Frame index

Post by jstevens »

HansV wrote:The selected frame is Selection.Frames(1)
Hans,

So if I have 10 frames the index for each frame would be: Selection.Frames(1), Selection.Frames(2) and etc.?

Would the VBA code to identify the frame index then be: Msgbox Selection.Frames.Index ? The returned value would be 1, 2, 3,.... 10.
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Frame index

Post by HansV »

Do you really need the index of the selected frame, or do you need the selected frame object itself?
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Frame index

Post by jstevens »

Hans,

I created "frame 1" with checkboxes (Yes/No) and assigned VBA code to the "Entry" field option of each checkbox. The code (found it online) works great but does not work on the other frames.

Code: Select all

Sub MakeCheckBoxesExclusive()

Dim oField As FormField

For Each oField In Selection.Frames(1).Range.FormFields
    oField.CheckBox.Value = False
Next oField

Selection.FormFields(1).CheckBox.Value = True

End Sub
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Frame index

Post by HansV »

It appears to work for me. I have attached a very simple sample document with two frames, each with three check boxes. Your macro has been assigned to each of the check boxes.
FramesAndCheckBoxes.docm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Frame index

Post by jstevens »

Hans,

What version of Word are you using? I'm on 2010.

Is it possible to have the frame inside a table cell? I copied the frame and pasted it into the table cells. It appears that only the objects of the frame were pasted.
EL_70.png
You do not have the required permissions to view the files attached to this post.
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Frame index

Post by HansV »

I'm using Word 2019.

You cannot have a frame inside a table cell. A frame is a floating object.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Frame index

Post by jstevens »

Hans,

Since the two checkboxes are in table cells can the code be modified accordingly?
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Frame index

Post by HansV »

Simply change Selection.Frames(1) to Selection.Cells(1)
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Frame index

Post by jstevens »

Hans,

Thanks for the suggestion.
Regards,
John