#1
|
|||
|
|||
Message box to input a value
Hello I have a very large document. When the document is opened I want a dialogue/message box to appear and into it the user will put a value 1-4. Each value has different paragraphs required. What I plan on doing say if 2 is selected when the file is opened - a macro will run and delete bookmarked text for input value 1, 3 and 4.
Would this be workable? Would I be better off using a dependent dropdown? thank you |
#2
|
||||
|
||||
Yes it would be workable but not particularly flexible. You can only use a bookmark name once so if there are multiple places to remove content then it would require lots of code. Also if the user makes the wrong choice there is no coming back without restarting the doc. I would hide text to make it non-destructive in the event of a bad selection - this does require the user's options set to not show hidden text.
I would be more inclined to use Content Controls and either the Tag or Title property on the dependent text so you can affect more than one text area. If the selection is allowed to show in the document then it could be used instead of a code-based dialog box. As an example, set up your doc with a dropdown Content Control titled Salesperson and then a bunch of Content Controls which use a title corresponding to one of those entries in Salesperson then code to hide CCs which don't have that title is Code:
Private Sub Document_ContentControlOnExit(ByVal myCC As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, sSales As String If myCC.Title = "Salesperson" Then sSales = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = aCC.Title <> sSales 'hide if CC title doesn't match selected salesperson Next aCC End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Reply
Apology if I'm replying incorrectly. Thank you "Guessed" for your reply and your suggestion to use CC sounds very good, only I've very basic knowledge of CC. Just to explain further, the document is very large, 30 + pages, it is an employment agreement for full time, part time and casual employees. Each employment type has different paragraphs to be inserted throughout the document. I do like the sound of a dropdown - that would be for each employment type, when the employment type is selected all relevant paragraphs will be presented throughout and those not relevant would be hidden? Is is going to be difficult for me? Is there somewhere I can get say sample code of how I would proceeds with this? Thank you for your response.
|
#4
|
||||
|
||||
You already have sample code but if you want a working sample, then you need to add a Dropdown Content Control titled "EmploymentType". Give it three entries for "Full time", "Part time" and "Casual"
Then select the text that only appears if Full time is selected and click the Plain Text CC button on your Developer Tab. Change the title of that CC to "Full time". Do the same for any other instances that need to be visible when Full time is selected. Do the same for the Part time and Casual sections of text. Put this code in your ThisDocument module. Code:
Private Sub Document_ContentControlOnExit(ByVal myCC As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, sET As String If myCC.Title = "EmploymentType" Then sET = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = aCC.Title <> sET 'hide if CC title doesn't match selected employment type Next aCC End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Confirmation of correct code
Hi Guessed, apology for querying again, could you please confirm I have the correct code below. I have put together a little test doc to test how it works. I created a content control dropdown, named it "EmploymentType". I created three options for the dropdown "Full time", "Part time" and "Casual". I then created plain text content control paras titling a couple "Full time" "Part time" "casual". I then copied your code to "this document" in VB. I also saved the document as a docm. Have I renamed the code correctly Guessed. I appreciate your assistance, apology I am new to this.
Private Sub Document_ContentControlOnExit(ByVal myCC As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, sET As String If myCC.Title = "Full Time" Then sET = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = myCC.Title <> sET 'hide if CC title doesn't match selected employment type Next aCC End If If myCC.Title = "Part Time" Then sET = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = myCC.Title <> sET 'hide if CC title doesn't match selected employment type Next aCC End If If myCC.Title = "Casual" Then sET = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = myCC.Title <> sET 'hide if CC title doesn't match selected employment type Next aCC End If End Sub |
#6
|
||||
|
||||
Don't use SET as a string variable name. SET is a reserved command name in VBA and will produce an error if used in this way Charge it to sSet of strSet.
Note also that content control titles are case sensitive so if the CC is called "Full time" the VBA needs to address it in the same way. Personally I don't like the use of hidden text except perhaps in a document the final version of which will be distributed either printed or as PDF. I prefer to fill content controls with the appropriate texts based on the selected value.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Thank you Graham, but I'm not sure what to use in place of "Set".
I will check your tips out and see if I can find my answer there. |
#8
|
||||
|
||||
You didn't need to change my code other than to change the sET to something else eg strET.
The dropdown Content Control needs the title EmploymentType As Graham says, hiding content is problematic if you are sending docs to other people without converting to PDF first. If you get it working as it is, I can explain how to permanently delete the hidden content.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Hey Guessed, I have got it working although it is not correct. Everything goes into hidden text. I did change the sSET to what Graham suggested.
I propose if I can get this going to create a macro to delete all hidden text. Any suggestions as to why everything turns to hidden text? I have checked that the names are all consistent within the VB code and the content controls. Also which I find a bit strange, even though the content control text is in hidden text (I can tell because of the dotted line underneath the text) - I can see it on the screen even though I have selection in File/Options do not show hidden text (box unticked). Here is the current code placed in "this document" : Private Sub Document_ContentControlOnExit(ByVal myCC As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, strSet As String If myCC.Title = "Full Time" Then strSet = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = aCC.Title <> strSet 'hide if CC title doesn't match selected employment type Next aCC End If If myCC.Title = "Part Time" Then strSet = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = aCC.Title <> strSet 'hide if CC title doesn't match selected employment type Next aCC End If If myCC.Title = "Casual" Then strSet = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls aCC.Range.Font.Hidden = aCC.Title <> strSet 'hide if CC title doesn't match selected employment type Next aCC End If End Sub thank you for your assistance, sorry about keep coming back - I think the error is something very simple, but I'm unable to see it. |
#10
|
||||
|
||||
You changed my code. If I meant for you to edit all the code I would have created it that way.
Look back at the code I provided and try to understand what it is doing. Or post your document so I can fix up the changes to align with your Content Controls.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Hey Guessed, thank you for offering to take a look at this and hopefully fix. This file is very small - I wanted to see it working before I started the process on the large doc. Thank you.
|
#12
|
||||
|
||||
Replace the code in the ThisDocument module with this
Code:
Private Sub Document_ContentControlOnExit(ByVal myCC As ContentControl, Cancel As Boolean) Dim aCC As ContentControl, strSet As String If myCC.Title = "EmploymentType" Then strSet = myCC.Range.Text For Each aCC In ActiveDocument.ContentControls If aCC.Title <> "EmploymentType" Then aCC.Range.Font.Hidden = aCC.Title <> strSet 'hide if CC title doesn't match selected employment type End If Next aCC End If ActiveWindow.View.ShowAll = False ActiveWindow.View.ShowHiddenText = False End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#13
|
|||
|
|||
Thank you Guessed - that worked great. There is a lot of space where the text was converted to hidden text. Is it possible for a macro to automatically run to delete the hidden text once the dropdown has done its job?
Thank you again. |
#14
|
||||
|
||||
The space is appearing because the text is inside the Content Control but the paragraph mark is not.
You could remove the trailing paragraph marks to position all three CCs consecutively in the same paragraph (only one appears at a time) Or Convert the plain text CCs into Rich Text CCs which do allow the paragraph mark to be inside the CC. The way you do it in your final doc depends on the layout you are trying to achieve.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#15
|
|||
|
|||
Hi Guessed, I am getting an error message which I've attached. It will work sometimes but mostly I will get the attached dialogue error.
Converting the plain text to rich text may be a bit cumbersome? Not sure. The doc will be used by basic word users. Can you or someone else suggest another method I could try to get this going please? Note that each selection on the dropdown contains about 35 pages. Thank you. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Create Custom Form in Outlook 2016, for the Message setting 'Do not include original message' | jsvictor1989@gmail.com | Outlook | 0 | 01-30-2019 10:54 PM |
trouble with input box | gummybear | Excel Programming | 0 | 12-04-2018 06:39 AM |
Need Help on Input Video into PPT | agus | PowerPoint | 2 | 07-13-2015 02:25 AM |
Message replys are shown at the bottom of the message, how can I get them to the top | Gardy | Outlook | 3 | 05-23-2012 01:20 AM |
Input Box | ubns | Word | 2 | 04-13-2012 06:28 AM |