Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-02-2023, 11:06 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Insert a condition in Find and Replace

I'm trying to insert a condition in the code here below. The style should be replaced only if the number of italicized characters is less than 30. Can someone help? Thanks!
Code:
Sub Italic()
With ActiveDocument
If Not .Styles("myItalic") Is Nothing Then
  For iType = 1 To 2
    Set aRng = ActiveDocument.StoryRanges(iType)
    With aRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Font.Italic = True
If aRng.Characters.Range.Count < 30 Then _
.Replacement.Style = ActiveDocument.Styles("myItalic")
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchCase = False
      .Execute Replace:=wdReplaceAll
    End With
  Next iType
End If
End With
End Sub

Reply With Quote
  #2  
Old 12-02-2023, 03:18 PM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! The following code finds italicized strings in each storyrange and, if the italicized string is less-than-30-chars-long, the whole storyrange gets styled as needed. I tested the code only once, so it may have some problems.
Code:
Sub Italicize_If()

Dim aRng As range
  For iType = 1 To 2
    Set aRng = ActiveDocument.StoryRanges(iType)
    With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = ""
        .Replacement.text = ""
        .Font.Italic = True
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        Do
        If .Execute And aRng.Characters.count < 30 Then
            aRng.Select
            ActiveDocument.StoryRanges(iType).Style = "myItalic"
            GoTo lbl_next
        End If
        aRng.Collapse wdCollapseEnd
        Loop
    End With
lbl_next:
  Next iType
lbl_exit:
Set aRng = Nothing
End Sub
Reply With Quote
  #3  
Old 12-03-2023, 12:27 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Hi, Vivka! Thank you for your code, but that isn't what I need. My fault: I didn't explain properly what I need. The macro should convert only the italicized strings of the doc except when the italicized string is longer than 30 characters.
Reply With Quote
  #4  
Old 12-03-2023, 03:08 AM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! Try this:
Code:
Sub Italicize_If()

Dim aRng As range
  For iType = 1 To 2
    Set aRng = ActiveDocument.StoryRanges(iType)
    With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = ""
        .Replacement.text = ""
        .Font.Italic = True
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        Do
           If .Execute And aRng.Characters.count < 30 Then
               aRng.Select
               selection.Style = "myItalic"
           End If
           aRng.Collapse wdCollapseEnd
        Loop While .found
    End With
  Next iType
lbl_exit:
Set aRng = Nothing 
Exit Sub
End Sub
Reply With Quote
  #5  
Old 12-03-2023, 07:12 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Vivka! It ins't the fasted code you've suggested but it works perfectly.
Reply With Quote
  #6  
Old 12-03-2023, 07:46 AM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, RobiNew! I've just slightly modified your code. And, really, I don't know a better code for doing this job. I think, your algorithm is quite fast and optimal. Maybe experts here, if they want, can offer a different solution. I'd like to have another good lesson!
Reply With Quote
  #7  
Old 12-03-2023, 09:48 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Well, that was just a friendly comment! Perhaps you can explain why the code here below doesn't work at all. I want to go to the first page of the document without using Selection. Thanks!
Code:
Dim myRange As Range
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseStart
Reply With Quote
  #8  
Old 12-03-2023, 02:54 PM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, as for me,
selection.HomeKey wdStory
is the best way of moving the cursor to the doc's start.
If you don't like it, you can use
ActiveDocument.range(0, 0).Select
Reply With Quote
  #9  
Old 12-03-2023, 03:28 PM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks, Vivka!
Reply With Quote
  #10  
Old 12-03-2023, 03:35 PM
Guessed's Avatar
Guessed Guessed is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This variation on Vivka's code avoids the selection object and hence it runs a lot faster.
Code:
Sub Italicize_If2()
  Dim aRng As Range, iType As Integer
  For iType = 1 To 2
    Set aRng = ActiveDocument.StoryRanges(iType)
    With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Font.Italic = True
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        Do While .Execute
          If aRng.Characters.Count < 30 Then aRng.Style = "myItalic"
          aRng.Collapse wdCollapseEnd
        Loop
    End With
  Next iType
lbl_exit:
  Set aRng = Nothing
  Exit Sub
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 12-04-2023, 01:53 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks a lot, Guessed! It is an improvement.
Reply With Quote
  #12  
Old 12-04-2023, 05:43 AM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thank you, Andrew! I added selection to make the found instance visible. Without it, the code is definitely faster. I live and learn.
Reply With Quote
  #13  
Old 12-04-2023, 06:19 AM
gmaxey gmaxey is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default It does work as it is coded.

Quote:
Originally Posted by RobiNew View Post
Well, that was just a friendly comment! Perhaps you can explain why the code here below doesn't work at all. I want to go to the first page of the document without using Selection. Thanks!
Code:
Dim myRange As Range
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseStart



RobiNew


Your code does work for what you are telling it to do. After running that code, the range is at the start of the document. Consider:
Code:
Sub ScrahtchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim myRange As Range
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseStart
myRange.InsertBefore "I'm here."
lbl_Exit:
  Exit Sub
End Sub

If you want to start typing at the start of the document then you have to move the insert point to the start of the document as others have explained.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 12-04-2023, 07:23 AM
vivka vivka is offline Insert a condition in Find and Replace Windows 7 64bit Insert a condition in Find and Replace Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Yes, Greg, you are absolutely right! I was about to write this to RobiNew with the demonstration you used.
Reply With Quote
  #15  
Old 12-04-2023, 07:48 AM
RobiNew RobiNew is offline Insert a condition in Find and Replace Windows 10 Insert a condition in Find and Replace Office 2016
Competent Performer
Insert a condition in Find and Replace
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks to you both! I'm trying to get rid of Selection, but it isn't always easy.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert a condition in Find and Replace Find & Replace to insert thousands separators jeffreybrown Word 3 10-19-2019 04:37 PM
Insert a condition in Find and Replace Find and replace condition norgro Word VBA 2 07-31-2015 06:38 AM
Insert a condition in Find and Replace Find and Replace - How to insert brackets around all numbers in a doc Natedogg Word 2 05-21-2015 07:16 AM
Insert a condition in Find and Replace Insert text at the end of a sentence Find/Replace AlmostFriday Word 6 06-17-2012 05:21 AM
Find & Replace Insert Issue mipacker Word 0 02-18-2009 08:59 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:26 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