PDA

View Full Version : Solved: Using code from later versions of Office



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.

gmaxey
06-14-2011, 03:33 PM
Try:
Case Is = "PDF"
strDocName = strDocName & ".pdf"
#If VBA6 Then
ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=17
#End If
End Select

Talis
06-15-2011, 10:55 AM
Thank you very much for your help.
I have tried it in Word 2003 but not Word 2007 as yet.

When the code is run it has a tendency to close Word 2003 (almost every time) after it has done the job and finished running. One time it closed Word but not the VBE which is a new one to me!
I will research the use of the #.
Thanks.
Will postpone putting 'Solved' in case someone comes up with an alternative suggestion.

Frosty
06-15-2011, 05:33 PM
# is conditional compile formatting... another option is putting the version specific code in a separate module (this will require a rewrite of your code, obviously), and only calling the code in that module if it is in Application.Version.

Word compiles the entire module if any code within the module is called... so you can get away with having "uncompilable" code in an earlier version of Word... if you structure your project correctly.

This is a klugey way to get what you want, but I used it to make projects backwards compatible between Word2003 and Word2000... so it will work between Word 2007 and Word 2003, as long as you code project is in .dot format (as opposed to .dotm format).

Talis
06-16-2011, 11:29 AM
Thank you Frosty for your tips.
I have tried your suggestion and it also works.

As I reported above MS Word is closing after finishing the conversions and it is the line d.Close causing the problem. I commented out this line which resulted in Word not closing; however, it produced two test files in the 'Converted' folder which I can't delete until I restart my computer (presumably).
It looks like d.Close will have to stay.
As my query was more to do with future mini-projects I believe this problem to be solved.
Thanks.

Frosty
06-16-2011, 12:18 PM
Talis, in looking at your code... I think you've got a couple of flaws in your methodology.

1. You're trying to use Word to open the document (set d = application.documents.open) but not testing whether Word *should* open the document... instead testing what type of document it is after you've attempted to open it. Instead of using ActiveDocument.Name, try using oFile.Name instead, and moving the Set D = Application.Documents.Open (oFile.Path) below the test of whether to attempt opening the document.

2. after setting D... you then reference activedocument... you should continue to use D (since you've set it to the document you just opened).

I suspect Word might be closing on you because it is encountering files it can't open, but keeping the earlier reference to a document it *could* open as "d". Try something like the following instead (I'm not a fan of dimming variables in code, but I kept your style rather than my own):

For Each oFile In oFolder.Files
strDocName = oFile.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
Dim d As Document
Set d = Application.Documents.Open(oFile.Path)
strDocName = Left(strDocName, intPos - 1)
ChangeFileOpenDirectory tFolder
Select Case FileType
Case Is = "TXT"
strDocName = strDocName & ".txt"
d.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
Case Is = "RTF"
strDocName = strDocName & ".rtf"
d.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
Case Is = "HTML"
strDocName = strDocName & ".html"
d.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
Case Is = "PDF"
strDocName = strDocName & ".pdf"
'Remove apostrophe at start of next line if using Office 2007
'd.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF

End Select
End If
d.Close
ChangeFileOpenDirectory oFolder
Next oFile

Talis
06-16-2011, 11:59 PM
Thank you. That now works perfectly.