一个XML文件编码是Ansi,我需要手工打开,编码选择utf-8再保存,怎样用vb代码自动执行
1个回答
展开全部
http://www.lbhao.com/detaile-829-1-1.html
'把下面代码复制到模块里
'TxtAutoSel 只读属性,读取文本文件,并返回文件的编码和内容
'TxtFile 读写属性,读取或写入指定编码的文本文件,取值为读取,赋值为写入。
Option Explicit
Enum TXT
ANSI
Unicode
Unicode_big_endian
End Enum
Type AutoSel
Text As String
Code As TXT
End Type
Public Property Get TxtAutoSel(地址$) As AutoSel '自动选择编码
Dim Head%, FN%: FN = VBA.FreeFile
With TxtAutoSel
Open 地址 For Binary As FN
Get FN, 1, Head
Select Case Head
Case &HFEFF: .Text = Unicode_File(FN): .Code = Unicode
Case &HFFFE: .Text = Unicode_big_endian_File(FN): .Code = Unicode_big_endian
Case Else: .Text = ANSI_File(FN): .Code = ANSI
End Select
Close FN
End With
End Property
Public Property Get TxtFile$(地址$, 编码 As TXT)
Dim FN%: FN = VBA.FreeFile
Open 地址 For Binary As FN
Select Case 编码
Case TXT.ANSI: TxtFile = ANSI_File(FN)
Case TXT.Unicode: TxtFile = Unicode_File(FN)
Case TXT.Unicode_big_endian: TxtFile = Unicode_big_endian_File(FN)
End Select
Close FN
End Property
Public Property Let TxtFile(地址$, 编码 As TXT, 数据$)
Dim FN%: FN = VBA.FreeFile
Open 地址 For Output As FN
'清空文件
Close FN
Open 地址 For Binary As FN
Select Case 编码
Case TXT.ANSI: ANSI_File(FN) = 数据
Case TXT.Unicode: Unicode_File(FN) = 数据
Case TXT.Unicode_big_endian: Unicode_big_endian_File(FN) = 数据
End Select
Close FN
End Property
Private Property Get ANSI_File$(编号%)
On Error Resume Next
Dim Hade() As Byte
Get 编号, 1, Hade
ANSI_File = Input(LOF(编号), 编号)
End Property
Private Property Let ANSI_File(编号%, ByVal Text$)
Put 编号, 1, Text
End Property
Private Property Get Unicode_File$(编号%)
Dim Bin() As Byte
On Error Resume Next
ReDim Bin(LOF(编号) - 3)
Get 编号, 3, Bin
Unicode_File = Bin
End Property
Private Property Let Unicode_File(编号%, ByVal Text$)
Put 编号, 1, &HFEFF
Dim Bin() As Byte: Bin = Text
Put 编号, , Bin
End Property
Private Property Get Unicode_big_endian_File$(编号%)
On Error Resume Next
Dim I%, Bin() As Byte
ReDim Bin(LOF(编号) - 3)
Get 编号, 1, I
For I = 0 To UBound(Bin) Step 2
Get 编号, , Bin(I + 1)
Get 编号, , Bin(I)
Next
Unicode_big_endian_File = Bin
End Property
Private Property Let Unicode_big_endian_File(编号%, ByVal Text$)
Put 编号, 1, &HFFFE
Dim I%, Bin() As Byte: Bin = Text
For I = 0 To UBound(Bin) Step 2
Put 编号, , Bin(I + 1)
Put 编号, , Bin(I)
Next
End Property
'读取文本文件:
Text1 = TxtFile("c:\ANSI.txt", ANSI) '读取ANSI文本文件
Text2 = TxtFile("c:\Unicode.txt", Unicode) '读取Unicode文本文件
Text3 = TxtFile("c:\Ube.txt", Unicode_big_endian) '读取Unicode_big_endian文本文件
Dim txt As AutoSel
txt = TxtAutoSel("c:\Auto.txt") '读取未知编码的文本文件
Text4 =txt.Text '获取未知编码文本文件的值
Label1.Caption = Choose(txt.Code + 1, "ANSI", "Unicode", "Unicode_big_endian") '获取文本文件的编码
'保存文本文件
TxtFile("c:\ANSI.txt", ANSI) = Text1 '保存ANSI文本文件
TxtFile("c:\Unicode.txt", Unicode) = Text2 '保存Unicode文本文件
TxtFile("c:\Ube.txt", Unicode_big_endian) = Text 3 '保存Unicode_big_endian文本文件
文章来自网络
'把下面代码复制到模块里
'TxtAutoSel 只读属性,读取文本文件,并返回文件的编码和内容
'TxtFile 读写属性,读取或写入指定编码的文本文件,取值为读取,赋值为写入。
Option Explicit
Enum TXT
ANSI
Unicode
Unicode_big_endian
End Enum
Type AutoSel
Text As String
Code As TXT
End Type
Public Property Get TxtAutoSel(地址$) As AutoSel '自动选择编码
Dim Head%, FN%: FN = VBA.FreeFile
With TxtAutoSel
Open 地址 For Binary As FN
Get FN, 1, Head
Select Case Head
Case &HFEFF: .Text = Unicode_File(FN): .Code = Unicode
Case &HFFFE: .Text = Unicode_big_endian_File(FN): .Code = Unicode_big_endian
Case Else: .Text = ANSI_File(FN): .Code = ANSI
End Select
Close FN
End With
End Property
Public Property Get TxtFile$(地址$, 编码 As TXT)
Dim FN%: FN = VBA.FreeFile
Open 地址 For Binary As FN
Select Case 编码
Case TXT.ANSI: TxtFile = ANSI_File(FN)
Case TXT.Unicode: TxtFile = Unicode_File(FN)
Case TXT.Unicode_big_endian: TxtFile = Unicode_big_endian_File(FN)
End Select
Close FN
End Property
Public Property Let TxtFile(地址$, 编码 As TXT, 数据$)
Dim FN%: FN = VBA.FreeFile
Open 地址 For Output As FN
'清空文件
Close FN
Open 地址 For Binary As FN
Select Case 编码
Case TXT.ANSI: ANSI_File(FN) = 数据
Case TXT.Unicode: Unicode_File(FN) = 数据
Case TXT.Unicode_big_endian: Unicode_big_endian_File(FN) = 数据
End Select
Close FN
End Property
Private Property Get ANSI_File$(编号%)
On Error Resume Next
Dim Hade() As Byte
Get 编号, 1, Hade
ANSI_File = Input(LOF(编号), 编号)
End Property
Private Property Let ANSI_File(编号%, ByVal Text$)
Put 编号, 1, Text
End Property
Private Property Get Unicode_File$(编号%)
Dim Bin() As Byte
On Error Resume Next
ReDim Bin(LOF(编号) - 3)
Get 编号, 3, Bin
Unicode_File = Bin
End Property
Private Property Let Unicode_File(编号%, ByVal Text$)
Put 编号, 1, &HFEFF
Dim Bin() As Byte: Bin = Text
Put 编号, , Bin
End Property
Private Property Get Unicode_big_endian_File$(编号%)
On Error Resume Next
Dim I%, Bin() As Byte
ReDim Bin(LOF(编号) - 3)
Get 编号, 1, I
For I = 0 To UBound(Bin) Step 2
Get 编号, , Bin(I + 1)
Get 编号, , Bin(I)
Next
Unicode_big_endian_File = Bin
End Property
Private Property Let Unicode_big_endian_File(编号%, ByVal Text$)
Put 编号, 1, &HFFFE
Dim I%, Bin() As Byte: Bin = Text
For I = 0 To UBound(Bin) Step 2
Put 编号, , Bin(I + 1)
Put 编号, , Bin(I)
Next
End Property
'读取文本文件:
Text1 = TxtFile("c:\ANSI.txt", ANSI) '读取ANSI文本文件
Text2 = TxtFile("c:\Unicode.txt", Unicode) '读取Unicode文本文件
Text3 = TxtFile("c:\Ube.txt", Unicode_big_endian) '读取Unicode_big_endian文本文件
Dim txt As AutoSel
txt = TxtAutoSel("c:\Auto.txt") '读取未知编码的文本文件
Text4 =txt.Text '获取未知编码文本文件的值
Label1.Caption = Choose(txt.Code + 1, "ANSI", "Unicode", "Unicode_big_endian") '获取文本文件的编码
'保存文本文件
TxtFile("c:\ANSI.txt", ANSI) = Text1 '保存ANSI文本文件
TxtFile("c:\Unicode.txt", Unicode) = Text2 '保存Unicode文本文件
TxtFile("c:\Ube.txt", Unicode_big_endian) = Text 3 '保存Unicode_big_endian文本文件
文章来自网络
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询