Author | Message |
---|
Nina Ametuar Posts: 134
|
Code: | Sub Main() Const path = "C:\temp\" Dim file Dim document Dim counter Dim object Dim WordApp Set WordApp = CreateObject("Word.Application") WordApp.Application.ScreenUpdating = False iCounter = 0 Set object = CreateObject("Scripting.FileSystemObject") file = object.GetAbsolutepathName(path & "*.doc") Do While file > "" Set document = WordApp.Application.Documents.Open(file) If UCase(document.AttachedTemplate) <> "Normal.dot" Then document.AttachedTemplate = "Normal.dot" counter = counter + 1 document.Save End If document.Close file = path Loop WordApp.Application.ScreenUpdating = True MsgBox "Procedure beëindigt," & vbNewLine & CStr(counter) & " document(en) behandelt.", vbInformation End Sub Main() | This script does some functions on a Word document. I need to loop through a certain amount of files (Word documents) with different filenames so no 1.doc-2.doc-3.doc-... Can somebody help me go through this loop ? The fault should be in Line 15 , 23 in this code above. Thanks in advance! nina
|
dzadzuks Ametuar Posts: 135
|
Quoted from Nina | Code: | Sub Main() Const path = "C:\temp\" Dim file Dim document Dim counter Dim object Dim WordApp Set WordApp = CreateObject("Word.Application") WordApp.Application.ScreenUpdating = False iCounter = 0 Set object = CreateObject("Scripting.FileSystemObject") file = object.GetAbsolutepathName(path & "*.doc") Do While file > "" Set document = WordApp.Application.Documents.Open(file) If UCase(document.AttachedTemplate) <> "Normal.dot" Then document.AttachedTemplate = "Normal.dot" counter = counter + 1 document.Save End If document.Close file = path Loop WordApp.Application.ScreenUpdating = True MsgBox "Procedure beëindigt," & vbNewLine & CStr(counter) & " document(en) behandelt.", vbInformation End Sub Main() | This script does some functions on a Word document. I need to loop through a certain amount of files (Word documents) with different filenames so no 1.doc-2.doc-3.doc-... Can somebody help me go through this loop ? The fault should be in Line 15 , 23 in this code above. Thanks in advance! nina |
Code: | If UCase(document.AttachedTemplate) <> "Normal.dot" Then | If you use UCase() then the text you compare it to needs to be upper case too, to make any sense of that function.. ("NORMAL.DOT"). P.S i dont know vbscript objects only the VB6 things.. so cant tell if anything is wrong with the usage of those objects..
|
Nina Ametuar Posts: 134
| thanks for your replay dzadzuks This is what I ended up with (working version!)
Code: | Call Start() Sub Start() Dim path path = InputBox("Specify the directory containing your Word documents:","Reset Templates") If path = "" Then MsgBox "Wrong input," & vbNewLine & "please try again.", vbCritical, "Reset Templates" Else Call Main(path) End If End Sub Sub Main(path) Dim word Dim object Dim folder Dim document Dim counter Set word = CreateObject("Word.Application") Set object = CreateObject("Scripting.FileSystemObject") Set folder = object.GetFolder(path) For Each file In folder.Files If (Right(file.path, 4) = ".doc") Or (Right(file.path, 4) = ".rtf") Then Set document = word.Application.Documents.Open(file.Path) If document.AttachedTemplate <> "normal.dot" Then document.AttachedTemplate = "normal.dot" counter = counter + 1 document.Save document.Close word.Quit End If End If Next MsgBox "Procedure ended," & vbNewLine & CStr(counter) & " document(s) fixed.", vbInformation, "Reset Templates" End Sub |
|