Applescript how to display a dialog with text and a variable - variables

I need to display a applescript dialog with text and a number by which i mean the text that it displays would be text and a varible like this display dialog "Enter spelling" and repeatnum buttons{"done"} This is the code I have`isplay dialog "How many spellings are there" buttons {"That amount"} default answer ""
set amount to text returned of result
set repeatdone to 0
repeat amount times
repeatdone = repeatdone + 1
display dialog "enter spelling " and repeatdone buttons {"Ok"} default answer ""
end repeat
When I try this it gives me the error cannot make enter spelling into type boolean, Is there anyway to make a applescript dialog display text and a varible

Try:
display dialog "How many spellings are there" buttons {"That amount"} default answer ""
set amount to text returned of result
set repeatdone to 0
repeat amount times
set repeatdone to repeatdone + 1
display dialog "enter spelling " & repeatdone buttons {"Ok"} default answer ""
end repeat

Related

How to call the MsgBox Function

How do I “Call” Vba’s MsgBox function? Is this the same as just using the actual function itself? I want to make a MsgBox function that returns the date and time.
Like this:
MsgBox "It is now " & Format(Now, "yyyy-mm-dd hh:nn"), vbInformation + vbOKOnly, "Current Date and Time"
Is this the same as just using the actual function itself ?
Yes, you may consider it as a regular function in VBA.
I want to make a MsgBox function that returns the date and time
The MsgBox function is for displaying questions or warnings and etc. to users and returning answers like Yes, No, Cancel and etc. Strictly speaking the function displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.
If you need to let users enter any information on on the dialog box you need to use the InputBox function instead. It displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box. You may then parse the date entered on the dialog box and use it in the code. For example:
Dim Message, Title, Default, MyValue
Message = "Enter a value between 1 and 3" ' Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "1" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
' Display dialog box at position 100, 100.
MyValue = InputBox(Message, Title, Default, 100, 100)

How to add message box VbCancel? [duplicate]

This question already has answers here:
Trouble with InputBoxes
(5 answers)
Closed 4 months ago.
I have found the Macro for MS Word (as below) from the website https://excelchamps.com/blog/vba-code-search-google-chrome/
Sub GoogleSearch()
Dim chromePath As String
Dim search_string As String
Dim query As String
query = InputBox("Please enter the keywords", "Google Search")
search_string = query
search_string = Replace(search_string, " ", "+")
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url http://www.google.com/search?hl=en&q=" & search_string)
End Sub
I would like to close the Message Box when I mistakenly press the Macro button/ran this Macro. However, whatever I press (Yes/No/Close that msgbox), it ignored all of these choices and automatically search what I type in the msgbox.
I tried to add the below code but it doesn't work.
If MsgBoxResult = vbCancel Then
Exit Sub
Else
I am junior to Word Macro. May I ask if anyone have any suggestions to make the msgbox work? (press cancel or close the window and then don't open Chrome and search automatically) Thank you for your kind attention.
Just have a look to the official documentation for InputBox
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
(...)
If the user chooses OK or presses ENTER, the InputBox function returns whatever is in the text box. If the user chooses Cancel, the function returns a zero-length string ("").
So (in opposite to MsgBox), the InputBox always returns a String, while vbCancel is a number. You will need to check if the return value is the empty string. Note that you can't distinguish between "the user didn't enter something and pressed [OK]" or "the user pressed [Cancel]".
If MsgBoxResult = "" Then Exit Sub

Removing Combo Box value without having to lose focus

I have a combo box with a list of customers which I am filtering as the user types.
When the form loads there is a preset value in the combo box, however when I backspace the combo box doesn't update the value (it keeps the original customer). The value will only become null once I click away from the combo box. Then I click back and it starts filtering as I type.
I tried setting the focus to another control and then coming back to the combo box on the AfterUpdate events but the code doesn't change the focus
Private Sub cboCustCode_AfterUpdate()
Me.txtCustName = DLookup("[CUST_NAME]", "[PLACE_HOLDER_CUST_LIST]", "[CUST_CODE] = '" & Me.cboCustCode & "'")
Me.txtCustName.SetFocus
Me.cboCustCode.SetFocus
End Sub
I want to be able to backspace in the combo box and then start typing and see the filtered results
I fixed it by checking if the combo box text length is 0 and then setting the combo box value = null
Private Sub cboCustCode_Change()
Me.txtCustName = DLookup("[CUST_NAME]", "[PLACE_HOLDER_CUST_LIST]", "[CUST_CODE] = '" & Me.cboCustCode & "'")
If Len(Me.cboCustCode.Text) = 0 Then
Me.cboCustCode = Null
End If
End Sub

Check for an empty Text Box

I have a text box in a Word 2016 document. This is not a TextBox form object but a normal Text Box that you insert from the Insert tab here:
I am trying to check if it is empty when the document is opened. For the life of me I cannot figure out how to do this. I have tried all of the following:
If (Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text & vbNullString) = 0) Then
If (IsNull(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) Then
If (LTrim(RTrim(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) = "") Then
If (ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text = "") Then
None of these return true when the text box is empty? This is the text box:
It seems that the text box always contains a paragraph marker (which I can't delete). This is what the VBA watch shows:
Watch : : ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text : "
" : String : ThisDocument.Document_Open
Note that the watch is two separate lines which makes me think that there's CRLF or something in there?
There is always a paragraph break in an otherwise empty textbox. Accordingly, you could use something along the lines of:
Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2")
If Not .TextFrame Is Nothing Then
If Trim(.TextFrame.TextRange.Text) = vbCr Then
MsgBox "Empty Text Range"
End If
Else
MsgBox "No Text Range"
End If
End With
End Sub

Delete Selected Text in a Text Box

Hey fellow Stackoverflow users,
i'm trying to create a text editor with HTML tag functions (for export) and can't get a solution working. Therefor i created a textbox where the user should be able to insert the text. For this insert function i need the in the title described function to delete the selected text (only the selected text, the text around it stays) inside the textbox. Even SendKeys won't function right.
Please let me know if anyone got an idea, thanks upfront!
EDIT: Here's the corrected code for the bold button with maintextbox as textbox for the user's text:
Private Sub BoldButton_Click()
If maintextbox.SelLength = 0 Then
MsgBox ("Please highlight the text you want to edit!")
Else
SelectionText = maintextbox.SelText MsgBox ("SelText: " & SelectionText)
maintextbox.SelText = "<b>" & _
SelectionText & _
"</b>"
End If
End Sub
Simply:
TextBox.SelText = ""
The .Sel* methods all relate to the text currently selected within the control. SelText = "" replaces the current selection with nothing, thereby deleting it whilst preserving the surrounding unselected text.