Jason_S
02-21-2017, 04:36 PM
What I'm using: Word For Mac 2011
What I am trying to: Insert programmatically IN THE REVIEWING PANE, IN A COMMENT, a macrobutton field that when clicked passes an argument to global macro. Yes, I know that Macrobutton cannot natively pass arguments. For workarounds, I reviewed a helpful post on wordmvp which can be viewed searching google for "wordmvp using macro button"
The code below works fine in the main body of the Agreement. But it does not work in when the macrobutton field is entered into a comment (I am viewing comments and testing this code via the Reviewing Pane). The reason it doesn't work *appears* to be that WORD can't see the nested private field in the reviewing pane (again, Word sees private field just fine in document section).
I have also tried using "AddIn" field, but that too doesn't appear to work for Macrobutton field inserted into a Comment either (Again, second field appears to be unreadable when in comment/reviewing pane). My problem may be in the subroutine named ShowHiddenFieldData, and the Selection.fields(2) line because selection objects seem to be unstable in the comments/Reviewing Pane. I also note there seems to be some instability in toggling fields codes in the reviewing pane. Advice would be appreciated.
Two Questions: (1) Can any of you repeat my error in Word (PC - any version)? That is, first move cursor to place in main document, run test_InsertNestedField to insert MacroButton field with nested Private field, then double click on Macrobutton to run ShowHiddenFieldData macro which should return the "hidden" text from the Private field nested in the Macrobutton field. This should work just fine. Then click "Insert Comment" and in the reviewing pane, repeat the steps again. This is not working; I get "run time error 5941 Requested Member of Collection Does not Exist."
(2) If you can repeat my error, please suggest some work-arounds or alternatives. I need to be able to insert dozens of these macrobuttons in the COMMENTS SECTION, and when the buttons are clicked I need to pass an ID number to a macro that will use that ID to fetch data that will then populate a user form for further action by the user. (e.g. in comment #14, Macrobutton "Click Here to See detailed rules and policies about X")
========Code: following below are three (3) separate sub-routines
Function Function_InsertPrivateWithinMacroButton2(PrivateText As String, PublicText As String)
Dim MyField1 As Field
Dim MyField2 As Field
ActiveWindow.View.ShowFieldCodes = False
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = False
Set MyField1 = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, preserveformatting:=False)
MyField1.Code.Select
Selection.Characters.First.Delete
MyField1.Code.Select
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
Selection.InsertAfter "Private " & PrivateText
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set MyField2 = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, preserveformatting:=False)
MyField2.Code.Select
Selection.Characters.First.Delete
MyField2.Code.Select
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
Selection.InsertAfter "MACROBUTTON ShowHiddenFieldData " & PublicText
MyField2.Update
MyField2.Select
Selection.Collapse wdCollapseEnd
Application.ScreenUpdating = True
End Function
' ============ Sub-Routine 2 below
Sub test_InsertNestedField()
Dim PrivateText As String
Dim PublicText As String
PrivateText = "This is PRIVATE Text."
PublicText = "Double Click Here to launch ShowHiddenFieldData macro"
Function_InsertPrivateWithinMacroButton2 PrivateText, PublicText
End Sub‹
' ============ Sub-Routine 3 below
Sub ShowHiddenFieldData()
' This subroutine tests the presence of the private field supposed to be hidden as nested field with macrobutton
Dim MacroData As String
MacroData = Selection.Fields(2).Code
Msgbox ("Unparsed Macro Data is " & MacroData)
End Sub
What I am trying to: Insert programmatically IN THE REVIEWING PANE, IN A COMMENT, a macrobutton field that when clicked passes an argument to global macro. Yes, I know that Macrobutton cannot natively pass arguments. For workarounds, I reviewed a helpful post on wordmvp which can be viewed searching google for "wordmvp using macro button"
The code below works fine in the main body of the Agreement. But it does not work in when the macrobutton field is entered into a comment (I am viewing comments and testing this code via the Reviewing Pane). The reason it doesn't work *appears* to be that WORD can't see the nested private field in the reviewing pane (again, Word sees private field just fine in document section).
I have also tried using "AddIn" field, but that too doesn't appear to work for Macrobutton field inserted into a Comment either (Again, second field appears to be unreadable when in comment/reviewing pane). My problem may be in the subroutine named ShowHiddenFieldData, and the Selection.fields(2) line because selection objects seem to be unstable in the comments/Reviewing Pane. I also note there seems to be some instability in toggling fields codes in the reviewing pane. Advice would be appreciated.
Two Questions: (1) Can any of you repeat my error in Word (PC - any version)? That is, first move cursor to place in main document, run test_InsertNestedField to insert MacroButton field with nested Private field, then double click on Macrobutton to run ShowHiddenFieldData macro which should return the "hidden" text from the Private field nested in the Macrobutton field. This should work just fine. Then click "Insert Comment" and in the reviewing pane, repeat the steps again. This is not working; I get "run time error 5941 Requested Member of Collection Does not Exist."
(2) If you can repeat my error, please suggest some work-arounds or alternatives. I need to be able to insert dozens of these macrobuttons in the COMMENTS SECTION, and when the buttons are clicked I need to pass an ID number to a macro that will use that ID to fetch data that will then populate a user form for further action by the user. (e.g. in comment #14, Macrobutton "Click Here to See detailed rules and policies about X")
========Code: following below are three (3) separate sub-routines
Function Function_InsertPrivateWithinMacroButton2(PrivateText As String, PublicText As String)
Dim MyField1 As Field
Dim MyField2 As Field
ActiveWindow.View.ShowFieldCodes = False
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = False
Set MyField1 = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, preserveformatting:=False)
MyField1.Code.Select
Selection.Characters.First.Delete
MyField1.Code.Select
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
Selection.InsertAfter "Private " & PrivateText
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set MyField2 = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, preserveformatting:=False)
MyField2.Code.Select
Selection.Characters.First.Delete
MyField2.Code.Select
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
Selection.InsertAfter "MACROBUTTON ShowHiddenFieldData " & PublicText
MyField2.Update
MyField2.Select
Selection.Collapse wdCollapseEnd
Application.ScreenUpdating = True
End Function
' ============ Sub-Routine 2 below
Sub test_InsertNestedField()
Dim PrivateText As String
Dim PublicText As String
PrivateText = "This is PRIVATE Text."
PublicText = "Double Click Here to launch ShowHiddenFieldData macro"
Function_InsertPrivateWithinMacroButton2 PrivateText, PublicText
End Sub‹
' ============ Sub-Routine 3 below
Sub ShowHiddenFieldData()
' This subroutine tests the presence of the private field supposed to be hidden as nested field with macrobutton
Dim MacroData As String
MacroData = Selection.Fields(2).Code
Msgbox ("Unparsed Macro Data is " & MacroData)
End Sub