#1
|
|||
|
|||
How to display a message box if a box is selected
Hello,
I'm trying to have a message box displayed if a box is ticked. Currently, if the box is ticked is displays text within the word doc- see code If Me.chkVisa = True Then Selection.GoTo What:=wdGoToBookmark, Name:="VisaHeader" Selection.TypeText "28. Visa Sponsorship" Selection.GoTo What:=wdGoToBookmark, Name:="VisaText" Selection.TypeText "As an employee from outside the European Union you will require to have a company sponsored visa before commencing employment. The company will work with an appointed immigration specialist to ensure the correct clearance to work in the United Kingdom." Selection.GoTo What:=wdGoToBookmark, Name:="VisaText2" Selection.TypeText "On completion of your probationary period, were you to leave the company within 24 months of your visa start date you will be required to pay back a percentage of the costs associated with obtaining the company sponsorship visa." End If I also want it to display a pop up message. I had written something like this but it doesn't work- Private Sub Messagebox() If Me.chkVisa = True Then MsgBox ("Hello") End If End Sub Thanks Ross |
#2
|
|||
|
|||
It's ok- managed to solve it
|
#3
|
||||
|
||||
It is better to avoid using the Selection object - especially if the bookmark is in a header, and as you may have realised you can simply call the message box from your code. The following uses a function to write to the bookmark and reset the bookmark to the new text, so it is easy to replace or remove the text. The bookmarks are accessed by name.
Code:
Sub chkVisa_Click() If Me.chkVisa = True Then FillBM "VisaHeader", "28. Visa Sponsorship" FillBM "VisaText", "As an employee from outside the European Union you will require to have a company sponsored visa before commencing employment. The company will work with an appointed immigration specialist to ensure the correct clearance to work in the United Kingdom." FillBM "VisaText2", "On completion of your probationary period, were you to leave the company within 24 months of your visa start date you will be required to pay back a percentage of the costs associated with obtaining the company sponsorship visa." Else FillBM "VisaHeader", "" FillBM "VisaText", "" FillBM "VisaText2", "" End If MsgBox "Hello" End Sub Private Sub FillBM(strBMName As String, strValue As String) 'Graham Mayor Dim oRng As Range With ActiveDocument On Error GoTo lbl_Exit Set oRng = .Bookmarks(strBMName).Range oRng.Text = strValue oRng.Bookmarks.Add strBMName End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
Hi Graham.
Thanks for your reply. I understand the first part of the code. It's the second part from Private Sub FillBM that I don't what it is saying? Also, you said it makes easy to reset the bookmark to the new text? with the code I have written, If I wanted to change the text I was just deleting it and entering the new text so wanted to know what the difference was? Thanks again. VB knowledge is basic but like to know this stuff. Thanks Ross |
#5
|
||||
|
||||
The Private Sub FillBM is simply a means of writing a value into a named bookmark without destroying the bookmark. It is called from the first macro to set the values in three bookmarks according to the setting of the check box in the userform.
Basically it sets a range to the bookmarked range, writes a new value to the range, then resets the bookmark to the modified range. As the Function is marked 'Private' it must be in the same module as the macro that calls it. If you manually delete text, there is every possibility that you will also delete the bookmark. If you want to change the value in a bookmark, call the function with a new value FillBM BookmarkName, Value
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Ok, Thanks
So would you do it the same way if you asking the user to input the text? As I had, Private Sub cmdOk_Click() Selection.GoTo What:=wdGoToBookmark, Name:="FirstName" Selection.TypeText Me.txtFirstName Unload Me End Sub |
#7
|
||||
|
||||
Absolutely
Code:
Private Sub cmdOk_Click() FillBM "FirstName", Me.txtFirstName.Text Unload Me End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
|||
|
|||
Hi Graham,
Thanks for your help. Got another question. I have the following code and when the check box is selected the text is added to the doc and what I want to know is there a way of entering in the relocation amount, with say, a pop up input box at the end? Sub chkRelocationAssistance_Click() If Me.chkRelocationAssistance = True Then FillBM "Relocation1", "You are eligible for financial assistance through the Relocation Policy, a copy of which is enclosed. On receipt please will you sign Appendix 1,2 and 3 policy and pass to your manager on your first day. The assistance will be capped at £; which will be tax free in line with HMRC(Her Majesty's Revenue and Customs) regulations." FillBM "Relocation2", "You have been offered relocation assistance based on your particular circumstances and for this reason, we ask that you do not discuss this matter with your work colleagues and your absolute discretion is appreciated." FillBM "Relocation3", "Relocation Policy" Else FillBM "Relocation1", "" FillBM "Relocation2", "" FillBM "Relocation3", "" End If End Sub Thanks Ross |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Problem opening Outlook Today. Error Message: Cannot display the folder. Cannot find this file. Veri | atwnsw | Outlook | 0 | 11-01-2015 05:05 PM |
How to Display Arrow for Drop Down Lists in Message Body | hue2 | Outlook | 0 | 03-20-2015 12:24 PM |
Pivot Table Question- Can the table display in the order the boxes were checked/selected? | blackjack | Excel | 0 | 09-13-2014 08:57 PM |
Option Button selected will display text | ksigcajun | Word VBA | 7 | 07-14-2014 09:31 AM |
Display paragraph of text based on value selected in combo | WordWaza | Word | 0 | 08-09-2013 06:30 AM |