|
|
|
date: Mon, 25 Aug 2008 06:33:55 +0300,
group: microsoft.public.word.vba.beginners
back
Re: DOC to TXT
It would of course have helped had I included the macro :o(
Sub SaveAllAsTXT()
Dim strFileName As String
Dim strDocName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
strDocName = ActiveDocument.FullName
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".txt"
oDoc.SaveAs FileName:=strDocName, _
FileFormat:=wdFormatText
oDoc.Close SaveChanges:=wdDoNotSaveChanges
strFileName = Dir$()
Wend
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor wrote:
> The following macro will convert all the doc files in a given folder
> to TXT
>
> Mike Berger wrote:
>> Hello Newsgroup!
>>
>> I have a foldertree with 3 underfolders, that include about 500 .doc
>> documents.
>>
>> I will open the .doc automatily and then saving as .txt and if
>> possible delete the .doc file from the disc.
>>
>> I can open one file with VBA, when I have the filename in the code.
>>
>> It's possible to make that with VBA?
>>
>> Thank you.
>> Mike
date: Mon, 25 Aug 2008 12:12:59 +0300
author: Graham Mayor
RE: DOC to TXT
Hi Mike,
i'm assuming my answer in the german groups was a bit too complicated.
The easiest way do get all *.doc documents' folders and names is that, I
think:
In the cmd-shell, navigate to your start folder,
lets say c:\test.
There you enter:
dir *.doc /b/s > c:\testdir.txt
This will create a text-file,
containing all fullnames of all docs in the actual folder and all subfolders.
You may check how german characters like Ã,ü,ä,ö are displayed,
using word or any editor.
Then it goes like this:
Dim MyDoc as document
Dim stmp as string
Dim sDoc as string
Open "c:\Testdir.txt" For Input As #1
While Not EOF(1)
Line Input #1, stmp ' Read line of data.
Set mydoc = Documents.Add(stmp, Visible:=false)
sDos = Left(stmp, Len(stmp) - 3) & "txt"
mydoc.SaveAs sDos, FileFormat:=wdFormatDOSText
mydoc.Close
' Kill stmp ' careful
Wend
Close #1
Note, that you may not be allowed to write to c:\,
so you may use another directory for the output of dir *.doc /b/s >.
Beware of typos.
I can't test the code right now.
--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Word 2002, Windows XP (german versions)
date: Mon, 25 Aug 2008 02:46:02 -0700
author: Helmut Weber
Re: DOC to TXT
Hello Graham,
thank you very much for your help.
Your program code is going very good.
Mike
Graham Mayor schrieb:
> It would of course have helped had I included the macro :o(
>
> Sub SaveAllAsTXT()
> Dim strFileName As String
> Dim strDocName As String
> Dim strPath As String
> Dim oDoc As Document
> Dim fDialog As FileDialog
> Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
>
> With fDialog
> .Title = "Select folder and click OK"
> .AllowMultiSelect = False
> .InitialView = msoFileDialogViewList
> If .Show <> -1 Then
> MsgBox "Cancelled By User", , "List Folder Contents"
> Exit Sub
> End If
> strPath = fDialog.SelectedItems.Item(1)
> If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
> End With
>
> If Documents.Count > 0 Then
> Documents.Close SaveChanges:=wdPromptToSaveChanges
> End If
> If Left(strPath, 1) = Chr(34) Then
> strPath = Mid(strPath, 2, Len(strPath) - 2)
> End If
> strFileName = Dir$(strPath & "*.doc")
>
> While Len(strFileName) <> 0
> Set oDoc = Documents.Open(strPath & strFileName)
>
> strDocName = ActiveDocument.FullName
> intPos = InStrRev(strDocName, ".")
> strDocName = Left(strDocName, intPos - 1)
> strDocName = strDocName & ".txt"
> oDoc.SaveAs FileName:=strDocName, _
> FileFormat:=wdFormatText
> oDoc.Close SaveChanges:=wdDoNotSaveChanges
> strFileName = Dir$()
> Wend
> End Sub
>
>
date: Tue, 26 Aug 2008 06:55:24 +0300
author: Mike Berger
Re: DOC to TXT
Hello Helmut,
thank you very much for your help.
Your program code is going very good.
The text file I have to change the ÃÃÃ, but then are no problem
Mike
Helmut Weber schrieb:
> Hi Mike,
>
> i'm assuming my answer in the german groups was a bit too complicated.
> The easiest way do get all *.doc documents' folders and names is that, I
> think:
> In the cmd-shell, navigate to your start folder,
> lets say c:\test.
> There you enter:
> dir *.doc /b/s > c:\testdir.txt
> This will create a text-file,
> containing all fullnames of all docs in the actual folder and all subfolders.
> You may check how german characters like Ã,ü,ä,ö are displayed,
> using word or any editor.
>
> Then it goes like this:
> Dim MyDoc as document
> Dim stmp as string
> Dim sDoc as string
> Open "c:\Testdir.txt" For Input As #1
> While Not EOF(1)
> Line Input #1, stmp ' Read line of data.
> Set mydoc = Documents.Add(stmp, Visible:=false)
> sDos = Left(stmp, Len(stmp) - 3) & "txt"
> mydoc.SaveAs sDos, FileFormat:=wdFormatDOSText
> mydoc.Close
> ' Kill stmp ' careful
> Wend
> Close #1
>
> Note, that you may not be allowed to write to c:\,
> so you may use another directory for the output of dir *.doc /b/s >.
> Beware of typos.
> I can't test the code right now.
>
date: Tue, 26 Aug 2008 06:56:09 +0300
author: Mike Berger
Re: DOC to TXT
You can use FileSystemObject to browse files and folders in your specified
folder, and use it to delete the document.
Dim doc As Word.Document
Dim fso, folder, subFolder, docFile
Dim sPath As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("E:\Training\docs")
For Each subFolder In folder.SubFolders
For Each docFile In subFolder.Files
If UCase(Right(docFile.Name, 4)) = ".DOC" Then
sPath = docFile.Path
Set doc = Documents.Open(sPath, , , False, , , , , , , ,
False)
sPath = docFile.Path & ".txt"
doc.SaveAs sPath, Word.wdFormatText
doc.Close False
docFile.Delete
End If
Next
Next
"Mike Berger" ????
news:#qDapNmBJHA.1628@TK2MSFTNGP02.phx.gbl...
> Hello Newsgroup!
>
> I have a foldertree with 3 underfolders, that include about 500 .doc
> documents.
>
> I will open the .doc automatily and then saving as .txt and if possible
> delete the .doc file from the disc.
>
> I can open one file with VBA, when I have the filename in the code.
>
> It's possible to make that with VBA?
>
> Thank you.
> Mike
>
date: Thu, 4 Sep 2008 23:48:12 +0800
author: Kyle.Zhang
|
|