PDA

View Full Version : Changing font automatically via vba



jayjay
04-29-2009, 11:26 PM
Good day all experts,


I have a folder with approx 400 word documents. This documents have a series of style fonts, but I only need to change the 1 ex. Times new Roman size 14 to Arial size 12. What I want to achieve is to change the font via VBA instead of going into each document manually.

Word Platform = 2003


Is there a way I can do this?


All help will be appreciated


TIA


Regards
Jay-Jay

lucas
04-30-2009, 09:13 AM
See if this helps get you started. I don't necessarily wish to create docs and a folder to develop this for you but these two routines should get you where you want to be. The first one shows how to open all the files in a folder and run your code on them and how to close and save them....read the comments.

The second shows you how to change the styles from one to the other. Note that the two styles must exist...
Sub ProcessAll() '(sPath As String)
Dim sPath As String
Dim WdDoc As Document, sFile As String
'Change the path below to suit your need
sPath = "F:\Temp\test\New Folder\"
sFile = Dir(sPath & "*.doc")
'Loop through all .doc files in that path
Do While sFile <> ""
Set WdDoc = Application.Documents.Open(sPath & sFile)

'Do something with that Document, insert whatever you want to do here
' MsgBox used here to show the loop at work
' Comment out the msgbox and replace with your code.
MsgBox WdDoc.Name
'You can save it, if you like, here it's not saved
' WdDoc.Close wdSaveChanges
WdDoc.Close wdDoNotSaveChanges
sFile = Dir
Loop
End Sub



Sub FlipStyles()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
Select Case oPara.Style
Case "MyNew"
oPara.Style = "MyNew2"
Case "Mynew2"
oPara.Style = "MyNew"
End Select
Next
End Sub

Thanks to Gerry and Steiner for these contributions.

Paul_Hossler
05-01-2009, 05:00 AM
Maybe this would be faster. I'm still learning the best way to use .Find, but this seems to work


Sub FlipStyles()
With ActiveDocument.Content.Find
.ClearFormatting
.Style = "Heading 2"
.Replacement.ClearFormatting
.Replacement.Style = "Heading 1"
.Execute Replace:=wdReplaceAll
End With
End Sub


Paul

macropod
05-01-2009, 05:43 AM
Even faster and, IMHO preferred to changing to another Style that might not have the desired attributes, is to change the underlying Style for the text concerned. For example:

Sub UpdateStyle()
With ActiveDocument.Styles("Main").Font
.Name = "Arial"
.Size = 12
End With
End Sub
The above code changes the definition of the 'Main' Style, assuming that's what the font is formatted with. Change the Style name to suit.

If the Style name can't be relied upon (eg the user has hard-formatted the text), Find/Replace may be needed but, again, Style name replacement isn't the way to go. Instead:

Sub UpdateFont()
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Size = 14
.Font.Name = "Times New Roman"
.Replacement.ClearFormatting
.Replacement.Font.Size = 12
.Replacement.Font.Name = "Arial"
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

jayjay
05-03-2009, 11:21 PM
Brilliant.

Thank you all for your help. All worked.. You guys are the best

Have a great day ahead

:clap: