Protect txt file into word doc with password

Saves txt file as word doc with password.
Passing txtfile having full path and filename, same as SaveAsWordDoc.
Then passing password to have document protect it with.
There is no reason why it should not work for docx, just pass word doc ending with docx, I still need to test it though.

CodeFunctionName
What is this?

Public

Not Tested

Imported
Function HideTxt_into_Doc(txtFile, SaveAsWordDoc, WordDocPwd)
    ' Saves txt file as word doc with password.
    ' txtFile and SaveAsWordDoc are the full path with filename
    ' Returns 1 or 0 if operation was successful or no.
    ' Adds COPY of TEXT FILE TO A PASSWORD PROTECTED WORD DOC.
    ' A program that utilises Word for Windows to open a file called 'BigText' gives the file the name 'BigWord.doc' and saves the file with the password 'enter'.
    Dim wdA As Object, wdD As Object
    Dim WkDir As String, Source As String, Destination As String
    Rett = 0
    ' Find Word
    On Error Resume Next
    Set wdA = GetObject(, "Word.Application") ' Is it already open?
    If Err = 429 Then
        Err.Clear
        Set wdA = CreateObject("Word.Application") '    No, then start Word
        If Err = 429 Then '    Sorry, could not find Word
            ' MsgBox "Word not available"
            Err.Clear
            Goto ByeBye
        End If
    End If
    WkDir = App.Path ' Retrieve our current location on the hard drive
    Destination = SaveAsWordDoc ' WkDir & "\" & "BigWord.doc" ' Name of the Destination document
    If Dir$(Destination) < > "" Then ' Check to see it does not already exists
        ' MsgBox Destination & " must not already exist"
        GoTo ByeBye
    End If
    Source = txtFile ' WkDir & "\" & "BigText" ' Name of the Source document
    Set wdD = wdA.Documents.Open(Source) ' Open the Source with Word
    If wdD = Null Then
        ' MsgBox "There must be a file called 'BigText' in " & WkDir
        GoTo ByeBye
    End If
    wdD.SaveAs FileName:=Destination, FileFormat:=0, Password:=WordDocPwd ' Create the Destination document and password protect it
    ' wdD.SaveAs FileName:=Destination, FileFormat:=0, Password:="enter" ' Create the Destination document and password protect it
    wdD.Close ' Close the document
    Rett = 1 ' MsgBox "The document " & Destination & " is created"
ByeBye:
    HideTxt_into_Doc = Rett
    wdA.Quit ' Terminate Word
    Set wdD = Nothing
    Set wdA = Nothing
End Function

txtFile, SaveAsWordDoc, WordDocPwd

Views 142

Downloads 72

CodeID
DB ID