Attach Normal.dot using c#

P

Paramveer

I have a scenario where a template is attached with word document.
All the templates is available on other server say '\\ABC'.

As server name has been changed from '\\ABC' to 'XYZ', path of the template
gets lost and now the word document takes lot of time to open.

I am planning to replace all the document with normal.dot so that documents
opens fast as normal .dot is a global template and is available on every
system.

Could any body help me a c# code on how to attach normal.dot template to all
documents. I have to fix 100,000 documents.
 
G

Graham Mayor

I don't know about c#, but in Word vba - the subject of this forum - the
following macro should work for the documents in a given folder. As each
document must be opened to make the change it will take some time to run. It
might be simpler just to provide users with a macro that updates and saves
the current document. Each document would only have to be changed once when
it is next used.

Sub AttachNormalTemplate()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim nTemplate As String
Dim iFld As Integer
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

nTemplate = "D:\Word Templates\Normal.dot"

With fDialog
.Title = "Select Folder containing the documents to be modifed and click
OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
PathToUse = fDialog.SelectedItems.Item(1)
If Right(PathToUse, 1) <> "\" Then PathToUse = PathToUse + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
myFile = Dir$(PathToUse & "*.do?")

While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
With myDoc
.UpdateStylesOnOpen = False
.AttachedTemplate = nTemplate
.Close SaveChanges:=wdSaveChanges
End With
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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