WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Setting Password in Word


Here is the code I used to set a password in Word. I have not been able to figure out how to "unset" the password using OLE automation, as yet.

Example 1:


;Password Set
;Point to a directory and this utility will set the password for each
;word document in a directory.

AddExtender("WWWSK34i.DLL")

GoSub DirList
GoSub InitiateLog
GoSub filelist2
:Terminate
exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:InitiateLog
Worddoc = ObjectOpen("Word.Application")
objectclose(Worddoc) ; if word is already running or is in a hung session, close it

docpwd=AskLine("Password","Enter the Password:","") 

:back
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Select the directory where all the word docs are located that need a character count
:DirList
dskscn=(DiskScan(2))
dirsel=AskItemList("Select Drive", dskscn, @Tab, @Sorted, @Single)
dl=StrCat(dirsel, "\")
DirChange("%dl%")
while @TRUE ; Loop until break...
dirs = DirItemize("*")
If dirs == "" Then break
DirChk = AskItemList("Select directory", dirs, @TAB, @sorted, @single)
If DirChk == "" Then break
DirChange(DirChk)
endwhile
Return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Read all the word documents into an array, set password for each and close the document

:fileList2
list=FileItemize("*.*")
GoSub NoFilesFound
array=Arrayize(list, @tab)
filecount=ItemCount(list, @Tab)
Worddoc = ObjectOpen("Word.Application")
WDOC = Worddoc.Application
WDOC.visible = @false 

j=0
While j <= (filecount-1)

docname = StrCat(DirGet(), array[j])
docs = WDOC.documents
docs.open(docname)
wsas = WDOC.activedocument
wsas.Password = "%docpwd%"

docs.close
j=j+1
EndWhile

WDOC.quit
objectclose(wsas)
objectclose(WDOC)
objectclose(Worddoc)
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:NoFilesFound
If list == "" Then GoSub Messager
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:Messager
Display(5, "No Files Found", "The directory does not contain any files. %@crlf% The program will now terminate.")
GoTo Terminate
Return 


Example 2:



;=========================================================================
; Set password on Word Docs
;
; Here is a dialog version you can play with.  Does your version work
; like you expect?  I tried with this, which is basically your code,
; and it didn't seem to be setting a password.
;
;=========================================================================

;=========================================================================
:initialize

  IntControl(12,5,0,0,0)                           ; Allow quite termination

  TimeDelay(2)                              ; Allow time to press "ctrl" or "shift"
  exe = IntControl(1004,0,0,0,0)
  pgmname = ItemExtract(-1,exe,"\")
  pgm = ItemExtract(1,pgmname,".")

  ; Turn on debugging on the fly?
  If IsKeyDown(@CTRL)
    Debug(@ON)
  EndIf

  ; Turn on debug tracing on the fly?
  If IsKeyDown(@SHIFT)
    DebugTrace(@ON,"%pgm%.txt")             ; In the current directory
  EndIf

  files = ""
  password = ""
  Worddoc = ObjectOpen("Word.Application")
  ObjectClose(Worddoc) ; if word is already running or is in a hung session, close it

;=========================================================================
:main

  #DefineSubRoutine PWCBP(PW_Handle,PW_Message,PW_ID,rsvd1,rsvd2)
     ;DialogprocOptions Constants
     MSG_INIT=0                ; The one-time initilization
     MSG_TIMER=1               ; Timer event
     MSG_BUTTONPUSHED=2        ; Pushbutton or Picturebutton
     MSG_RADIOPUSHED=3         ; Radiobutton clicked
     MSG_CHECKBOX=4            ; Checkbox clicked
     MSG_EDITBOX=5             ; Editbox or Multilinebox
     MSG_FILESELECT=6          ; Filelistbox
     MSG_ITEMSELECT=7          ; Itembox
     MSG_COMBOCHANGE=8         ; Combobox/Droplistbox
     MSG_CALENDAR=9            ; Calendar date change
     MSG_SPINNER=10            ; Spinner number change
     MSG_CLOSEVIA49=11         ; Close clicked (Enabled via Intcontrol 49)
     MSG_FILEBOXDOUBLECLICK=12 ; Get double-click message on a FileListBox
     MSG_ITEMOXDOUBLECLICK=13  ; Get double-click message on an ItemBox
     DPO_DISABLESTATE=1000     ; codes -1=GetSetting 0=EnableDialog 1=DisableDialog
     DPO_CHANGEBACKGROUND=1001 ; -1=GetSetting otherise bitmap or color string
     ;DialogControlState Constants
     DCSTATE_SETFOCUS=1        ; Give Control Focus
     DCSTATE_QUERYSTYLE=2      ; Query control's style
     DCSTATE_ADDSTYLE=3        ; Add control style
     DCSTATE_REMOVESTYLE=4     ; Remove control style
     DCSTATE_GETFOCUS=5        ; Get control that has focus
     DCSTYLE_INVISIBLE=1       ; Set Control Invisible
     DCSTYLE_DISABLED=2        ; Set Control Disabled
     DCSTYLE_NOUSERDATA=4      ; Note: Setable via DialogControlState function ONLY SPINNER control only
     DCSTYLE_READONLY=8        ; Sets control to read-only (user cannot type in data) EDITBOX MULTILINEBOX SPINNER
     DCSTYLE_PASSWORD=16       ; Sets 'password mode' where only *'s are displayed EDITBOX
     DCSTYLE_DEFAULTBUTTON=32  ; Sets a button as a the default button PUSHBUTTON PICTUREBUTTON
     DCSTYLE_DIGITSONLY=64     ; Set edit box to accept digits only EDITMOX MULTILINEBOX
     DCSTYLE_FLAT=128          ; Makes a 'flat' hyperlink-looking button PUSHBUTTON PICTUREBUTTON
     ;DialogControlSet / DialogControlGet Constants
     DC_CHECKBOX=1             ; CHECKBOX
     DC_RADIOBUTTON=2          ; RADIOBUTTON
     DC_EDITBOX=3              ; EDITBOX MULTILINEBOX
     DC_TITLE=4                ; PICTURE RADIOBUTTON CHECKBOX PICTUREBUTTON VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON
     DC_ITEMBOXCONTENTS=5      ; ITEMBOX FILELISTBOX DROPLISTBOX
     DC_ITEMBOXSELECT=6        ; ITEMBOX FILELISTBOX DROPLISTBOX
     DC_CALENDAR=7             ; CALENDAR
     DC_SPINNER=8              ; SPINNER
     DC_MULTITABSTOPS=9        ; MULTILINEBOX
     DC_ITEMSCROLLPOS=10       ; ITEMBOX FILELISTBOX
     DC_BACKGROUNDCOLOR=11     ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FILELISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
     DC_PICTUREBITMAP=12       ; PICTURE PICTUREBUTTON
     DC_TEXTCOLOR=13           ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FIELLISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
     DC_ITEMBOXADD=14          ; ITEMBOX FILELISTBOX DROPLISTBOX
     DC_ITEMBOXREMOVE=15       ; ITEMBOX FILELISTBOX DROPLISTBOX


     Switch PW_Message
        Case MSG_INIT
           DialogProcOptions(PW_Handle,MSG_BUTTONPUSHED,@TRUE)
           Return(-1)

       Case MSG_BUTTONPUSHED
          Switch PW_ID
             Case 001           ; set password
               pw = DialogControlGet(PW_Handle,011,DC_EDITBOX)
               If pw == ""
                 ans = AskYesNo("Warning","Do you want to set the password to (null)")
                 If ans == @NO Then Return(-2)
               EndIf
               Worddoc = ObjectOpen("Word.Application")
               WDOC = Worddoc.Application
               WDOC.visible = @FALSE

               For ii = 1 To filecount

                 docname = ItemExtract(ii,files,@TAB)
                 docs = WDOC.documents
                 docs.open(docname)
                 wsas = WDOC.activedocument
                 wsas.Password = "%pw%"

                 docs.close
                 DialogControlSet(PW_Handle,009,DC_TITLE,StrCat(docname," password updated"))
               Next

               WDOC.quit
               ObjectClose(wsas)
               ObjectClose(WDOC)
               ObjectClose(Worddoc)

               DialogControlSet(PW_Handle,009,DC_TITLE,"Done")

               :CANCEL
               Return(-2)

             Case 002            ; Exit
                Return(-1)

             Case 004            ; Browse for directory
               flags=1|2
               directory = AskDirectory("Select a directory:","","","",0)
               dispdir = ItemRemove(-1,directory,"\")
               DialogControlSet(PW_Handle,003,DC_EDITBOX,dispdir)
               filestoget = StrCat(directory,"*.doc")
               DirChange(dispdir)
               files = FileItemize("*.doc")
               filecount = ItemCount(files,@TAB)
               DialogControlSet(PW_Handle,005,DC_ITEMBOXCONTENTS,files)
               DialogControlSet(PW_Handle,009,DC_TITLE,StrCat(filecount," files found"))
               Return(-2)

          EndSwitch    ;PW_ID
          Return(-1)      ;  Do default processing


     EndSwitch       ; PW_Message
     Return(-1)      ;  Do default processing
  #EndSubRoutine       ;End of Dialog Callback PWCallbackProc



  PWFormat=`WWWDLGED,6.1`

  PWCaption=`Set Password on Word Documents`
  PWX=-01
  PWY=-01
  PWWidth=206
  PWHeight=218
  PWNumControls=011
  PWProcedure=`PWCBP`
  PWFont=`DEFAULT`
  PWTextColor=`DEFAULT`
  PWBackground=`DEFAULT,DEFAULT`
  PWConfig=0

  PW001=`013,199,040,012,PUSHBUTTON,DEFAULT,"Set Password",1,90,32,DEFAULT,DEFAULT,DEFAULT`
  PW002=`063,199,036,012,PUSHBUTTON,DEFAULT,"Exit",0,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW003=`009,021,142,012,EDITBOX,directory,DEFAULT,DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW004=`159,021,036,012,PUSHBUTTON,DEFAULT,"Browse",2,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW005=`009,049,142,092,ITEMBOX,files,DEFAULT,DEFAULT,5,256,DEFAULT,DEFAULT,DEFAULT`
  PW006=`009,009,026,010,STATICTEXT,DEFAULT,"Directory",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW007=`009,037,020,010,STATICTEXT,DEFAULT,"File List",DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW008=`009,175,022,008,STATICTEXT,DEFAULT,"Status",DEFAULT,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW009=`009,185,182,010,VARYTEXT,status,DEFAULT,DEFAULT,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW010=`009,147,044,008,STATICTEXT,DEFAULT,"New Password",DEFAULT,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
  PW011=`009,157,072,012,EDITBOX,password,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

  ButtonPushed=Dialog("PW")

  Exit


Article ID:   W16149
File Created: 2004:03:30:15:43:00
Last Updated: 2004:03:30:15:43:00