Re: Newbie: Saving as Word doc

From: MikeD (nobody_at_nowhere.edu)
Date: 03/27/04


Date: Fri, 26 Mar 2004 20:28:26 -0500


"Ed" <ed_millis@removethis.hotmail.com> wrote in message
news:%23Z9OWv0EEHA.3784@TK2MSFTNGP10.phx.gbl...
> Well, my first attempt at fixing this thing didn't work, so I'm trying
> again. I've inherited a VB app that experts here say is poorly written.
I
> can believe it! Plus, I'm not real hot at programming, so I don't always
> understand exactly what's going on. This VB app opens up text files in
> Word, splits them into separate documents, and saves each new document
into
> a folder with a " .doc" extension.
>
> I have discovered that just because the new docs are saved with " .doc",
and
> Word sees and opens them just fine, they are *not* necessarily Word docs,
> with real Word formatting. This means some of my Word macros (like search
> tools, etc.) don't work because they can't "see" what they need to key off
> of.

This is true. Merely changing a file's extension does NOT "convert" that
file to a different format. In fact, it doesn't change the file at all. It
just means the file may be opened with a different application (and the
application has to support the file's format). What you're ending up with
is just a regular old text file that just *happens* to have a .doc
extension. Just reminiscing....in the DOS days, it was very common for text
files to have this extension (README.DOC files were *very* common).
Personally, I don't know why MS ever chose .doc as the extension for Word
document files as many other types of applications have "document" files
(fortunately, they didn't choose .dat for Access database files; that would
have REALLY fubared things).

>
> I added code to the VB app to run after the separation part is finished
that
> goes back into the folder, opens every doc, and resaves it as a Word doc.
I
> have over 12,000 docs, though, and after 30 minutes and it still wasn't
> done, I figured there had to be a better way - like get it to save as a
Word
> doc the *first* time, rather than do it again. But, like a newbie, I
don't
> know how to change the code to do that.
>
> This appears to be the current Save process:
> ' get text cut from long doc
> Set fil = fso.GetFile("c:\tempfile.txt")
> Count = Count + 1
> ' save into folder as " .doc"
> fil.Copy ("c:\TIR\" & tempfile & ".doc")
> tempfile = " "
>
> This is what I've added:
> fldr2 = "c:\TIR " & dates & "\"
> Set WordApp = CreateObject("Word.Application")
> strFName = Dir$(fldr2 & "*.*")
> Do While Len(strFName) > 0
> If StrComp(Right$(strFName, 4), ".doc", vbTextCompare) = 0 Then
> Set WordDoc = WordApp.Documents.Open(fldr2 & strFName)
> WordDoc.SaveAs FileName:=fldr2 & strFName, _
> FileFormat:=wdFormatDocument
> WordDoc.Close
> End If
> strFName = Dir$
> Loop
> Set WordDoc = Nothing
> Set WordApp = Nothing
>
> Any help merging the two processes and shortening my run time is greatly
> appreciated!

Well, you could get rid of the first part (which merely copies the file with
a different extension). Just open the .txt file directly, using your
Word.Application object. Other than that, there's probably not anything
different you can do to convert the text format file to a Word format file
(unless you want to get into the messy Word file format and write the file
"from scratch"). In all probability, that'd probably be considerably quicker
than using Automation with Word, but also a LOT more difficult.

Two other things that I see with your code:

1. NEVER hard-code paths.
2. You're not quitting Word. Setting the object variable for the
Word.Application object does not cause Word to quit. You need to call its
Quit method. You're closing the document and that's good, but you're not
quitting the Word application instance you've created.

Mike



Relevant Pages

  • Re: GIF
    ... What's the Mac equivalent of that arrangement? ... extension letters are optional, though -- they're not required for the ... Mac to recognize the intended format. ... so long as the file opens and I can insert it into ...
    (alt.usage.english)
  • Re: 2007 Compatibility Pack problem
    ... Word uses the extension of a file to decide on how to open it. ... "doc" means that the file is in the older Word file format while "docx" means it is in the new open xml format. ... If we rename the file with .docx, the same document opens OK. ...
    (microsoft.public.word.docmanagement)
  • Re: How do I change file extentions?
    ... >> not a Word Doc. ... Word will read the header data to determine which format ... To add to what Terry has said, the extension is also there for Windows. ... >> Word has to create temp files in the active folder whenever it opens or ...
    (microsoft.public.word.newusers)
  • Re: Open file from drag over application Icon
    ... files with whatever the extension you support. ... app as the default app for a extension. ... I want to fire the drag events when someone drags a file over the ... app recognized the file that was dropped on it and opens the file ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Common Dialog - Change file extension
    ... >> whose format is distinct then the extension is the first thing the ... > can make sense for the app to validate that what was typed is one of the ... > valid choices and possibly prompt the user if they said something else. ...
    (microsoft.public.vb.general.discussion)