2016-03-23 4 views
0

ここにあなたの質問があります。はテキストファイルを読み込みますそれからテキストファイルを読み込むためにそれを処理します。 ファイルシステムオブジェクトメソッド。VBAテストテキストファイルのエンコーディング

  • システムのデフォルト
  • ASCII
  • UNICODE

:およびそれのための構文は、この object.OpenTextFile(filename[, iomode[, create[, format]]])で、今、オプションのパラメータ formatは3つのエンコーディングオプションでテキストファイルを読み取るために使用されます

私の質問:

どのようにファイルを事前にエンコードするのですか?私は右のFormatを選ぶことができますか?

ここでは、プロジェクトの一部として使用しているサンプルコードを示します。この関数では、タイプが(rtf、doc、rtf)のいずれかのファイルタイプ(txt、docx、doc、rtf) DOCX)実際のファイルオブジェクト(文書)が返され、ファイルがテキストファイルであれば、新しいドキュメントという新しい文書に行ずつ読み込んで返します。

Sub test() 
    ReadTxtOrDocIntoDocument_1 ("C:\Test.txt") 
End Sub 

Function ReadTxtOrDocIntoDocument_1(ByVal DocPath As String) As Document 
'in this func we only create a new doc for the text file 
'because the opening of a text file in word is messy 
'if the file is supported then just open it for further processing 
'====================================================== 
    If Not DocPath = "" Then 'check if the path is not empty 
     'make sure one temp doc exists throughout the processing time 
     Dim fileExt As String 'stores the file extension of the list file 
     fileExt = Right(DocPath, Len(DocPath) - InStrRev(DocPath, ".")) 
     '====================================================== 
     'let's check if the file is text or doc 
     Select Case LCase(fileExt) 
     Case "docx" 
      Set ReadTxtOrDocIntoDocument_1 = Documents.Open(DocPath) 
     Case "doc" 
      Set ReadTxtOrDocIntoDocument_1 = Documents.Open(DocPath) 
     Case "rtf" 
      Set ReadTxtOrDocIntoDocument_1 = Documents.Open(DocPath) 
     Case "txt" 
      'a doc that recieves the list words for comparison 
      Set ReadTxtOrDocIntoDocument_1 = Documents.Add 
      '====================================================== 
      Dim FSO As Object 
      Set FSO = CreateObject("Scripting.FileSystemObject") 
      '====================================================== 
      Const ForReading = 1 
      Const OpenAsUNICODE = -1 
      Const OpenAsSystemDefault = -2 
      '====================================================== 
      Dim TxtFile As Object 
      Set TxtFile = FSO.OpenTextFile(DocPath, ForReading, , OpenAsSystemDefault) 
      Dim ThisLine As String 
      Dim i As Integer 
      i = 0 
      '====================================================== 
      Do Until (TxtFile.AtEndOfStream) 'keep looping until end of file 
       ThisLine = TxtFile.ReadLine 
       'go to doc starting point 
       ReadTxtOrDocIntoDocument_1.Range.MoveStart unit:=wdStory, Count:=1 
       'insert text after the begining 
       ReadTxtOrDocIntoDocument_1.Range.InsertAfter ThisLine 
       'go to end of the doc 
       ReadTxtOrDocIntoDocument_1.Range.MoveEnd unit:=wdStory, Count:=1 
       'insert a new line (paragraph) 
       ReadTxtOrDocIntoDocument_1.Range.Paragraphs.Add 
       'increase counter to read next txt line 
       i = i + 1 
      Loop 
      '====================================================== 
      TxtFile.Close 

      Set FSO = Nothing 
      Set TxtFile = Nothing 
     End Select 

    Else 

     MsgBox "no file path was provided" 
     Exit Function 
     'close the tempDoc after processing 
    End If 
End Function 

答えて

1

は以下のVBScriptコードでした。最小限の変更を加えてExcel 2010でVBAコードとして実行します。 Microsft Scripting Runtimeへの参照を追加する必要があります。適切な型指定、エラー処理などで一括して処理することができます。テキストファイルの文字数が2文字未満の場合は失敗します。それを処理したいかもしれません。このエンコードの問題の主な問題は、ファイルにエンコード属性がないことです。ここでは、Byte-Order-Markを使用してエンコーディングを識別しています。 UTF-32/UTF-16/UTF-8/ANSI/ASCII/EBCDICを決定するためにコンテンツの統計分析を行うには、BOMに依存しないエンコードの真のテストでは、ファイルを大きくする必要があります。

Public Function IsUnicodeFile(FilePath) 
     Dim objFSO 
     Dim objStream 

     Dim intAsc1Chr 
     Dim intAsc2Chr 


     Set objFSO = CreateObject("Scripting.FileSystemObject") 
     If (objFSO.FileExists(FilePath) = False) Then 
      IsUnicodeFile = False 
      Exit Function 
     End If 

     ' 1=Read-only, False==do not create if not exist, -1=Unicode 0=ASCII 
     Set objStream = objFSO.OpenTextFile(FilePath, 1, False, 0) 
     intAsc1Chr = Asc(objStream.Read(1)) 
     intAsc2Chr = Asc(objStream.Read(1)) 
     objStream.Close 

     If (intAsc1Chr = 255) And (intAsc2Chr = 254) Then 
      IsUnicodeFile = True 
     Else 
      IsUnicodeFile = False 
     End If 

     Set objStream = Nothing 
     Set objFSO = Nothing 
End Function 
+0

ありがとうございました。この物は多くの作業が必要です。ここでそれについて話をしているが、C#のための別のスタックページです(http://stackoverflow.com/questions/90838/how-can-i-detect-the-encoding-codepage-of-a-text-file?rq=1 ) –

関連する問題