Aqua
11-14-2005, 02:46 PM
I am creating an excel spreadsheet that will list form field values from word documents. I am to the point that the basic flow is complete and am working on error handling.
The Error that I would like to correct (not handle) is the case where the document that I am trying to access is already opened. The way the code is written, the infamous " document is in use, open as read only, notify or cancel" message box comes up. I want to automatically select read only. Is this possible? How would I do this?
Thanks in advance, and if this is not clear, let me know and I will try to clarify.
-Dan aka "Aqua"
Private Sub CommandButton1_Click()
Dim fs, d, f, s
Dim strDir As String
' gets filepath
strDir = Application.ActiveWorkbook.Path
x = 2
' creates file system object
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(strDir)
Set fc = f.Files
' opens word application
Set Wdapp = CreateObject("Word.Application")
' opens each word document in filepath
For Each f1 In fc
If Right(f1.Name, 3) = "doc" Then
If Left(f1.Name, 2) <> "~$" Then
Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName)
' gets data from word document and places in excel
With Wdapp.ActiveDocument
Me.Cells(x, 1).Value = .FormFields("Text1").Result
Me.Cells(x, 2).Value = .FormFields("Text2").Result
Me.Cells(x, 3).Value = .FormFields("Text3").Result
Me.Cells(x, 4).Value = .FormFields("Text4").Result
x = x + 1
End With
Set wddoc = Nothing
End If
End If
Next
Set fs = Nothing
Set f = Nothing
Set fc = Nothing
Wdapp.Quit
End Sub
The Error that I would like to correct (not handle) is the case where the document that I am trying to access is already opened. The way the code is written, the infamous " document is in use, open as read only, notify or cancel" message box comes up. I want to automatically select read only. Is this possible? How would I do this?
Thanks in advance, and if this is not clear, let me know and I will try to clarify.
-Dan aka "Aqua"
Private Sub CommandButton1_Click()
Dim fs, d, f, s
Dim strDir As String
' gets filepath
strDir = Application.ActiveWorkbook.Path
x = 2
' creates file system object
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(strDir)
Set fc = f.Files
' opens word application
Set Wdapp = CreateObject("Word.Application")
' opens each word document in filepath
For Each f1 In fc
If Right(f1.Name, 3) = "doc" Then
If Left(f1.Name, 2) <> "~$" Then
Set wddoc = Wdapp.documents.Open(strDir & "\" & f1.shortName)
' gets data from word document and places in excel
With Wdapp.ActiveDocument
Me.Cells(x, 1).Value = .FormFields("Text1").Result
Me.Cells(x, 2).Value = .FormFields("Text2").Result
Me.Cells(x, 3).Value = .FormFields("Text3").Result
Me.Cells(x, 4).Value = .FormFields("Text4").Result
x = x + 1
End With
Set wddoc = Nothing
End If
End If
Next
Set fs = Nothing
Set f = Nothing
Set fc = Nothing
Wdapp.Quit
End Sub