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?

Re: Macro to detect doctype and change? by Jean-Guy

Jean-Guy
Tue Mar 13 10:00:03 CDT 2007

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Macro to detect doctype and change? by MKG

MKG
Wed Mar 14 15:16:35 CDT 2007

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....

"Jean-Guy Marcil" wrote:

> 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
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>
>
>

Re: Macro to detect doctype and change? by Jean-Guy

Jean-Guy
Wed Mar 14 21:37:30 CDT 2007

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org