Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2023, 01:21 AM
RobiNew RobiNew is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Competent Performer
Macro to return a specific character in a specific paragraph.
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default Macro to return a specific character in a specific paragraph.


I'm trying to devise a code to return a specific character in a specific paragraph. Can someone help? Thanks!
Code:
Sub ChrSix()
'The sixth character in the first word of paragraph 5 is "." (Title.)
  With ActiveDocument
 If .Paragraphs(5).Range.Words(1).Characters(6) = "." Then
MsgBox "Found"
 Else
MsgBox "Not Found"
End If
  End With
End Sub
Reply With Quote
  #2  
Old 11-29-2023, 02:56 AM
gmaxey gmaxey is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,602
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

The "." after Title is not the sixth character for the first word. It is the first characters of the second Word ". "


Code:
Sub ChrSix()
'The sixth character in the first word of paragraph 5 is "." (Title.)
  With ActiveDocument
 If .Paragraphs(5).Range.Words(2).Characters(1) = "." Then
MsgBox "Found"
 Else
MsgBox "Not Found"
End If
  End With
End Sub

See for yourself:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oPar As Paragraph, oWord As Range
  Set oPar = ActiveDocument.Range.Paragraphs(5)
    For Each oWord In oPar.Range.Words
      oWord.Select
    Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-29-2023, 08:37 AM
RobiNew RobiNew is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Competent Performer
Macro to return a specific character in a specific paragraph.
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Thank you, Gmaxey! That was a typo, I meant Words(1). That code was part of the larger set here below. It works, but perhaps it can be made more efficient? Thanks a lot!
Code:
Sub ChrSixBis()
  With ActiveDocument
If .Paragraphs(5).Range.Words(1) = "title" And .Paragraphs(5).Range.Characters(6) = "." _
Or .Paragraphs(5).Range.Words(1) = "Title" And .Paragraphs(5).Range.Characters(6) = "." _
Or .Paragraphs(5).Range.Words(1) = "TITLE" And .Paragraphs(5).Range.Characters(6) = "." Then
    .Paragraphs(5).Range.Style = "myStyle"
    .Paragraphs(5).Range.Words(1).Case = wdLowerCase
    .Paragraphs(5).Range.Words(1).Font.SmallCaps = True
Else
MsgBox "Not Found"
End If
End With
End Sub
Reply With Quote
  #4  
Old 11-29-2023, 03:37 PM
Guessed's Avatar
Guessed Guessed is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
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 might be easier
Code:
Sub ChrSixBis()
  With ActiveDocument.Paragraphs(5).Range
    If LCase(.Text) Like "title.*" Then
      .Style = "Heading 1"
      .Words(1).Case = wdLowerCase
      .Words(1).Font.SmallCaps = True
    Else
      MsgBox "Not Found"
    End If
  End With
End Sub
I would recommend you add an error checker in case the document doesn't have 5 paragraphs. Also, if you are trying to assign a custom style, you might also need an error checker to ensure that style exists in the document.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 11-30-2023, 12:57 AM
RobiNew RobiNew is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Competent Performer
Macro to return a specific character in a specific paragraph.
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

Thank you very much indeed, Guessed! Could you please explain the magics of '*' ?
Reply With Quote
  #6  
Old 11-30-2023, 01:48 AM
Guessed's Avatar
Guessed Guessed is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
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

The * is 'something else'. That test is basically saying does the paragraph start with the string "title." and then followed by anything else.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 11-30-2023, 01:58 AM
RobiNew RobiNew is offline Macro to return a specific character in a specific paragraph. Windows 10 Macro to return a specific character in a specific paragraph. Office 2016
Competent Performer
Macro to return a specific character in a specific paragraph.
 
Join Date: Sep 2023
Posts: 200
RobiNew is on a distinguished road
Default

I see. Thanks a lot!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing font for a specific character wagicz PowerPoint 1 03-16-2021 03:13 PM
Add paragraph return between two specific character jeffreybrown Word 2 03-30-2017 02:45 PM
Macro to return a specific character in a specific paragraph. Extract Line of Text w/ specific characters up to the paragraph character, send to Excel dmarie123 Word VBA 10 07-20-2015 12:16 AM
Macro to return a specific character in a specific paragraph. Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank caboy Word VBA 2 04-01-2015 07:00 AM
Macro to return a specific character in a specific paragraph. How can select from a specific character to another character mohsen.amiri Word 2 02-19-2015 11:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:41 PM.


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