Open .txt file without convert file dialog

D

Doekoe

I need to convert .txt files into Word Document files. I want to do
this in a night job and I am creating a Word Macro that will do this
for me. Problem I encounter is that the Macro wil Open the file, but
the Convert File Dialog opens.

Now I created a macro automatically to see which commands I need to
use to not getting this dialog but it seems not to work.

Is there a way to let Word know which file conversion to use?

Thanks in advance.

Dirk
 
P

Pesach Shelnitz

5/18/2007 - Open .txt file without convert file dialog

The following macro will open a txt file (and other types of files that Word
can convert) and save it in the Word 97-2003 format. Note that the macro
assumes that the original file has a file extension, such as .txt. This macro
processes only one file, but the code can easily be adapted to process
multiple files.

Sub OpenFileAndSaveAsWord()
Const Error_FileNotFound = 5151
Dim fileName As String
Dim doc As Document
Dim pos As Integer

fileName = InputBox("Type the name of your file," _
& vbCrLf & "including its full path.", "Open File", , 1000, 100)
On Error Resume Next
Set doc = Documents.Add(fileName)
If Err.Number = Error_FileNotFound Then
MsgBox "The file specified was not found.", vbCritical Or vbOKOnly,
"File Not Found"
Exit Sub
End If
On Error GoTo 0
pos = InStr(1, fileName, ".", vbTextCompare)
If pos <> 0 Then
fileName = Left(fileName, pos - 1) & ".doc"
End If
doc.SaveAs fileName:=fileName, FileFormat:=wdFormatDocument
doc.Close
End Sub
 
P

Peter Jamieson

With text files you may be encountering one or two dialog boxes: Word's
file format selection dialog, and for text files, the encoding dialog box.

In the Documents.Open method, you can
a. specify that you do not want the format selection dialog by setting
the ConfirmConversions parameter to false:

Documents.Open _
FileName:="Abc.txt", _
ConfirmConversions:=False

b. Specify a particular format - wdOpenFormatAuto will probably work
but you could use wdOpenFormatEncodedText:

Documents.Open _
FileName:="Abc.txt", _
ConfirmConversions:=False, _
Format:=wdOpenformatEncodedText

c. Specify a particular encoding, e.g. if all your text files are
UTF-8, you could use

Documents.Open _
FileName:="Abc.txt", _
ConfirmConversions:=False, _
Format:=wdOpenformatEncodedText, _
Encoding:=msoEncodingUTF8

You can find the built-in enumeration names for Format and Encoding
using the VBA Editor's object browser.

Peter Jamieson

http://tips.pjmsn.me.uk
 
D

Doekoe

Hi Peter,

Thanks for your answer. The trick was, that in my code I had
Format:=wdOpenFormatAuto. I changed this to you variable and now it
opens the txt file as I would like it.

Dirk
 

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