Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Getting MSW to convert txt to doc in vba

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edward - 14 Jan 2006 02:18 GMT
I have a subdirectory of text files that I need to convert to MSW.  During
the conversion, I want to run a macro that adds headers and footers to each
file.

My approach is to write in VBA for Word.  I don't have a problem locating
the files in the subdirectory, and have a count algorithm that works.  

My problem is that I can't seem to open an existing text file as a word
document so that I can run my header/footer macro, then close the file and
save it in MSWord format.  

Am I supposed to be opening a text file , then running the macro and saving
it as a Word file, or am I supposed to be opening a Word file and reading the
text file into it, then running the macro, then saving as a Word file?

There's lots of stuff in VB that allows developers to build text files, but
nothing I see that talks about this kind of conversion.  

Thanks.

Phantom Researcher
Jay Freedman - 14 Jan 2006 03:50 GMT
>I have a subdirectory of text files that I need to convert to MSW.  During
>the conversion, I want to run a macro that adds headers and footers to each
[quoted text clipped - 17 lines]
>
>Phantom Researcher

Hi Edward,

Really, you can do the job either way. In the end it makes no
difference in the finished document.

If your macro instructs Word to open a .txt file, Word creates a
document in memory and places the text in the body of the document. At
this point it's like any other document in memory. You can add
information to the header and footer. Then do a SaveAs, giving a
different filename (if only to change the extension to .doc) and
including the parameter FileFormat:=wdFormatDocument. (The latter is
necessary because if you omit it, Word defaults to saving the document
in its original .txt format, and that would cause it to display a
warning that the formatting -- the header and footer -- would be
lost.) Read the VBA help topic on the SaveAs method to see the
possible parameter values.

Alternatively, you can instruct Word to make a new blank document, and
use the InsertFile command to bring in the text file. After adding the
header and footer, you do a SaveAs; in this case it will assume a
default file format of wdFormatDocument, although it wouldn't hurt to
include that parameter anyway.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Edward - 15 Jan 2006 03:29 GMT
that worked great.  One other problem encountered:

I tried to give the msw file the same name as the text file by this:
For i = 1 To fs.FoundFiles.Count
       textfilename = fs.FoundFiles(i)
then I assign textfilename as the filename.  Problem is, textfilename
includes the .txt extension, so even though the file is a word file, it winds
up having a .txt extension.  It retains its header and footer info implanted
by the macro.
I suppose I could open a dos window and change all the .txt to .doc, but
this shouldn't be necessary.  Is there a way to get the filename without the
.txt extension relic from the text file?

I would try to truncate the characters, but can't find the corresponding
search phrase in the help file
Signature

Phantom Researcher

> >I have a subdirectory of text files that I need to convert to MSW.  During
> >the conversion, I want to run a macro that adds headers and footers to each
[quoted text clipped - 47 lines]
> Email cannot be acknowledged; please post all follow-ups to the
> newsgroup so all may benefit.
Jay Freedman - 15 Jan 2006 04:13 GMT
>that worked great.  One other problem encountered:
>
[quoted text clipped - 11 lines]
>I would try to truncate the characters, but can't find the corresponding
>search phrase in the help file

If you have at least Word 2000, you can use the Replace function like
this:

   textfilename = Replace(LCase(textfilename), _
                   ".txt", ".doc")

The LCase function converts the filename to all lowercase characters,
and the Replace function then inserts ".doc" in place of the ".txt".

If your macro has to run on Word 97 (which doesn't have the Replace
function) or you just don't like Replace (which will replace the four
characters ".txt" wherever they appear in the string, even if they
aren't at the end), this is the longer equivalent:

   If LCase(Right$(textfilename, 4)) = ".txt" Then
       textfilename = _
         Left$(textfilename, Len(textfilename) - 4) _
         & ".doc"
   End If

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.