A
Alex Zendel
Hello all.
I’ve been stuck on this VBA/Word problem for far too long and I’
hoping somebody can come to the rescue. Here’s the situation. M
coworker entered a lot of data (hundreds of records) into a wor
template and saved them as .dot files. I need to get these data int
a spreadsheet so I can eventually import them into a database. In th
past, I’ve been able to convert a folder/directory of these .do
files to the wdFormatTextLineBreaks format but now this code isn’
working any longer. Once these documents are in the text format,
have code to open each one and copy the data into a spreadsheet.
The root of this problem is that theses records were saved as template
and not documents. So I’ve tried using the Template.OpenAsDocumen
method and when I later try to programmatically save the ne
‘document’, it throws runtime error 5154: "You cannot save
template to a non-template format". I’ve tried saving it t
wdFormatDocument in hopes that I can run the code again and to conver
them into the text file format that I need. But I’m still receivin
the same runtime error. PLEASE HELP!
ThankYou ThankYou ThankYou ThankYou ThankYou ThankYou ThankYo
ThankYou
The code is below. The first part of the code reads every file in th
specified directory into the fList array. Then the second half i
where I’m trying to open each template and save it as a text file.
See comments for location of run time error.
Sub OpenandchangeAllFiles()
Dim fList() As String
Dim strFileName() As String
Dim fName As String
Dim fPath As String
Dim I As Integer
'set a directory to be searched for files
fPath = "C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\"
'build a list of the files
fName = Dir(fPath & "*.dot")
While fName <> ""
'add fName to the list
I = I + 1
ReDim Preserve fList(1 To I)
ReDim Preserve strFileName(1 To I)
strFileName(I) = fName
fList(I) = fName
'get next filename
fName = Dir()
Wend
'see if any files were found
If I = 0 Then
MsgBox "No files found"
Exit Sub
End If
Dim strOutFile As String
Dim pDoc3 As Document, strFullName As String, strOutDir As String
'cycle through the list and open
For I = 1 To UBound(fList)
strFullName = fPath & fList(I)
Debug.Print strFullName
Set pDoc3 = Documents.Open(FileName:=strFullName
Format:=wdOpenFormatTemplate)
'Set the output directory
strOutDir = "C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\Output\"
'create the output file name
strOutFile = strOutDir & Left(fList(I), Len(fList(I)) - 4)
strOutFile = strOutFile & ".doc"
'pDoc3.SaveAs FileName:=strOutFile
'Debug.Print strOutFile
C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\Output\MID_Bay_Country_Estates.doc
Dim pDoc As Document, pDoc2 As Document, pTemp As Template
For Each pDoc In Documents
'OpenAllFiles.doc is the document in which this code exists i
being run
' If it is closed, then this code will stop
If pDoc.Name <> "OpenAllFiles.doc" Then
'transfer the active document's template to the templat
object
Set pTemp = ActiveDocument.AttachedTemplate
Set pDoc2 = pTemp.OpenAsDocument
'BELOW LINE IS WHERE I GET RUN TIME ERROR 5154
' "You cannot save a template to a non-template format"
' This is what the OpenAsDocument method is for. It eve
says so in the help files
pDoc2.SaveAs FileName:=strOutFile
fileformat:=wdFormatDocument
pDoc2.Close
End If
Next
Next 'next document
End Su
I’ve been stuck on this VBA/Word problem for far too long and I’
hoping somebody can come to the rescue. Here’s the situation. M
coworker entered a lot of data (hundreds of records) into a wor
template and saved them as .dot files. I need to get these data int
a spreadsheet so I can eventually import them into a database. In th
past, I’ve been able to convert a folder/directory of these .do
files to the wdFormatTextLineBreaks format but now this code isn’
working any longer. Once these documents are in the text format,
have code to open each one and copy the data into a spreadsheet.
The root of this problem is that theses records were saved as template
and not documents. So I’ve tried using the Template.OpenAsDocumen
method and when I later try to programmatically save the ne
‘document’, it throws runtime error 5154: "You cannot save
template to a non-template format". I’ve tried saving it t
wdFormatDocument in hopes that I can run the code again and to conver
them into the text file format that I need. But I’m still receivin
the same runtime error. PLEASE HELP!
ThankYou ThankYou ThankYou ThankYou ThankYou ThankYou ThankYo
ThankYou
The code is below. The first part of the code reads every file in th
specified directory into the fList array. Then the second half i
where I’m trying to open each template and save it as a text file.
See comments for location of run time error.
Sub OpenandchangeAllFiles()
Dim fList() As String
Dim strFileName() As String
Dim fName As String
Dim fPath As String
Dim I As Integer
'set a directory to be searched for files
fPath = "C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\"
'build a list of the files
fName = Dir(fPath & "*.dot")
While fName <> ""
'add fName to the list
I = I + 1
ReDim Preserve fList(1 To I)
ReDim Preserve strFileName(1 To I)
strFileName(I) = fName
fList(I) = fName
'get next filename
fName = Dir()
Wend
'see if any files were found
If I = 0 Then
MsgBox "No files found"
Exit Sub
End If
Dim strOutFile As String
Dim pDoc3 As Document, strFullName As String, strOutDir As String
'cycle through the list and open
For I = 1 To UBound(fList)
strFullName = fPath & fList(I)
Debug.Print strFullName
Set pDoc3 = Documents.Open(FileName:=strFullName
Format:=wdOpenFormatTemplate)
'Set the output directory
strOutDir = "C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\Output\"
'create the output file name
strOutFile = strOutDir & Left(fList(I), Len(fList(I)) - 4)
strOutFile = strOutFile & ".doc"
'pDoc3.SaveAs FileName:=strOutFile
'Debug.Print strOutFile
C:\DOF\WHC\FROM_field_crew\Hugh\Form140s\MID\Output\MID_Bay_Country_Estates.doc
Dim pDoc As Document, pDoc2 As Document, pTemp As Template
For Each pDoc In Documents
'OpenAllFiles.doc is the document in which this code exists i
being run
' If it is closed, then this code will stop
If pDoc.Name <> "OpenAllFiles.doc" Then
'transfer the active document's template to the templat
object
Set pTemp = ActiveDocument.AttachedTemplate
Set pDoc2 = pTemp.OpenAsDocument
'BELOW LINE IS WHERE I GET RUN TIME ERROR 5154
' "You cannot save a template to a non-template format"
' This is what the OpenAsDocument method is for. It eve
says so in the help files
pDoc2.SaveAs FileName:=strOutFile
fileformat:=wdFormatDocument
pDoc2.Close
End If
Next
Next 'next document
End Su