#1
|
||||
|
||||
vbYesNo, my script only acts on Yes, doesn't do anyting on No? Help
Hello,
I need help. Trying to figure out what the heck is wrong with my script. I have an InputBox, to enter a text that I would find in a Word document. So basicly, if I find a word among a sentence, I wish to be asked Yes or No, if I should delete it. If I Click on Yes, it works fine, but if I click on No, it's stuck. That's it. I must do it on a dummy document to test it and must say Yes in order to stop the script. Is there anyone that could guide me where is my script going wrong? I'm also trying to encorporate vbYesNoCancel, but I don't know what to do, cause the button Cancel is there, but it doesn't act on it. Sub Delete_Sentence_if_Word_found() Dim strTexts As String Dim oRng As range Dim bFound As Boolean bFound = False strTexts = InputBox("Enter texts to be found here: ") Set oRng = ActiveDocument.range With oRng.Find .ClearFormatting .Text = strTexts .Forward = False .Wrap = wdFindStop .Format = False .MatchCase = False While .Execute bFound = True oRng.Expand Unit:=wdParagraph oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView Selection.range, True If MsgBox("Are you sure to delete the sentence?", vbYesNo) = vbYes Then Selection.Delete End If oRng.Collapse wdCollapseEnd Wend End With If Not bFound Then End If lbl_Exit: Set oRng = Nothing Exit Sub End Sub |
#2
|
||||
|
||||
Try the following
Code:
Sub Delete_Sentence_if_Word_found() Dim strTexts As String Dim oRng As Range strTexts = InputBox("Enter texts to be found here: ") Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting While .Execute(findText:=strTexts) oRng.Expand Unit:=wdSentence oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView oRng, True If MsgBox("Are you sure to delete the sentence?", vbYesNo) = vbYes Then oRng.Text = "" End If oRng.Collapse wdCollapseEnd Wend 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 |
#3
|
||||
|
||||
And to include a Cancel option by building on to Graham's code
Code:
Sub Delete_Sentence_if_Word_found() Dim strTexts As String, oRng As Range, iResponse As Integer strTexts = InputBox("Enter texts to be found here: ") Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting While .Execute(findText:=strTexts) oRng.Expand Unit:=wdSentence oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView oRng, True iResponse = MsgBox("Are you sure to delete the sentence?", vbYesNoCancel) If iResponse = vbYes Then oRng.Text = "" ElseIf iResponse = vbCancel Then Exit Sub End If oRng.Collapse wdCollapseEnd Wend End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
||||
|
||||
Thanks Graham :)
Thank you for your effort. I'm so grateful.
It's progress. It's almost to what I need. The danger in that script, is that it selects all the empty paragraph marks under it, & it doesn't pick up text List (text list meaning, it doesn't open the auto List fomat), but I do have titles beggining with numbers with tabs. So I ended up with numbers. I'm trying to include also the vbYesNoCancel. With your script, I've figured out how to managed mine a little bit. This is an example of titles numbers, text list. If you copy this in word, you will get paragraph marks where you see space below. ****If you use the script, to find (continued)**** ====> hoping to delete the whole sentence with the 1. tab and sentence.**** 1. Significant accounting policies (continued) 2. Significant accounting policies (continued) 3. Significant accounting policies (continued) 4. Significant accounting policies (continued) 5. Significant accounting policies (continued) This is my modified script that picks up text numbers, and moves forwards. But I can excecute Cancel, however, it does the same things as ''ansering No''. Any insights? Sub Delete_Sentence_if_Word_found() Dim strTexts As String Dim oRng As range Dim bFound As Boolean bFound = False strTexts = InputBox("Enter texts to be found here: ") Set oRng = ActiveDocument.range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = strTexts .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False While .Execute bFound = True oRng.Expand Unit:=wdParagraph oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView Selection.range, True If MsgBox("Are you sure to delete the sentence?", vbYesNoCancel) = vbYes Then Selection.Delete 'Else: Exit Sub 'End If 'oRng.Collapse wdCollapseEnd '.Execute If (vbYesNoCancel) = vbCancel Then Exit Sub End If End If oRng.Collapse wdCollapseEnd '.Execute Wend End With If Not bFound Then MsgBox "You have put a text that is not part of the document." & vbCr + vbCr _ & "Put another texte and try again.", vbInformation & vbOKOnly, "THE TEXT IS NOT IN THIS DOCUMENT" End If lbl_Exit: Set oRng = Nothing Exit Sub End Sub |
#5
|
||||
|
||||
wooohooo Guessed, Cancel works..... more progress
OK, Cancel works the way I was hoping, thank you so much.
However, as I was mentioning to Graham, it picks up, all the empty paragraph marks under the sentence, but it doesn't pick up the text list. So I end up with a pack of numbers. But with all our heads together, we can come up with a magical plan. I feel it. Thanks so much. How can I learn to do programming like an expert? Oh and by the way, now I work with Word 365. I've tried to update my profile info here, but the most I found was Word 2019. Not sure who is in charge of this forum. It's an great forum. I learn so much from you experts. I'm hoping one day to ''get it''. So fare for me, it 's trial and error. Searching for answers in Google. Thanks so much for guiding me Cendrinne |
#6
|
||||
|
||||
I'll be back tomorrow. Got to go to bed, working tomorrow, and have to get up early. Thanks for all your help and guidance.
|
#7
|
||||
|
||||
Please use the # to add code tags when posting code.
Both the code I posted and Andrew's Cancel addition will delete autonumbered texts, if those sentences complete a paragraph. If they don't then you will also need to remove the numbering e.g Code:
Sub Delete_Sentence_if_Word_found() Dim strTexts As String, oRng As Range, iResponse As Integer strTexts = InputBox("Enter texts to be found here: ", , "continued") Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting While .Execute(findText:=strTexts) oRng.Expand Unit:=wdSentence oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView oRng, True iResponse = MsgBox("Are you sure to delete the sentence?", vbYesNoCancel) If iResponse = vbYes Then oRng.Text = "" oRng.ListFormat.RemoveNumbers ElseIf iResponse = vbCancel Then Exit Sub End If oRng.Collapse wdCollapseEnd Wend End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub Without knowing what is in the paragraphs you can only program on the basis of what you do know.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
||||
|
||||
Ahhhh I was wondering where was the #. Just above LOL. OK noted, I'm truly sorry, I didn't know. Thank you both of you for all your help. I'm soooo impressed that you can code with ease. You are experts, that is for sure. I'm impressed.
I'll have to create a macro to delete single numbers with a tab and paragraph marks alone to remove all the numbers if alone. However, now it put's the numbers on the following sentence. But it's progressed. I'll try to figure out with my code and your codes to have it the way I hope to find it. Where to go to learn more on VBA coding? Tutorials? Any insights to understand it the way you guys do it? Thanks a million |
#9
|
|||
|
|||
Nothing is easy in Word if trying to deal with "Sentences" That is because Word doesn't see a sentence like you or I. Consider the code that Graham and Andrew have provided:
If you have this sentence: I gave my old dog to Mr. Smith and he renamed him Fido. If you run the code and enter "dog" as the word to find then you will be given the chance to delete "I gave my old dog to Mr." (not the entire sentence as we understand a sentence to be). You could have course add code to handle such things as Mr. Mrs. Dr. etc. but it is hit and miss. Also, you don't need the iResponse variable: Code:
Sub Delete_Sentence_if_Word_found() Dim strTexts As String, oRng As Range strTexts = InputBox("Enter texts to be found here: ", , "continued") Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting While .Execute(findText:=strTexts) oRng.Expand Unit:=wdSentence oRng.Select ActiveDocument.ActiveWindow.ScrollIntoView oRng, True Select Case MsgBox("Are you sure to delete the sentence?", vbYesNoCancel) Case vbYes oRng.Text = "" oRng.ListFormat.RemoveNumbers Case vbCancel: Exit Sub End Select oRng.Collapse wdCollapseEnd Wend End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub |
#10
|
||||
|
||||
Greg!!!!! Hello, it works better...
Hello, dear old friend,
I've tried it 2 different ways, by adding a text paragraph right below the non auto numbers titles, and having 4 empty paragraph marks below the non auto numbers titles. This time, it didn't delete all the empty empty paragraph marks, however, it did put the non auto numbers to the paragraph below it. So basicly, it doesn't take the whole sentence, just the text part. My macro took everything, but it didn't do what you guys amazingly did, to have it Skip cases when I've pressed on that No button, or to Cancel the macro when I press on that Cancel button. I'll try to figure out to merge all of these scripts to have it work. But we are making progress Thanks Greg |
#11
|
||||
|
||||
ahhh Got it. I know why it didn't select the numbers. It's partly my fault. I'm the one that said sentence, but since these are often title line, we could consider them Paragraphs.
So below, instead of : Code:
oRng.Expand Unit:=wdSentence Code:
oRng.Expand Unit:=wdParagraph Thank you, case solved |
Tags |
vbyesno help |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Script Directory | charlesdh | Excel Programming | 3 | 06-12-2018 01:36 PM |
Outlook VBA Script | kcm5153 | Outlook | 1 | 04-07-2015 11:41 PM |
Help with VBA script | nsyrax | Word VBA | 1 | 01-18-2014 03:38 AM |
the character "v" when typed acts like ctrl-v. | jim redfield | Word | 1 | 09-22-2012 05:19 AM |
Script Doesn't works on other machine | ravininave | Word | 1 | 01-05-2011 01:45 PM |