Creating/Using VBA Script to Convert Work 2000 wps to Word 2003 do

D

Dave DiFilippo

Hi,

I'm a .NET software developer upgrading my mother's PC from Works 2000
to Word 2003. She has several hundred files in Word that I would like to
convert in an automated script. Right now, I only have her PC which has
Works 2000 and Word 2003. I was thinking/hoping that I could code a VBA
script that would traverse through a directory of .wps, convert each file to
a .doc file, and output the file to a separate directory. I'm not that
familiar with VBA, but I thought I would start by recording a macro in Word
2003 that opens a .wps file and saves it as a Word .doc. Any help on some
example VBA scripts, or maybe a script that has already been created would be
most appreciated. I've seen converters out in the newgroups, but they all
appear to be for single file conversion. If anyone has some tips on
performing a batch conversion, that would be great. If someone feels this
would be rather difficult in VBA, and much easier in .NET, I could always
wait till I get access to my own PC.

Thanks,

Dave
 
D

Dave DiFilippo

Sorry - The files are in Works 2000 5.0 .wps format, and I need to convert
them to Word 2003 .doc format.
 
T

TC

It should be easy in VBA, as long as Word will open the files in their
current format & save them to a new name in Word format.

Try something like this:

(UNTESTED)

dim oWord as object, sFile as string

' start an invisible copy of Word.
set oword = createobject ("word.application")

' loop through all wps files in the folder.
sfile = dir$("C:\MyDoccies\*.wps")
while sfile <> ""
msgbox "Processing " & sfile
' open wps file.
oword.open ... sfile ... <<<<<
' save as Word file.
oword.saveas ... <<<<<
sfile = dir$()
wend
set oword = nothing

To get the .open and .saveas commands right, use you macro idea. That's
often the quickest way to see how to do something in VBA. But it does
commonly produce some unnecessary extra guff.

If you're an experienced coder, that should be enough to get started.

HTH,
TC
 
D

Dave DiFilippo

Thanks for the reply. I've spent quite a bit of time and research, and came
up with a pretty good VBA solution, thanks to my days of VB experience. I
though I would share it by pasting it below. Let me know if there is a
better place/way to share something like this. It worked out rather well,
and I've given it some good testing. If I had more time, I might have
created a small form to assign the string variable that are hardedcoded in
the script, to make this function more dynamic and usable for other batch
conversion. The following is a copy paste of the code:

Sub ConvertWorksToWord()
'
' Title: ConvertWorksToWord Macro
' Date: 12/20/2005
' Author: Dave DiFilippo
' St. Louis MO
' [email protected]
'
' Description: This macro uses the Microsoft Scripting Libraries
FileSystemObject
' to iterate through a directory of Works 2000 documents, and save them
' as Word documents.
'
' Notes:
'
' There are several dependancies for this macro to work
'
' - It is assumed that all files within the MyWorksDirectoryPath contain
only the
' Works .wps files to be converted. There are no tests to determine if the
' file is a valid Works file, but the Documents.Open method will fail if
it's not.
'
' - The MyFileConverterName contains the FormatName of the Works 2000 file
' converter. It is assumed this is install on the machine running the
macro.
' To determine what file converters are installed, uncomment the following
' code:
'
' For Each conv In FileConverters
' MsgBox "Installed FileConverter FormatName=" & conv.FormatName
' Next conv
'
' - A reference under 'Tools' - 'References' must be checked to enable
' linkage to the Microsoft Scripting Runtime scrrun.dll to use the
' FileSystemObject
'
' - The MyWordDirectoryPath is the directory path whose
' purpose is for storing the Word equivalents of the Works files
' to be converted. If the directory has not been created, the script
' will create the folder, provided the path is valid. If the directory
' exists, then it is deleted and recreated. This is to provide the ability
' to restart the macro from the begining, in case an error occurred.
'
' - If you are receiving the following message everytime a Works file
' is opened to be converted:
'
' This file needs to be opened by the <convertername> text converter,
' which may pose a security risk if the file you are opening is a
' malicious file. Choose Yes to open this file only if you are sure it
' is from a trusted source.
'
' Refer to the following knowledge base article to supress the dialog:
'
'
http://support.microsoft.com/default.aspx?scid=kb;en-us;837011&Product=wrd2003
'
'
Dim MyFileSysObject As New FileSystemObject
Dim MyWorksFolder As Folder
Dim MyWordFolder As Folder
Dim MyWorksFiles As Files
Dim MyWordFiles As Files
Dim MyWorksDirectoryPath As String
MyWorksDirectoryPath = "C:\Documents and Settings\The Family Login\My
Documents\My Recipes in Works"
Dim MyWordDirectoryPath As String
MyWordDirectoryPath = "C:\Documents and Settings\The Family Login\My
Documents\My Recipes in Word"
Dim MyFileConverter As FileConverter
Dim MyFileConverterName As String
MyFileConverterName = "Works 2000"

On Error GoTo ErrorHandler

' Iterate through list of installed FileConverters to find the
' Work 2000 converter.
For Each fc In FileConverters
If fc.CanOpen = True Then
If fc.FormatName = MyFileConverterName Then
Set MyFileConverter = fc
End If
End If
Next fc

' Set objects to location of the folder where Works files are stored
Set MyWorksFolder = MyFileSysObject.GetFolder(MyWorksDirectoryPath)
Set MyWorksFiles = MyWorksFolder.Files

' Check to see if the output Word folder exists. If it does, delete
' it. Create the ouput folder for converted Word files to be stored
If MyFileSysObject.FolderExists(MyWordDirectoryPath) Then
MyFileSysObject.DeleteFolder FolderSpec:=MyWordDirectoryPath,
Force:=True
End If
MyFileSysObject.CreateFolder (MyWordDirectoryPath)

' Main iteration loop - Loops through all files in Works folder
' and converts each file to a Word document.
For Each MyWorksFile In MyWorksFiles
'Store the file name without file extention so that we
'can name the converted Word file the same name as the original
'Works file, with the .doc Word extention
Dim MyFileName As String
MyFileName = Mid(MyWorksFile.Name, 1, Len(MyWorksFile.Name) - 4)

'Point to Works directory, and open current iteration file with
Works converter
ChangeFileOpenDirectory MyWorksDirectoryPath
Documents.Open FileName:=MyWorksFile.Name, ConfirmConversions:=False _
, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=MyFileConverter.OpenFormat, _
NoEncodingDialog:=True, XMLTransform:=""

'Point to Word directory, and save current iteration file as Word
document
ChangeFileOpenDirectory MyWordDirectoryPath
ActiveDocument.SaveAs FileName:=MyFileName & ".doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

'Close converted Word document.
ActiveDocument.Close
Next MyWorksFile

' Set objects to location of the folder where Word files are stored, to
determine
' the number of files that have been created
Set MyWordFolder = MyFileSysObject.GetFolder(MyWordDirectoryPath)
Set MyWordFiles = MyWordFolder.Files


MsgBox "ConvertWorksToWork script has completed successfully !!!" &
vbCrLf & _
"Number of original Works documents read in " &
MyWorksDirectoryPath & " is " & MyWorksFiles.Count & vbCrLf & _
"Number of original Word documents created in " &
MyWordDirectoryPath & " is " & MyWordFiles.Count

GoTo CleanUp
'Error Handler merely displays the error that occurs, and gracefully exits
the script
ErrorHandler:

MsgBox "An error has occurred in the VBA macro script module
ConvertWorksToWord:" & vbCr & vbCr & _
vbTab & "Error Source = " & Err.Source & vbCr & _
vbTab & "Error Number = " & Err.Number & vbCr & _
vbTab & "Error Description = " & Err.Description & vbCr & vbCr & _
"Script will now exit."

'Make a direct effort to clear objects from memory, and release any
resources they may
'be taking up.
CleanUp:
Set MyFileSysObject = Nothing
Set MyFileConverter = Nothing
Set MyWorksFolder = Nothing
Set MyWorksFiles = Nothing
Set MyWordFolder = Nothing
Set MyWordFiles = Nothing
End Sub
 
Top