Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2023, 01:17 PM
Jetheat Jetheat is offline Replacing a Sentence with New Format Mac OS X Replacing a Sentence with New Format Office 2016 for Mac
Novice
Replacing a Sentence with New Format
 
Join Date: Dec 2013
Posts: 21
Jetheat is on a distinguished road
Default Replacing a Sentence with New Format

I have a document with a table.
In one of the rows, it has a sentence like this:

door type: Flat Entry Door door material: Wood

I'd like to replace all instances of this with proper formatting like this:

Door Type: Flat-Entry Door
Door Material: Wood

I have already tried 'find and replace' to do this, but it doesn't seem to work.



Can a Macro do this across the document?

Of course, whatever variable comes after Door Type: and Door Material: should appear as it is but properly formatted. Formatted for Calibri (8).

See my attached doc.

Is there a way to do this?

I use Word on a Mac.

JH
Attached Files
File Type: docx Sub Sentence.docx (290.2 KB, 3 views)
Reply With Quote
  #2  
Old 11-21-2023, 03:11 PM
gmaxey gmaxey is offline Replacing a Sentence with New Format Windows 10 Replacing a Sentence with New Format 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

As long as all of the current format in your working files are consistent with your sample find and replace should work fine.

Find " door type"
Replace with "Door Type"


Find " door material
Replace with "^pDoor Material"
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-21-2023, 03:32 PM
Jetheat Jetheat is offline Replacing a Sentence with New Format Mac OS X Replacing a Sentence with New Format Office 2016 for Mac
Novice
Replacing a Sentence with New Format
 
Join Date: Dec 2013
Posts: 21
Jetheat is on a distinguished road
Default

I also need to place it on two lines instead of one and format it to Calibri 8.

I may have 20 of them on one doc and so I was thinking of an easy macro.

Also, the text after Door Type and Door Material could be different in different tables.

JH
Reply With Quote
  #4  
Old 11-21-2023, 03:57 PM
gmaxey gmaxey is offline Replacing a Sentence with New Format Windows 10 Replacing a Sentence with New Format 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

Macros aren't easy to write and this is not a macro writing service. This is a forum to help people who want to learn how to write their own macros.


Have you tried what I posted?


If you did, it would not matter how many "of them" you have in the document and it wouldn't matter what text follows " door type" and " door material"


Do a little research on how to write a "find and replace" macro. If you can't figure it out, then post what you tried and I or someone will be glad to help you from there.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-21-2023, 04:21 PM
Guessed's Avatar
Guessed Guessed is offline Replacing a Sentence with New Format Windows 10 Replacing a Sentence with New Format 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

For the content you provided, this macro works
Code:
Sub Macro1()
  Dim aRng As Range, aSty As Style
  Set aRng = ActiveDocument.Range
  Set aSty = ActiveDocument.Styles("Note Heading")
  aSty.Font.Name = "Calibri"
  aSty.Font.Size = 8
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Style = aSty
    .Text = "door type:"
    .Replacement.Text = "Door Type:"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    Do While .Execute(Replace:=wdReplaceOne)
      aRng.Paragraphs(1).Range.Font.Reset
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "door material:"
    .Replacement.Text = "^pDoor Material:"
    .Execute Replace:=wdReplaceAll
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 11-22-2023, 09:30 AM
Jetheat Jetheat is offline Replacing a Sentence with New Format Mac OS X Replacing a Sentence with New Format Office 2016 for Mac
Novice
Replacing a Sentence with New Format
 
Join Date: Dec 2013
Posts: 21
Jetheat is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
For the content you provided, this macro works
Code:
Sub Macro1()
  Dim aRng As Range, aSty As Style
  Set aRng = ActiveDocument.Range
  Set aSty = ActiveDocument.Styles("Note Heading")
  aSty.Font.Name = "Calibri"
  aSty.Font.Size = 8
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Style = aSty
    .Text = "door type:"
    .Replacement.Text = "Door Type:"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    Do While .Execute(Replace:=wdReplaceOne)
      aRng.Paragraphs(1).Range.Font.Reset
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "door material:"
    .Replacement.Text = "^pDoor Material:"
    .Execute Replace:=wdReplaceAll
  End With
End Sub
This is superb. Thank you Andrew.

Can a slight adjustment be made?

The original sentence either has a space before it or some text which is joined onto it (like xxxDoor type).

Is there a way for the code to remove any space before the word, or separate the previous word from 'Door', and then move the whole thing onto another line anyway?

So:
xxxdoor type: Flat Entry Door door material: Wood

would become:

xxx
Door Type: Flat Entry Door
Door Material: Wood

and
door type: Flat Entry Door door material: Wood

Becomes
Door Type: Flat Entry Door
Door Material: Wood

Can an adjustment like this be made in the code?

Really helpful.
Thanks,

JH
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sentence Template michissimo Word 1 11-19-2020 05:53 PM
Replacing a Sentence with New Format VBA: Set two styles in one sentence jeffreybrown Word VBA 6 09-07-2018 03:46 PM
Replacing a Sentence with New Format I want to know how to format a sentence after beginning it with an automatic number spirituel Word 2 09-29-2015 01:50 AM
Replacing a Sentence with New Format End of sentence puncuation. nrsmd Word 2 07-04-2015 10:33 PM
Replacing a Sentence with New Format Delete does not bring second sentence closer to first sentence Andoheb Word 29 07-03-2014 01:48 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:22 AM.


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