Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2020, 05:52 AM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 08-18-2020, 12:27 PM
Charles Kenyon Charles Kenyon is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #3  
Old 08-18-2020, 01:39 PM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 08-18-2020, 08:52 PM
Charles Kenyon Charles Kenyon is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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?
Reply With Quote
  #5  
Old 08-18-2020, 09:09 PM
Charles Kenyon Charles Kenyon is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
  #6  
Old 08-18-2020, 09:53 PM
gmayor's Avatar
gmayor gmayor is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #7  
Old 08-19-2020, 08:22 PM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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).
Reply With Quote
  #8  
Old 08-19-2020, 11:16 PM
gmayor's Avatar
gmayor gmayor is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by savvy View Post
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.
There are code examples for this at VBA Code Examples (4)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 08-20-2020, 02:24 AM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 08-20-2020, 04:34 AM
gmayor's Avatar
gmayor gmayor is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #11  
Old 08-20-2020, 10:17 AM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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.
Reply With Quote
  #12  
Old 08-30-2020, 09:05 AM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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?
Reply With Quote
  #13  
Old 08-31-2020, 12:30 AM
gmayor's Avatar
gmayor gmayor is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #14  
Old 08-31-2020, 06:48 AM
savvy savvy is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2016
Novice
new to vba - go to end of sub
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default

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.
Reply With Quote
  #15  
Old 08-31-2020, 02:30 PM
Charles Kenyon Charles Kenyon is offline new to vba - go to end of sub Windows 10 new to vba - go to end of sub Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,083
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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
Reply With Quote
Reply

Tags
nubie, search and replace

Thread Tools
Display Modes


Other Forums: Access Forums

All times are GMT -7. The time now is 04:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft