Talis
06-14-2011, 12:38 PM
Hi,
Have tried to find an answer by looking through the archives but keep getting side-tracked by the interesting material.
On another forum which has a thriving eBook section, a poster asked for help in batch converting doc files to text or RTF because the eReader he'd purchased wouldn't accept doc files despite being advertised as having that capabilty. He didn't state what version of Word he had on his computer so I 'produced' (hacked together bits - mainly from this forum and VBA Help) a subroutine which works fine, even does a bit extra!
The problem which I'd like help with is the line below Case Is = "PDF":
'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
If this is uncommented and used in Office 2003 I get the expected error message:
"Compile Error
Variable not defined"
with "wdExportFormatPDF" highlighted in VBE.
I wondered whether there is some way to avoid doing what I've had to do - including a comment to the effect that the next line in the subroutine should be uncommented if using Office 2007.
Here's the subroutine:
Option Explicit
Sub ChangeDocsToTxtOrRTFOrHTML()
Dim fs As Object
Dim oFolder As Object
Dim tFolder As Object
Dim oFile As Object
Dim strDocName As String
Dim intPos As Integer
Dim locFolder As String
Dim fileType As String
Dim ext As String
On Error Resume Next
locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\myDocs")
Select Case Application.Version
Case Is < 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
Case Is >= 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
End Select
Application.ScreenUpdating = False
ActiveWindow.Visible = False
Set fs = CreateObject("Scripting.FileSystemObject")
Set oFolder = fs.GetFolder(locFolder)
Set tFolder = fs.CreateFolder(locFolder & "Converted")
Set tFolder = fs.GetFolder(locFolder & "Converted")
For Each oFile In oFolder.Files
Dim d As Document
Set d = Application.Documents.Open(oFile.Path)
strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")
ext = UCase(Mid(strDocName, intPos + 1))
'To convert non-DOC files comment out next line and the END IF line below
If ext = "DOC" Or ext = "DOCX" Then
strDocName = Left(strDocName, intPos - 1)
ChangeFileOpenDirectory tFolder
Select Case fileType
Case Is = "TXT"
strDocName = strDocName & ".txt"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
Case Is = "RTF"
strDocName = strDocName & ".rtf"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
Case Is = "HTML"
strDocName = strDocName & ".html"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
Case Is = "PDF"
strDocName = strDocName & ".pdf"
'Remove apostrophe at start of next line if using Office 2007
'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
End Select
End If
d.Close
ChangeFileOpenDirectory oFolder
Next oFile
Application.ScreenUpdating = True
ActiveWindow.Visible = True
End Sub
Thanks for reading.
Have tried to find an answer by looking through the archives but keep getting side-tracked by the interesting material.
On another forum which has a thriving eBook section, a poster asked for help in batch converting doc files to text or RTF because the eReader he'd purchased wouldn't accept doc files despite being advertised as having that capabilty. He didn't state what version of Word he had on his computer so I 'produced' (hacked together bits - mainly from this forum and VBA Help) a subroutine which works fine, even does a bit extra!
The problem which I'd like help with is the line below Case Is = "PDF":
'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
If this is uncommented and used in Office 2003 I get the expected error message:
"Compile Error
Variable not defined"
with "wdExportFormatPDF" highlighted in VBE.
I wondered whether there is some way to avoid doing what I've had to do - including a comment to the effect that the next line in the subroutine should be uncommented if using Office 2007.
Here's the subroutine:
Option Explicit
Sub ChangeDocsToTxtOrRTFOrHTML()
Dim fs As Object
Dim oFolder As Object
Dim tFolder As Object
Dim oFile As Object
Dim strDocName As String
Dim intPos As Integer
Dim locFolder As String
Dim fileType As String
Dim ext As String
On Error Resume Next
locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\myDocs")
Select Case Application.Version
Case Is < 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
Case Is >= 12
Do
fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
End Select
Application.ScreenUpdating = False
ActiveWindow.Visible = False
Set fs = CreateObject("Scripting.FileSystemObject")
Set oFolder = fs.GetFolder(locFolder)
Set tFolder = fs.CreateFolder(locFolder & "Converted")
Set tFolder = fs.GetFolder(locFolder & "Converted")
For Each oFile In oFolder.Files
Dim d As Document
Set d = Application.Documents.Open(oFile.Path)
strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")
ext = UCase(Mid(strDocName, intPos + 1))
'To convert non-DOC files comment out next line and the END IF line below
If ext = "DOC" Or ext = "DOCX" Then
strDocName = Left(strDocName, intPos - 1)
ChangeFileOpenDirectory tFolder
Select Case fileType
Case Is = "TXT"
strDocName = strDocName & ".txt"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
Case Is = "RTF"
strDocName = strDocName & ".rtf"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
Case Is = "HTML"
strDocName = strDocName & ".html"
ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
Case Is = "PDF"
strDocName = strDocName & ".pdf"
'Remove apostrophe at start of next line if using Office 2007
'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
End Select
End If
d.Close
ChangeFileOpenDirectory oFolder
Next oFile
Application.ScreenUpdating = True
ActiveWindow.Visible = True
End Sub
Thanks for reading.