Answered by:
script to change existing paragraph font

Question
-
Hi,
I thought this would be easy but I'm going round in circles.... I want a vbscript to change existing Word 2010 paragraphs 7 and 8 to "Courier New" 8 point. The macro works but not the script for some reason. Help please. Brian
Thursday, March 6, 2014 9:38 AM
Answers
-
A Find can't find two different font sizes at the same time and, in any event, that only adds two lines to the code. And, as I originally said, the preferred way of doing this is by modifying the applicable Styles.
Perhaps I misunderstood your requirements - I thought you were referring to paragraphs with a 7pt or 8pt font. If, however, you're wanting to change the entire 7th & 8th paragraphs, you could use:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\Brian\Documents\Test.doc")
With objDoc
With .Range(.Paragraphs(7).Range.Start, .Paragraphs(8).Range.End)
.Font.Size = 8
.Font.Name = "Courier New"
End With
.Save
.Close
End With
objWord.Quit
Set objDoc = Nothing: Set objWord = Nothing
Cheers
Paul Edstein
[MS MVP - Word]
- Proposed as answer by Fei XueMicrosoft employee Friday, March 7, 2014 1:56 AM
- Edited by macropodMVP Friday, March 7, 2014 2:42 AM Trying to fix unwanted line wrap in code
- Marked as answer by Brian658 Friday, March 7, 2014 10:49 AM
Friday, March 7, 2014 12:51 AM
All replies
-
Without seeing your script, it's difficult for anyone to advise what the issue is. Although the preferred way of doing this is by modifying the applicable Styles, that also assumes Styles are being used properly. The following code, however, should achieve what you're after:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\Brian\Documents\Test.doc")
With objDoc
With .Content.Find
.Font.Size = 7
.Wrap = 1 'wdFindContinue
With .Replacement
.Font.Size = 8
.Font.Name = "Courier New"
End With
.Execute , , , , , , , , , , 2 'Replace:=wdReplaceAll
.Font.Size = 8
.Execute , , , , , , , , , , 2 'Replace:=wdReplaceAll
End With
.Save
.Close
End With
objWord.Quit
Set objDoc = Nothing: Set objWord = NothingCheers
Paul Edstein
[MS MVP - Word]Thursday, March 6, 2014 11:54 PM -
Many thanks for the speedy reply. This works for me but is there a way of achieving this result without using the Content.Find (7 point font size), i.e. just by somehow specifying paragraphs 7 and 8?
Steep learning curve for me at the moment.
regards
Brian
Friday, March 7, 2014 12:41 AM -
A Find can't find two different font sizes at the same time and, in any event, that only adds two lines to the code. And, as I originally said, the preferred way of doing this is by modifying the applicable Styles.
Perhaps I misunderstood your requirements - I thought you were referring to paragraphs with a 7pt or 8pt font. If, however, you're wanting to change the entire 7th & 8th paragraphs, you could use:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\Brian\Documents\Test.doc")
With objDoc
With .Range(.Paragraphs(7).Range.Start, .Paragraphs(8).Range.End)
.Font.Size = 8
.Font.Name = "Courier New"
End With
.Save
.Close
End With
objWord.Quit
Set objDoc = Nothing: Set objWord = Nothing
Cheers
Paul Edstein
[MS MVP - Word]
- Proposed as answer by Fei XueMicrosoft employee Friday, March 7, 2014 1:56 AM
- Edited by macropodMVP Friday, March 7, 2014 2:42 AM Trying to fix unwanted line wrap in code
- Marked as answer by Brian658 Friday, March 7, 2014 10:49 AM
Friday, March 7, 2014 12:51 AM -
Sorry for not being too clear, I had difficulty with Range.Start and Range.End
Many thanks again...
Brian
Friday, March 7, 2014 10:49 AM