#1
|
|||
|
|||
new to vba - go to end of sub
In this simple search for a word, I want to end the sub if the word is not in the document and not type Regards.
Sub Macro2() ' ' Macro2 Macro ' ' Selection.Find.ClearFormatting With Selection.Find.Font .Bold = False .Italic = False End With Selection.Find.Replacement.ClearFormatting With Selection.Find.Replacement.Font .Bold = False .Italic = False .Underline = wdUnderlineNone End With With Selection.Find .Text = "test" .Replacement.Text = "test replaced" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.EndKey Unit:=wdLine Selection.TypeText Text:= _ "Regards," End Sub |
#2
|
|||
|
|||
Look up GoTo.
Code:
Sub SearchTest() ' Charles Kenyon ' If word not found skip rest of macro Const strWORDTOFIND As String = "Test" With ActiveDocument.Range.Find .Text = strWORDTOFIND If .Found = False Then GoTo endofsub End With MsgBox strWORDTOFIND & " is present." endofsub: End Sub You could also use: Code:
If .Found = False Then Exit Sub |
#3
|
|||
|
|||
Thanks Charles. I am really a beginner so please excuse my ignorance. The doc is a letterhead and the body has the word salutation. When I step into, even though the word salutation is there it skips right to endofsub: without giving "MsgBox strWORDTOFIND & " is present." " . Your kind help is appreciated.
Sub salutation() ' ' ' ' Charles Kenyon ' If word not found skip rest of macro Const strWORDTOFIND As String = "salutation" With ActiveDocument.Range.Find .Text = strWORDTOFIND If .Found = False Then GoTo endofsub End With MsgBox strWORDTOFIND & " is present." Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting With Selection.Find.Font .Bold = False .Italic = False End With Selection.Find.Replacement.ClearFormatting With Selection.Find.Replacement.Font .Bold = False .Italic = False .Underline = wdUnderlineNone End With With Selection.Find .Text = "salutation" .Replacement.Text = " " .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute With Selection If .Find.Forward = True Then .Collapse Direction:=wdCollapseStart Else .Collapse Direction:=wdCollapseEnd End If .Find.Execute Replace:=wdReplaceOne If .Find.Forward = True Then .Collapse Direction:=wdCollapseEnd Else .Collapse Direction:=wdCollapseStart End If .Find.Execute End With Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.TypeText Text:= _ "It is a pleasure ...." endofsub: End Sub |
#4
|
|||
|
|||
If someone else does not, I will tackle this tomorrow. Your recorded macro should be written using the Range object rather than the selection object.
You really should consider using a UserForm. Why are you using a macro for this at all rather than a letter template? |
#5
|
|||
|
|||
I forgot to execute the find to fill the found property.
.Execute Code:
Sub SearchTest() ' Charles Kenyon ' If word not found skip rest of macro Const strWORDTOFIND As String = "Test" With ActiveDocument.Range.Find Let .Text = strWORDTOFIND .Execute If .Found = False Then GoTo endofsub End With ' Rest of macro here MsgBox strWORDTOFIND & " is present." endofsub: End Sub |
#6
|
||||
|
||||
Based on the original code The following should do the trick
Code:
Sub salutation() 'Graham Mayor - https://www.gmayor.com - Last updated - 19 Aug 2020 Dim oRng As Range Dim bFound As Boolean Const strWORDTOFIND As String = "salutation" Set oRng = ActiveDocument.Range With oRng.Find Do While .Execute(findText:=strWORDTOFIND, MatchCase:=False) With oRng .Text = "It is a pleasure ...." .Bold = False .Italic = False .Underline = wdUnderlineNone .Collapse 0 .Select End With bFound = True Exit Do Loop End With If Not bFound Then Beep MsgBox strWORDTOFIND & " is not present." End If Set oRng = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Hi Charles and Gmayor,
Thanks for the code. Very kind of you to help. I will try it and get back to you. I did try userform but being a beginner I found it hard to set up. The sub above is one of 10 modules. Each sub is called by the first sub. Currently, the first sub is invoked with a quick acces bar icon. It takes a Dragon trascription text and removes things like an unformatted phrase example: plan to follow .. and replaces it with paragraph symbol, PLAN TO FOLLOW: space (underlined and bold), removes extra blank spaces, and extra paragraphs. I am sure a more elegant way to do this is with fields but as you surmised I really don't know vba coding. I was able to write some macros in WPX3 but I want to learn VBA for word as a personal learning project. I am just finding the syntax impossible, especilly error handling and debug messages and the correction. Turns out I had an easier time "coding" in WPX3. I have not figured out how to add code to a sub on the flyin Word Macros, something which is helful in WordPerfect. Dictating some phrases and having Dragon Naturally Speaking do this also difficult It is actually easier in word than in Dragon. I am adding modules to find a list of text and replace each, in turn, with another text. I don't like how Dragon does this. This is challenging and I may ask for some help. So far I just use a sub for each word pair, a laborious enterprise when many word pairs correcting. I was hoping you could suggest a macro that I could integrate into my project. One thing I am tryng to learn is to open a template or dotm and auto run all my subs consecutively. I think I must use a dotm template for that to work, but I am having no success with autonew. The last sub (preferably first sub) would ask for the client's name and rename the letter.dotm to the last name automatically. In my project, I want to open a new doc.?dotm and run a macro to reassign the enter key in word to run a macro rather than using keyboard shortcut (like shft ctl F6). The first statements would be to re-enable the proper enter key functionality. This would avoid needing a userform and the tab key which is less intuitive for my secretary. Can I do this in word vba? I realize this is a lengthy project but I already have most of the simple formatting tasks woriking. I appreciate the help in this forum. Thanks to everyone. savvy (..evidently not so much). |
#8
|
||||
|
||||
Quote:
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
Hello Graham,
the link you gave me VBA Code Examples (4) results in malwarabyte detecting security threats. Not sure it's safe to open link and or download examples |
#10
|
||||
|
||||
Check the link at Virus Total - there is nothing wrong with the page.
There are no examples to download. The page contains code listings.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#11
|
|||
|
|||
Hi Graham,
I am following instructions in Create a Simple User Form. When I click Initialize I end up with Option Explicit Private Sub UserForm_Click() End Sub Private Sub UserForm_Initialize() End Sub rather than what the instructions say Option Explict Private oVars as Variables I'm not sure what mistake I made. Your help is appreciated. |
#12
|
|||
|
|||
Hi. Thanks so much for your "Replace a list of words from a table". Is it a big deal to make the replacement include the font of the replace list ex bold underline on several replacements?
|
#13
|
||||
|
||||
The macro will replace the word of phrase in column 1 with the word or phrase in column 2, formatted as it is formatted in column 2.
With regard to the userform, you didn't make a mistake. Add the missing line to the code module. See https://www.gmayor.com/Images_2011/Userfo3.jpg
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#14
|
|||
|
|||
i did run the macro with the 3 columns of which col 1 is the search and 2 is replace with fonts/formatting. Everthing works great exept for the fonts, they are transferred stripped of fonts. Perhaps it is a word setting then.
Yes I really appreciate you userformfeedback anjd got it to work. As you mentioned the bookmark issue is really a pain. I wish I had gone the control box route. Please check your support@gmayor.com. My gratitude and Regards. |
#15
|
|||
|
|||
Use styles in the location you are pasting for formatting.
To check, go to the location and press Ctrl+Spacebar and type. What you see is the formatting you'll get when you insert text there. Content Controls are much less fragile than bookmarks. See Working with Bookmarks in VBA |
Tags |
nubie, search and replace |
Thread Tools | |
Display Modes | |
|