Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Word docs, but skip those with macros or locked.

Status
Not open for further replies.

Iamthestig

Programmer
Apr 30, 2008
38
GB
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top