Iamthestig
Programmer
Hi all,
I have a simple programs that loops through a directory of word documents, opens the document and changes the template pathname. This works fine except when the document is locked by another user or contains macros that launches a form. My program will wait for a response from these docs. Is there a way I can skip these documents or put a timeout, so the progam continues after say 30 seconds?
My code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFilePath As String
Dim strPath As String
Dim strFileName As String
Dim OldServer As String
Dim NewServer As String
Dim objApp As New Application
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog
OldServer = "\\xxx01\"
NewServer = "\\yyy01\"
strFilePath = InputBox("What is the folder location that you want to use?", "Folder Location", , 200, 150)
If strFilePath.Length < 1 Then Exit Sub
If Microsoft.VisualBasic.Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
'strFileName = Dir(strFilePath & "*.doc")
For Each strFileName In My.Computer.FileSystem.GetFiles(strFilePath, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")
TextBox1.Text = strFileName.ToString
objDoc = objApp.Documents.Open(strFileName)
objTemplate = objDoc.AttachedTemplate
dlgTemplate = objApp.Dialogs(WdWordDialog.wdDialogToolsTemplates)
strPath = dlgTemplate.Template
If LCase(Microsoft.VisualBasic.Left(strPath, 12)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 12)
objDoc.Save()
End If
'strFileName = Dir()
Next
objDoc.Close()
objDoc = Nothing
objTemplate = Nothing
dlgTemplate = Nothing
MessageBox.Show("Finished")
End Sub
End Class
Thanks.
I have a simple programs that loops through a directory of word documents, opens the document and changes the template pathname. This works fine except when the document is locked by another user or contains macros that launches a form. My program will wait for a response from these docs. Is there a way I can skip these documents or put a timeout, so the progam continues after say 30 seconds?
My code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFilePath As String
Dim strPath As String
Dim strFileName As String
Dim OldServer As String
Dim NewServer As String
Dim objApp As New Application
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog
OldServer = "\\xxx01\"
NewServer = "\\yyy01\"
strFilePath = InputBox("What is the folder location that you want to use?", "Folder Location", , 200, 150)
If strFilePath.Length < 1 Then Exit Sub
If Microsoft.VisualBasic.Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
'strFileName = Dir(strFilePath & "*.doc")
For Each strFileName In My.Computer.FileSystem.GetFiles(strFilePath, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")
TextBox1.Text = strFileName.ToString
objDoc = objApp.Documents.Open(strFileName)
objTemplate = objDoc.AttachedTemplate
dlgTemplate = objApp.Dialogs(WdWordDialog.wdDialogToolsTemplates)
strPath = dlgTemplate.Template
If LCase(Microsoft.VisualBasic.Left(strPath, 12)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 12)
objDoc.Save()
End If
'strFileName = Dir()
Next
objDoc.Close()
objDoc = Nothing
objTemplate = Nothing
dlgTemplate = Nothing
MessageBox.Show("Finished")
End Sub
End Class
Thanks.