How to apply one format of text to some docs?

A

avkokin

Hello.
I have some documents with different formatting. I need to apply one
format to these documents, for example, the italics font for all
documents. I have a lot of documents. How it is possible to change
formatting of all documents without opening of each file?
Thanks.
 
D

DaveLett

In short, you have to open the document. To speed up the process, though, you
can open the document with the visible parameter set to false:

Dim oDoc As Document
Set oDoc = Documents.Open _
(FileName:="C:\Test.docx", _
Visible:=False)
With oDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
 
A

avkokin

Hello. Thank you very much.
OK, I use next code (+ your tips) for all docs into one folder. But I
get the cycling. How to do correct this code? Thank you.

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub
 
A

avkokin

Hello. Thank you very much.
OK, I use next code (+ your tips) for all docs into one folder. But I
get the cycling. How to do correct this code? Thank you.

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub
 
A

avkokin

Hello. Thank you very much.
OK, I use next code (+ your tips) for all docs into one folder. But I
get the cycling. How to do correct this code? Thank you.

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub
 
D

Doug Robbins - Word MVP

You have missed a

myFile = Dir$()

command inside the While>Wend loop.

Use

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
myFile = Dir$() 'This line was missing
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hello. Thank you very much.
OK, I use next code (+ your tips) for all docs into one folder. But I
get the cycling. How to do correct this code? Thank you.

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub
 
D

Doug Robbins - Word MVP

See response to your earlier message.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hello. Thank you very much.
OK, I use next code (+ your tips) for all docs into one folder. But I
get the cycling. How to do correct this code? Thank you.

Sub batchFormating()
Dim myFile As String
Dim myDoc As Document
Dim path As String
Dim fDlg As FileDialog
On Error Resume Next
Set fDlg = Application.FileDialog(msoFileDialogFolderPicker)
With fDlg
.Title = "Select folder and type OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Canceled", , "Batch formating"
Exit Sub
End If
path = fDlg.SelectedItems.Item(1)
If Right(path, 1) <> "\" Then path = path + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(path & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(path & myFile)
With myDoc
.Range.Font.Italic = True
.Close SaveChanges:=wdSaveChanges
End With
Wend
Set fDlg = Nothing
Set myDoc = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top