none
Problems trying to run macros when sections are unprotected and include checkboxes... RRS feed

  • Question

  • I would appreciate som VBA for Word help here. I am running into problems due to my limited knowledge of this scripting.

    I have a document template that has a different header on page 1 to the rest of the document. This is defined as Section 1 of the document. The section break applied after the header says Section Break (Next Page).
    In this Header 1 there is an image.
    There are 4 macros, and depending on what macro is executed, the existing image is removed and replaced by an another (appropriate) image.
    All this works fine.

    In Section 2 I have a set of checkboxes, these can be selected/deselected.
    When inserting these as Word fields I need to unprotect this Section 2 of the document, otherwise the user cannot select/deselect the fields in Word. To allow this I unprotect Section 1 and Section 2 so the user can run the macros and make selections in the checkboxes without any restrictions.
    Now for some reason the macros will not run because they say the document has been protected, but I have excluded Section 1 and Section 2 from the protection. The macros for the graphic insertion fail here with a  Runtime error 4605 - command is not available...:

    Selection.HomeKey Unit:=wdStory
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

    The idea here as I understand it is to locate the Header section on the first page where the image is to be inserted.

    To work around this I inserted ActiveX control checkboxes. These I can select/delesect and also run the macros as intended. But the problem I am now facing is that the actual size of the checkboxes in the document are minimal and cannot be scaled (?), whereas when using fields they were was scaleable by font size.


    Questions ...

    1. How can I scale the size of the ActiveX checkbox in the document?
    2. is there some obvious error here regarding the location of the Header 1 when using unprotected areas?


    thanks
    Robin

    Thursday, September 6, 2012 1:03 PM

Answers

  • Hi Robin

    OK, I've provided a 2003 *.dot template with the macro button fied, AutoText entries and macros. You should be able to access it from a Shared folder on SkyDrive:
    [file no longer available; link removed]

    Press Alt+F9 to view the Macrobutton fields. You want the one in the Comments cell of the table that's used to lay out the page.

    You'll see that there's an AutoText field embedded in the MacroButton field and the name of the macro that's called (ChangeCheckbox). That macro changes the name of the AutoText entry, which toggles between Symbol characters for checked and unchecked checkboxes. These are stored in the template.

    The AutoNew macro at the top of the code module will execute when a new document is created from the template. This sets the number of clicks required to trigger the MacroButton from a double-click to a single click.

    Whether you want to use a UserForm or not will depend on which way the user will feel more comfortable working with the document. The approach required will basically be the same...


    Cindy Meister, VSTO/Word MVP


    Thursday, September 6, 2012 4:02 PM
    Moderator
  • Hi Robin

    The traditional way to set "targets" in a Word document is to use bookmarks. See, for example, http://word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm and http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

    I'm not sure how you want the dialog box to interact with the document. Usually, the interaction takes place when the user clicks "OK". Then the macro checks each control on the form and writes the information to the target area.

    It is also possible to use events linked to the controls on the form to trigger code execution. A checkbox has a Click event, for example. Double-click on a checkbox to have the VBA editor generate the "sub" for that.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 7, 2012 2:25 PM
    Moderator
  • Hi Robin

    The ActiveX checkbox (and other controls) were developed for use on VBA UserForms. Making them available to place on the document surface was something of an after-thought and they are certainly not optimized for that purpose. There is no way to change or scale the size of an ActiveX checkbox.

    Forms protection applies to the entire document and locks certain parts of the document, even when some sections are set as unprotected. An example of things that are not available, even in unprotected sections, are graphics with text flow formatting and the document's headers and footers. This is because these things are managed at the section and document levels - levels that do not become unprotected when a section is unprotected.

    In which version of Word will this form be used? If it's 2010 then you could use a content control checkbox.

    Another possibility would be to use a technique that Microsoft used in its old fax forms: a MacroButton field with a checkbox as the display for the field. The macro linked to the field would exchange a checked check box for an unchecked checkbox. The checkboxes were stored as AutoText entries in the template. If you're working with Word 2003 or earlier you should have access to one of those old templates and be able to re-use those elements.

    It would also be possible for you to protect the section with the form checkboxes. The macro to manipulate the image in the header would have to deal with unprotecting, making the change, then re-protecting the document.


    Cindy Meister, VSTO/Word MVP

    Thursday, September 6, 2012 2:01 PM
    Moderator

All replies

  • Hi Robin

    The ActiveX checkbox (and other controls) were developed for use on VBA UserForms. Making them available to place on the document surface was something of an after-thought and they are certainly not optimized for that purpose. There is no way to change or scale the size of an ActiveX checkbox.

    Forms protection applies to the entire document and locks certain parts of the document, even when some sections are set as unprotected. An example of things that are not available, even in unprotected sections, are graphics with text flow formatting and the document's headers and footers. This is because these things are managed at the section and document levels - levels that do not become unprotected when a section is unprotected.

    In which version of Word will this form be used? If it's 2010 then you could use a content control checkbox.

    Another possibility would be to use a technique that Microsoft used in its old fax forms: a MacroButton field with a checkbox as the display for the field. The macro linked to the field would exchange a checked check box for an unchecked checkbox. The checkboxes were stored as AutoText entries in the template. If you're working with Word 2003 or earlier you should have access to one of those old templates and be able to re-use those elements.

    It would also be possible for you to protect the section with the form checkboxes. The macro to manipulate the image in the header would have to deal with unprotecting, making the change, then re-protecting the document.


    Cindy Meister, VSTO/Word MVP

    Thursday, September 6, 2012 2:01 PM
    Moderator
  • Hi Cindy,

    Thanks for the answer - it expalins some of the issues. I am working in Word 2007, mainly because this version was used for previous macro development and something that I knew a while ago. It seems like there is no simple way of doing what I want to do.

    I was considering a user form where the user can select the options they want to be "checked" on the form, effectively inserting or removing a check in a document field. I guess this is similar to what you were referring to in some of the above examples. I have to try and work my way through this with some simple examples. I'm not really concerned about having any document protection at all, so it could all be done by simple field setting/resetting if that is allowed from a user form.

    thanks

    Robin

    Thursday, September 6, 2012 2:42 PM
  • Hi Robin

    OK, I've provided a 2003 *.dot template with the macro button fied, AutoText entries and macros. You should be able to access it from a Shared folder on SkyDrive:
    [file no longer available; link removed]

    Press Alt+F9 to view the Macrobutton fields. You want the one in the Comments cell of the table that's used to lay out the page.

    You'll see that there's an AutoText field embedded in the MacroButton field and the name of the macro that's called (ChangeCheckbox). That macro changes the name of the AutoText entry, which toggles between Symbol characters for checked and unchecked checkboxes. These are stored in the template.

    The AutoNew macro at the top of the code module will execute when a new document is created from the template. This sets the number of clicks required to trigger the MacroButton from a double-click to a single click.

    Whether you want to use a UserForm or not will depend on which way the user will feel more comfortable working with the document. The approach required will basically be the same...


    Cindy Meister, VSTO/Word MVP


    Thursday, September 6, 2012 4:02 PM
    Moderator
  • Hi Cindy,

    Excellent. Thanks I am going to have a look at it, I have it on my PC now.

    Robin

    Thursday, September 6, 2012 4:13 PM
  • Hi Cindy,

    I am a bit stumped now on getting the ActiveX checkbox selection in a VBA user dialog to reference a speciic placement of the macro in my Word doc! I will be using the macro at approximately 30 different locations in the doc, but how do I get the use dialog selection to reference a specific macro?

    thanks

    Robin

    Friday, September 7, 2012 9:40 AM
  • Hi Robin

    The traditional way to set "targets" in a Word document is to use bookmarks. See, for example, http://word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm and http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

    I'm not sure how you want the dialog box to interact with the document. Usually, the interaction takes place when the user clicks "OK". Then the macro checks each control on the form and writes the information to the target area.

    It is also possible to use events linked to the controls on the form to trigger code execution. A checkbox has a Click event, for example. Double-click on a checkbox to have the VBA editor generate the "sub" for that.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 7, 2012 2:25 PM
    Moderator