Bạn sẽ có thể chuyển một tập tin text thành một tập tin word có password. Một cách để bạn giao tiếp với Word thông qua đối tượng bằng ngôn ngữ VB
Chúng ta sẽ mở tập tin BigText và chuyển thành BigText.doc và có password bảo vệ là 'enter'. (Bạn tạo tập tin text bigtext trước). Tạo một dự án VB6 và sử dụng các dòng code sau:
Option Explicit
Private Sub Form_Load()
HideTextINWord
End
End Sub
Private Sub HideTextINWord()
Dim wdA As Object, wdD As Object
Dim WkDir As String, Source As String, Destination As String
On Error Resume Next
Set wdA = GetObject(, ''Word.Application'')
If Err = 429 Then
Err.Clear
Set wdA = CreateObject(''Word.Application'')
If Err = 429 Then ' Không tìm thấy Word
MsgBox ''Word not available''
Err.Clear
Exit Sub
End If
End If
WkDir = App.Path
Destination = WkDir & ''\'' & ''BigWord.doc''
If Dir$(Destination) <> '''' Then
MsgBox Destination & '' bị trùng''
GoTo ErrEnd
End If
Source = WkDir & ''\'' & ''BigText''
Set wdD = wdA.Documents.Open(Source)
If wdD = Null Then
MsgBox ''There must be a file called 'BigText' in '' & WkDir
GoTo ErrEnd
End If
wdD.SaveAs FileName:=Destination, FileFormat:=0, Password:=''enter''
wdD.Close
MsgBox ''Tập tin'' & Destination & '' được tạo''
ErrEnd:
wdA.Quit
Set wdD = Nothing
Set wdA = Nothing
End Sub