Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a quiz with an answer box and a check my answer box.
I put the code in and then when I put in the answer it says "wrong try again" and when I write nothing in the box then it says "correct well done."
The code I put in is below:
Private Sub CommandButton1_Click()
If TextBox1.Value = motherboard Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Private Sub CommandButton1_Click()
If TextBox1.Value = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Without Defining motherboard in another location, I am assuming you want the Literal answer to be the Text "motherboard" So you need to add quotes around the word.
The reason it accepts your answer when there isn't one is because when you add a variable that isn't defined it is defaulted with a value of nothing. So when you enter nothing it is a match to a variable that also equals nothing. By adding the quotes it makes it look for exactly that word.
You also might want to add some kind of filters to the incoming text to make it more likely for a correct answer that may have an extra space for a capital letter to be excepted.
Private Sub CommandButton1_Click()
If Trim(LCase(TextBox1.Value)) = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Adding the LCase command will make all entered text lower case. And Adding the Trim command will remove all white space before and after the word.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I Believe this question is very common but I am getting a very unique situation.
I have a code that I am using for delay delivery.
But the problem is I am unable to run the macro.
Public Sub Applicaion_Reminder(ByVal Item As Object)
Dim objPeriodicalMail As MailItem
If Item.Class = olTask Then
If InStr(LCase(Item.Subject), "send an email periodically") Then
Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
'Change the following email information as per your actual needs
With objPeriodicalMail
.Subject = "Email to Gmail"
.To = "bfarhan8#gmail.com"
.HTMLBody = "<HTML><BODY>It's a Test</HTML></BODY>"
.Importance = olImportanceHigh
.ReadReceiptRequested = True
.Send
End With
End If
End If
End Sub
When I hit on the run it asks me for Macro Name when I define a name it creates a new Sub.
If I remove the parameters of the
Application_Reminder()
to match the name with a macro name It gives an error on line number 3.
My question is how to run this Macro Properly. I searched the web but didn't find any useful help.
What you have missed is that macros can either be subroutines or functions and for each there are two major types.
A subroutine does something. Your Application_Reminder is a subroutine because it does something: send a reminder. A function can do something, but its real purpose is to return a value.
Some subroutines and functions need parameters, but some do not.
If I write a function Sqrt, the immediate question is: square root of what? I want to be able to write:
Answer = Sqrt(5)
That is, today I want the square root of 5. Tomorrow, I might want the square root of 7.
I would write:
Function Sqrt(ByVal Number as Double) as Double
‘ Code to calculate square root of Number
Sqrt = ResultOfCalculation
End Function
Almost all functions have parameters, but it is not essential. I could have a function, GetCurrentTemperature that reads a thermometer and returns a temperature. It would not need a parameter.
You have written a subroutine that has a parameter: Applicaion_Reminder(ByVal Item As Object). When you try to run Applicaion_Reminder, the interpreter wants to know what Item. I do not think the interpreter’s response is very sensible. It should have just told you, “You cannot run a subroutine with a parameter.”
You need a subroutine without a parameter that decides which Item is to be processed. With some computer languages, that subroutine must have the name Main. With VBA it can have any name.
That is, you need a subroutine like this:
Sub PickAnItemThatNeedsAReminder()
Dim Item as Object
‘ Code to set Item to the required MailItem
Call Applicaion_Reminder(Item)
End Sub
There are four distinct methods of selecting a MailItem. I imagine you scrolling down your Sent Items folder looking for emails to which you have not received a reply. When you find such an email, you run PickAnItemThatNeedsAReminderwhich sends a reminder.
Sub PickAnItemThatNeedsAReminder ()
Dim Exp As Explorer
Dim Item As Object
Set Exp = Outlook.Application.ActiveExplorer
If Exp.Selection.Count = 0 Then
Call MsgBox("Please select one or more emails then try again", vbOKOnly)
Exit Sub
Else
For Each Item In Exp.Selection
Call Applicaion_Reminder(Item)
Next
End If
End Sub
Exp.Selection is a list of all the currently selected emails. You can select as many emails as you want and them run PickAnItemThatNeedsAReminder. It will call Applicaion_Reminder for every selected email.
Additional Background
My belief is you have found a routine that runs off an event and have tried to adapt it to your requirements. Events are an incredibly useful feature of Outlook. However, if you do not yet understand that you cannot run a macro without a parameter, you are not yet ready for events. We say: walk before you run.
BraX and Super Symmetry would be correct in telling you to use ThisOutlookSession if you are going to use events. I have suggested you use Explorer (which is technically an event) but which is much easier for a beginner to understand than an application level event which is what you seemed to have found. With my approach, all your code can be in an ordinary module.
Application.Reminder event Occurs immediately before a reminder is displayed
set up Task Item with reminder then call your vba function - Applicaion_Reminder
See example on this answer
https://stackoverflow.com/a/40144594/4539709
if you want to call it with selected email then see Tony's answer
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a textbox of maxLength 8. the 1st two characters must be "PM" or "00". I tried split(), but didnt work.
Use substring() method
Dim s As String = TextBox1.Text.Substring(0, 2)
If s = "PM" Or s = "00" Then
MessageBox.Show("good!")
Else
MessageBox.Show("bad!")
End If
Or you can use StartsWith()
If TextBox1.Text.StartsWith("PM") OR TextBox1.Text.StartsWith("00") Then
'Do something
End If
Another option is to use regular expressions:
Dim re As New Regex("PM|00")
If re.IsMatch(TextBox1.Text) Then
'do something
End If
The benefit is that when you decide what to do with the other 6 characters, you can modify the above to capture and return those (in full or part), without having to rewrite your code. You can even process multiple occurrences of PM|00 in one string and capture them all.
Useful resource, a Regex sandbox:
https://www.regex101.com/
Maybe you can try this:
if textbox1.text like "PM*" or textbox1.text like "00*" then
Do something
else msgbox("You don't have pm or 00 to start with!")
end if
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Some basic terms that interchange that i have no one to ask, and i guess to stupid to understand my self.
Method,procedure, parameters, operation, operator, function, instaniantiantion, separator,
I really need help!
I really need to see a long module of code that every thing is broken down for a stupid guy like me.
For instance break this down, not what it does but how it does it, and the terminology. Word by word,, all dots, = parentheses, the passes the gets all that stuff, I know that this is simple to everyone else. And i will need more help until i get it... Maybe we can work something out. Some one that is available,,, that knows this stuff backwards and forwards and has the patience, that would like to help me... I have tried books i just do not get it HELP
Public Class ViewerForm1
Private Sub btnSelectPicture_Click(sender As Object, e As EventArgs) Handles btnSelectPicture.Click
'Show the open dialog box.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
'Load the picture in the box.
btnSelectPicturePicture.Image = Image.FromFile(ofdSelectPicture.FileName)
'Show the name of the file in the forms caption.
Me.Text = "Picture Viewer "(ofdSelectPicture.FileName)
'Close the window at exit the application.
Me.Close()
End If
End Sub
Private Sub btnSelectPicture_Click(sender As Object,
e As EventArgs) Handles btnSelectPicture.Click
This line declares a default Click event handler and binds button's click event to it.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
Opens an open file dialog and if user presses OK, processes the result.
btnSelectPicturePicture.Image = Image.FromFile(ofdSelectPicture.FileName)
Loads the image into the button, based on the file previously selected.
Me.Text = "Picture Viewer "(ofdSelectPicture.FileName)
Line does not make sense and probably won't compile. Missing a & or a + for string concatenation. Assuming it had one, it would assign a form caption/title to a constant value + a file name that was selected.
Me.Close()
Closes the current form.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write a macro that inserts "!!!" at the beginning of a textbox if there is a "XX" located in any part of that text box. Ideally the macro will run this procedure for every textbox in the presentation, but I can figure out how to loop it through if someone can help me with the basic procedure.
For example, a text box with the following text:
I ate XX hamburgers on XX/XX/20XX
would become
!!!I ate XX hamburgers on XX/XX/20XX
I hope this can help.
Sub test()
Dim TestString As String
TestString = "I ate XX hamburgers on XX/XX/20XX"
Variable = InStr(1, TestString, "X")
If Variable > 0 Then
output = "!! " & TestString
End If
Debug.Print output
End Sub
Here TestString = your input string
The InStr function tests if "X" is present in the string, if it is then "!!" is joined to the variable "Output"
This should be quite easy to adapt?
i hope this will help you.
Sub test()
Dim s As String
s = "Test XX"
If InStr(1, s, XX, vbTextCompare) Then
s = "!!!" + s
End If
MsgBox s
End Sub
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am making a hangman game for my son, in VB.net. I have made buttons for each letter, and i have also made a list of words. The problem I am encountering is when I am trying to print the guessed letters to the labels. I don´t know how to this. Can anyone help me please?
I will try to help you although you haven't show your code.
A simple approach is:
Private sWord As String 'Your word here
Dim arrayLetters As Array
arrayLetters = sWord.ToCharArray
For i = 0 To arrayLetters.Length - 1
Dim lbl As New Label
lbl.Text = "_"
lbl.Tag = arrayLetters(i)
lbl.AutoSize = True
Me.FlowLayoutPanel1.Controls.Add(lbl) ' Assuming that you have added a FlowLayoutPanel in your form to handle your labels (AutoSizeMode=GrowAndShrink)
Next
Now you need a sub to check the if the user has pressed the right letter:
Private Sub CheckLetter(ByVal letter As Char)
For Each lbl As Label In FlowLayoutPanel1.Controls
If lbl.Tag = letter Then
lbl.Text = letter
Else
'Whatever you like if the user make a mistake
End If
Next
End Sub
Now in the event that handles the buttons click
CheckLetter("Here you put the corresponding letter")
Of course you can have one event to handle all the letters (or even use keyboard for input),add capital letters etc.
Show us your efforts