after protect a word document, cannot unprotect it

August 04th, 2011 - 11:41 pm ET by yinruzhi | Report spam
hi,everyone

i write two wsf file, one to protect a word document and another to
unprotect it

code of protect one:
<job id="protect_word">
<script language="VBScript">
Option Explicit

Dim docPath, errorFile
docPath = WScript.Arguments(0)
errorFile = WScript.Arguments(1)

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim errTextFile
Set errTextFile = fs.CreateTextFile(errorFile, True)

Dim wordApp, document
Set wordApp = CreateObject("Word.Application")
wordApp.DisplayAlerts = True

Set document = wordApp.Documents.Open(docPath)

Dim password
password="123"

document.Protect 0, False, password, False, False

If Err.Number = 0 Then
document.Save
Else
errTextFile.WriteLine("error information: " &
Err.Description)
End If

errTextFile.Close()

document.Close False
Set document = Nothing
wordApp.Quit
Set wordApp = Nothing

WScript.Quit(Err.Number)

</script>
</job>

code of unprotect one:
<job id="unprotect_word">
<script language="VBScript">
Option Explicit

Dim docPath, errorFile
docPath = WScript.Arguments(0)
errorFile = WScript.Arguments(1)

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim errTextFile
Set errTextFile = fs.CreateTextFile(errorFile, True)

Dim wordApp, document
Set wordApp = CreateObject("Word.Application")
wordApp.DisplayAlerts = True

Set document = wordApp.Documents.Open(docPath)

Dim password
password="123"
If document.ProtectionType <> -1 Then
document.Unprotect password
End If

If Err.Number = 0 Then
document.Save
Else
errTextFile.WriteLine("发生错误, 错误信息: " & Err.Description)
End If

errTextFile.Close()

document.Close False
Set document = Nothing
wordApp.Quit
Set wordApp = Nothing

WScript.Quit(Err.Number)

</script>
</job>

but when i use the second to unprotect the word document, there is a
error: password is incorrect
i google it, and i didn't find the solution

Any help would be greatly appreciated.

thanks

zhi
email Follow the discussionReplies 1 replyReplies Make a reply

Replies

#1 Mayayana
August 05th, 2011 - 08:39 am ET | Report spam
I don't use Word, but since you haven't had any help...
This link shows a different syntax:

http://msdn.microsoft.com/en-us/lib...45016.aspx

Document.Unprotect Password:=strPassword

Also, the docs make an unusual security recommendation
that the password should not be hard-coded. I wonder if
it's possible that newer versions of Word enforce that by
showing an input. (That's the sort of thing Microsoft might do.)

If you don't figure it out you might want to try an
MS Office newsgroup, since it's really a Word issue rather
than a VBS issue.


"yinruzhi" wrote in message
news:
hi,everyone

i write two wsf file, one to protect a word document and another to
unprotect it

code of protect one:
<job id="protect_word">
<script language="VBScript">
Option Explicit

Dim docPath, errorFile
docPath = WScript.Arguments(0)
errorFile = WScript.Arguments(1)

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim errTextFile
Set errTextFile = fs.CreateTextFile(errorFile, True)

Dim wordApp, document
Set wordApp = CreateObject("Word.Application")
wordApp.DisplayAlerts = True

Set document = wordApp.Documents.Open(docPath)

Dim password
password="123"

document.Protect 0, False, password, False, False

If Err.Number = 0 Then
document.Save
Else
errTextFile.WriteLine("error information: " &
Err.Description)
End If

errTextFile.Close()

document.Close False
Set document = Nothing
wordApp.Quit
Set wordApp = Nothing

WScript.Quit(Err.Number)

</script>
</job>

code of unprotect one:
<job id="unprotect_word">
<script language="VBScript">
Option Explicit

Dim docPath, errorFile
docPath = WScript.Arguments(0)
errorFile = WScript.Arguments(1)

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim errTextFile
Set errTextFile = fs.CreateTextFile(errorFile, True)

Dim wordApp, document
Set wordApp = CreateObject("Word.Application")
wordApp.DisplayAlerts = True

Set document = wordApp.Documents.Open(docPath)

Dim password
password="123"
If document.ProtectionType <> -1 Then
document.Unprotect password
End If

If Err.Number = 0 Then
document.Save
Else
errTextFile.WriteLine("å

Similar topics