MTNQ > 30-11-15, 01:34 AM
Function ProtectDoc(ByVal strDocPath As String, strPwd As String, blnProtect As Boolean) As Boolean
Dim wrdApp As Object
Dim wrdDoc As Object
Dim strFolderPath As String
Dim strFileName As String
Dim strFileType As String
Dim blnQuitApp As Boolean
Dim blnCloseDoc As Boolean
On Error Resume Next
If strDocPath = Trim(strDocPath) = "" Then Exit Function
strFolderPath = Left$(strDocPath, InStrRev(strDocPath, "\"))
strFileName = Mid$(strDocPath, Len(strFolderPath) + 1)
strFileType = Mid$(strFileName, InStrRev(strFileName, ".") + 1)
If strFolderPath = "" Or strFileName = "" Or strFileType = "" Then
MsgBox "Duong dan """ & strDocPath & """ khong dung", , "Loi"
Exit Function
End If
If strFileType <> "doc" And strFileType <> "docx" Then Exit Function
Set wrdApp = GetObject(, "Word.Application")
If wrdApp Is Nothing Then
Err.Clear
Set wrdApp = CreateObject("Word.Application"): blnQuitApp = True
Set wrdDoc = wrdApp.Documents.Open(strDocPath): blnCloseDoc = True
Else
Set wrdDoc = wrdApp.Documents(strFileName)
If Err.Number <> 0 Then
Err.Clear
Set wrdDoc = wrdApp.Documents.Open(strDocPath): blnCloseDoc = True
End If
End If
If Err.Number <> 0 Then GoTo ErrorHandler
On Error GoTo ErrorHandler
If blnProtect Then
If wrdDoc.ProtectionType = -1 Then wrdDoc.Protect 2, , strPwd: wrdDoc.Save
Else
If wrdDoc.ProtectionType <> -1 Then wrdDoc.Unprotect strPwd: wrdDoc.Save
End If
If (Not wrdDoc Is Nothing) And blnCloseDoc Then wrdDoc.Close True
ProtectDoc = True
Exit_ErrorHandler:
If Not wrdDoc Is Nothing Then Set wrdDoc = Nothing
If Not wrdApp Is Nothing Then
If blnQuitApp Then wrdApp.Quit
Set wrdApp = Nothing
End If
Exit Function
ErrorHandler:
ProtectDoc = False
Select Case Err.Number
Case 5174
MsgBox "Khong tim thay file: " & vbCrLf & strDocPath, , "Loi"
Case 5485
If blnCloseDoc Then wrdDoc.Close False
MsgBox "Mat khau: """ & strPwd & """ khong dung" & vbCrLf & strDocPath, , "Loi"
Case Else
MsgBox "File: """ & strDocPath & """" & vbCrLf & Err.Description, _
vbExclamation, "Error ProtectDoc: " & Err.Number
End Select
Err.Clear
GoTo Exit_ErrorHandler
End Function
MTNQ > 30-11-15, 02:05 AM
jeck09nt > 30-11-15, 09:37 AM
jeck09nt > 02-12-15, 12:46 PM
Pathpict = Me. DocMgrPathPict
Const wdAlignPageNumberCenter = 1
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
docfile.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = ""
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = Now() & " - " & Me.txtUserName
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Font.Name = "Times New Roman"
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Font.Size = "11"
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Font.Color = wdColorRed
docfile.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs(1).Alignment = wdAlignParagraphRight
docfile.Sections.Item(1).Footers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture (Pathpict)
docfile.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Alignment = wdAlignParagraphRight
If ChkChensotrang = True Then
docfile.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Bold = True
docfile.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Size = "11"
docfile.Sections(1).Footers(1).PageNumbers.Add (wdAlignPageNumberCenter)
End If
MTNQ > 09-12-15, 07:41 AM
Option Compare Database
Option Explicit
Private Const wdAllowOnlyFormFields = 2
Private Const wdHeaderFooterPrimary = 1
Private Const wdAlignParagraphLeft = 0
Private Const wdAlignPageNumberCenter = 1
Private Const wdAlignParagraphRight = 2
Private Const wdColorRed = 255
Function ProtectDoc(ByVal strDocPath As String, strPwd As String, blnProtect As Boolean, varText As Variant) As Boolean
Dim wrdApp As Object
Dim wrdDoc As Object
Dim strFolderPath As String
Dim strFileName As String
Dim strFileType As String
Dim blnQuitApp As Boolean
Dim blnCloseDoc As Boolean
On Error Resume Next
If strDocPath = Trim(strDocPath) = "" Then Exit Function
strFolderPath = Left$(strDocPath, InStrRev(strDocPath, "\"))
strFileName = Mid$(strDocPath, Len(strFolderPath) + 1)
strFileType = Mid$(strFileName, InStrRev(strFileName, ".") + 1)
If strFolderPath = "" Or strFileName = "" Or strFileType = "" Then
MsgBox "Duong dan """ & strDocPath & """ khong dung", , "Loi"
Exit Function
End If
If strFileType <> "doc" And strFileType <> "docx" Then Exit Function
Set wrdApp = GetObject(, "Word.Application")
If wrdApp Is Nothing Then
Err.Clear
Set wrdApp = CreateObject("Word.Application"): blnQuitApp = True
Set wrdDoc = wrdApp.Documents.Open(strDocPath): blnCloseDoc = True
Else
Set wrdDoc = wrdApp.Documents(strFileName)
If Err.Number <> 0 Then
Err.Clear
Set wrdDoc = wrdApp.Documents.Open(strDocPath): blnCloseDoc = True
End If
End If
If Err.Number <> 0 Then GoTo ErrorHandler
On Error GoTo ErrorHandler
If blnProtect Then
If wrdDoc.ProtectionType = -1 Then
InsertText wrdDoc, varText
wrdDoc.Protect 2, , strPwd: wrdDoc.Save
Else
wrdDoc.Unprotect strPwd
InsertText wrdDoc, varText
wrdDoc.Protect 2, , strPwd: wrdDoc.Save
End If
Else
If wrdDoc.ProtectionType <> -1 Then wrdDoc.Unprotect strPwd: wrdDoc.Save
End If
If (Not wrdDoc Is Nothing) And blnCloseDoc Then wrdDoc.Close True
ProtectDoc = True
Exit_ErrorHandler:
If Not wrdDoc Is Nothing Then Set wrdDoc = Nothing
If Not wrdApp Is Nothing Then
If blnQuitApp Then wrdApp.Quit
Set wrdApp = Nothing
End If
Exit Function
ErrorHandler:
ProtectDoc = False
Select Case Err.Number
Case 5174
MsgBox "Khong tim thay file: " & vbCrLf & strDocPath, , "Error ProtectDoc: " & Err.Number
Case 5485
If blnCloseDoc Then wrdDoc.Close False
MsgBox "Mat khau: """ & strPwd & """ khong dung" & vbCrLf & strDocPath, , "Loi"
Case Else
MsgBox "(File: " & strDocPath & ")" & vbCrLf & "Description: " _
& Err.Description, vbExclamation, "Error ProtectDoc: " & Err.Number
End Select
On Error GoTo 0
GoTo Exit_ErrorHandler
End Function
Function InsertText(docfile As Object, varText As Variant)
On Error GoTo ErrorHandler
Dim strDate As String
Dim strCurUser As String
Dim blnChensotrang As Boolean
Dim strPathpict As String
strDate = varText(0)
strCurUser = varText(1)
blnChensotrang = varText(2)
strPathpict = varText(3)
With docfile
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = ""
With .Sections(1).Headers(wdHeaderFooterPrimary).Range
If strDate & strCurUser <> "" Then
.Text = strDate & " - " & strCurUser
.Font.Name = "Times New Roman"
.Font.Size = "11"
.Font.Color = wdColorRed
.Paragraphs(1).Alignment = wdAlignParagraphRight
End If
End With
If strPathpict <> "" Then
.Sections.Item(1).Footers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture (strPathpict)
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Alignment = wdAlignParagraphRight
End If
If blnChensotrang = True Then
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Bold = True
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Size = "11"
.Sections(1).Footers(1).PageNumbers.Add (wdAlignPageNumberCenter)
End If
End With
Exit_ErrorHandler:
Exit Function
ErrorHandler:
Select Case Err.Number
Case 5152
MsgBox "Khong tim thay file: " & vbCrLf & strPathpict, , "Error InsertText: " & Err.Number
Resume Next
Case Else
MsgBox Err.Description, vbExclamation, "Error InsertText: " & Err.Number
End Select
Resume Exit_ErrorHandler
End Function
MTNQ > 11-12-15, 05:44 PM
jeck09nt > 15-12-15, 12:58 PM
MsgBox "File Word da duoc Protect, ban phai thuc hien Unprotect truoc khi thuc hien lai Protect !", vbCritical, "Thông báo"
jeck09nt > 15-12-15, 01:00 PM
MTNQ > 16-12-15, 01:29 AM
If wrdDoc.ProtectionType = -1 Then
InsertText wrdDoc, varText
wrdDoc.Protect 2, , strPwd: wrdDoc.Save
Else
MsgBox "File Word da duoc Protect, ban phai thuc hien Unprotect truoc khi thuc hien lai Protect !", vbCritical, "Thông báo"
ProtectDoc = False
If blnCloseDoc Then wrdDoc.Close False
GoTo Exit_ErrorHandler
End If
Function UnInsertText(docfile As Object)
On Error GoTo ErrorHandler
With docfile
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = ""
End With
Exit_ErrorHandler:
Exit Function
ErrorHandler:
MsgBox Err.Description, vbExclamation, "Error UnInsertText: " & Err.Number
Resume Exit_ErrorHandler
End Function
MTNQ > 16-12-15, 01:52 AM
Dim intResponse As Integer
intResponse = MsgBox("File Word: " & strFileName & vbCrLf & " Da duoc Protect, neu thuc hien tiep chon YES, khong chon NO !", vbYesNo, "Thông báo")
If intResponse = vbYes Then
Dim strOldPwd As String
strOldPwd = InputBox("Hay nhap mat khau cu da protect", "Thong bao !")
If strOldPwd > "" Then
wrdDoc.Unprotect strOldPwd
InsertText wrdDoc, varText
wrdDoc.Protect 2, , strPwd: wrdDoc.Save
Else
ProtectDoc = False
If blnCloseDoc Then wrdDoc.Close False
GoTo Exit_ErrorHandler
End If
Else
ProtectDoc = False
If blnCloseDoc Then wrdDoc.Close False
GoTo Exit_ErrorHandler
End If