Macro to detect doctype and change?

M

MKG

I need help creating a macro to do the following:

When any file is opened (I'm thinking this will be an autoopen macro), the
macro will read the document's extension, and if it's a particular extension
will automatically save it to the C drive with the same name but in another
format.

So it's not just giving the file a different extension, it's actually
changing it from one format (such as RTF or TXT) to Word 2003 format (.doc).

Is it possible to write such a macro?
 
J

Jean-Guy Marcil

MKG was telling us:
MKG nous racontait que :
I need help creating a macro to do the following:

When any file is opened (I'm thinking this will be an autoopen
macro), the macro will read the document's extension, and if it's a
particular extension will automatically save it to the C drive with
the same name but in another format.

So it's not just giving the file a different extension, it's actually
changing it from one format (such as RTF or TXT) to Word 2003 format
(.doc).

Is it possible to write such a macro?

You would need something like this:

'_______________________________________
Dim docOpened As Document
Dim strDocPath As String
Dim strDocName As String
Dim strDocNameExt As String

Const strSavePathTxt As String = "C:\SavePath\txt\"
Const strSavePathRtf As String = "C:\SavePath\rtf\"

Set docOpened = ActiveDocument
With docOpened
strDocPath = .FullName
strDocName = Left(.Name, Len(.Name) - 3)
strDocNameExt = LCase(Right(.Name, 3))
Select Case strDocNameExt
Case "txt"
.SaveAs strSavePathTxt & strDocName & "doc"
.Close
Case "rtf"
.SaveAs strSavePathRtf & strDocName & "doc"
.Close
End Select
End With

Documents.Open strDocPath
'_______________________________________

If you certain that all the documents concerned will always be based on
Normal.dot, you could store the macro in the ThisDocument module of
Normal.dot, under the Document_Open sub.

If not, to make this work on all documents, you would need to store the
macro in a global template (one that is in the Startup folder as defined
under Tools > Options... > File Locations tab> Startup folder). Also, you
would need an event that would detect the opening of any document. This is
no beginner stuff, but you can look at:
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

MKG

Thank you very much - but unfortunately it wasn't quite what I needed. I was
able to get it working, the problem is that rather than running only on .TXT
files it ran on all files that were opened; and rather than saving it to Word
format, it merely changed the extension. And then it got stuck in a loop of
opening, saving, closing, opening, saving, closing....
 
J

Jean-Guy Marcil

MKG was telling us:
MKG nous racontait que :
Thank you very much - but unfortunately it wasn't quite what I
needed. I was able to get it working, the problem is that rather
than running only on .TXT files it ran on all files that were opened;
and rather than saving it to Word format, it merely changed the
extension. And then it got stuck in a loop of opening, saving,
closing, opening, saving, closing....

Sorry. I forgot to mention that the code I posted was untested, just
something I threw together to get you going...

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top