Results 1 to 21 of 21

Thread: Problem with Password protected Word files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Problem with Password protected Word files

    When my code encounters a password protected Word file in the "For Each" loop, it prompts the user for the password, then suddenly the hidden Word app becomes visible ( even though I stated "oWApp.ScreenUpdating = False" ) and the user is seeing the opening & closing of the rest of each document inside the For Each loop.

    When I encounter a password protected file, I need to log the Word filepath and go on to the Next file, without exiting the loop. I'm having problems with this line: Set oWDoc = oWApp.Documents(abc)

    When I place the problem line before the Open statement, I get "bad file name" .
    Variable abc contains a full server and path, like: \\SERVER\User\My Documents\FileName.doc

    When I place the problem line and the HasPassword test, after the Open statement, I get the original problem - a password prompt and the Word app becomes visible while still processing files.

    Code:
           Set oWApp = GetObject(, "Word.Application")
            If ERR_APP_NOT_RUNNING Then
                Set oWApp = CreateObject("Word.Application")
            End If
                oWApp.ScreenUpdating = False
    
                For Each abc In colStoreWFiles
    
                Set oWDoc = oWApp.Documents(abc)  'PROBLEM LINE
                If oWDoc.HasPassword = True Then
                    Print #LOG, "message"
                Else
                    Set oWDoc = oWApp.Documents.Open(abc, , False)
                ....etc....
                End If
    Any information is appreciated.
    Last edited by CyberJar; Feb 21st, 2008 at 12:57 PM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    Quote Originally Posted by CyberJar
    When my code encounters a password protected Word file in the "For Each" loop, it prompts the user for the password, then suddenly the hidden Word app becomes visible ( even though I stated "oWApp.ScreenUpdating = False" ) and the user is seeing the opening & closing of the rest of each document inside the For Each loop.

    When I encounter a password protected file, I need to log the Word filepath and go on to the Next file, without exiting the loop. I'm having problems with this line: Set oWDoc = oWApp.Documents(abc)

    When I place the problem line before the Open statement, I get "bad file name" .
    Variable abc contains a full server and path, like: \\SERVER\User\My Documents\FileName.doc

    When I place the problem line and the HasPassword test, after the Open statement, I get the original problem - a password prompt and the Word app becomes visible while still processing files.

    Code:
           Set oWApp = GetObject(, "Word.Application")
            If ERR_APP_NOT_RUNNING Then
                Set oWApp = CreateObject("Word.Application")
            End If
                oWApp.ScreenUpdating = False
    
                For Each abc In colStoreWFiles
    
                Set oWDoc = oWApp.Documents(abc)  'PROBLEM LINE
                If oWDoc.HasPassword = True Then
                    Print #LOG, "message"
                Else
                    Set oWDoc = oWApp.Documents.Open(abc, , False)
                ....etc....
                End If
    Any information is appreciated.
    Try

    vb Code:
    1. oWApp.Visible=False
    2. oWApp.ScreenUpdating = False

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    Hi,

    I did try .Visible = False and .DisplayAlerts= False too, but Word still came up with thte prompt for the password.

  4. #4
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Problem with Password protected Word files

    He's already using ScreenUpdating and Visible is already false.

    Try out DisplayAlerts = False. I tried to reproduce the behavior, but I didn't get a prompt.

  5. #5
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Problem with Password protected Word files

    I think I see your problem, you're attaching to the running copy of Word instead of creating a new one every time. That should be what is causing it. I always create a new process. Do that, and your problem should go away.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    where are you calling this code from?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    When I run the program, I do not have a running version of Word. Word is closed. Is that what you mean?

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    Quote Originally Posted by CyberJar
    When I run the program, I do not have a running version of Word. Word is closed. Is that what you mean?
    Where are you running this program from?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Problem with Password protected Word files

    Yea, I was referring to making sure that createobject was being used instead of getobject. If that is the case, then I'm stumped. I can't reproduce the behavior here. I'm using Office 2003 and early binding. I also don't have to tell word to not update the screen or display alerts. It spawns not visible and runs like expected...

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    From my C:

    It executes on Word files locate in My Documents

  11. #11
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Problem with Password protected Word files

    PS /wave Koolsid

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    Quote Originally Posted by CyberJar
    From my C:

    It executes on Word files locate in My Documents
    No I mean are you calling this piece of code from Excel, VB6, or any other office application...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    Oops, sorry - it's a vb6 late binding app which works on Word and Excel files.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    I read somewhere to add a bogus password to the Open and then check for err.number 5408 - password is incorrect. The unprotected files ignore it and then just check for 5408.

    Set oWDoc = oWApp.Documents.Open(abc, , False, , "xyz")

    But somewhere else said that in Word 2003 this doesn't work - you get err.number 4198 - command failed, for all files and nothing gets processed.

    I'm using 2000, is this true when using 2003?

    Thanks

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    Quote Originally Posted by CyberJar
    Oops, sorry - it's a vb6 late binding app which works on Word and Excel files.
    Oh ok

    Unfortunately I don't have vb6 in the office. I cannot even test it

    Would it be possible for you to post the inital part of the code as well. I am just being selfish here so that I don't have to type the entire code. i can simply copy paste your code and then test it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    This is what works as of now:
    Code:
            Set oWApp = GetObject(, "Word.Application")
            If ERR_APP_NOT_RUNNING Then
                Set oWApp = CreateObject("Word.Application")
            End If
    
                oWApp.ScreenUpdating = False
    
                For Each abc In colStoreWFiles
    
                    Set oWDoc = oWApp.Documents.Open(abc, , False, , "xyz") 
                    oWDoc.Activate
                    ... process ...
    
    eHandler:
       If Err.Number = 5408 Then
           Print #LOG, msg
        End If
        Resume Next 'THIS IS WRONG, I NEED TO GET THE NEXT FILE - NOT CONTINUE. HOW?
    
        If Err.Number <> ERR_APP_NOT_RUNNING Then
            Print #LOG, msg
        End If
        Resume Next
    Thanks

  17. #17
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Problem with Password protected Word files

    great, let me check this out...

    edit: Ok I checked the code out. It is one way of approching your problem. The only hitch with this code is that if the password for the file is "xyz" then it will not give you the desired result. the other way is to replace "xyz" with a lenghty string. something like "aadadfsdgfdhghgfjfhjhjhdgjghgfkgjk"

    The other thing is that this

    Set oWDoc = oWApp.Documents(abc)

    is an incorrect way of setting the document to oWdoc. You will have to use oWApp.Documents.open() to set it. And there is no way that I could find wherein you can set it without making the application visible... Maybe I am not aware if there is any other way...

    Maybe I would have done the same thing as you have done in the above post....
    Last edited by Siddharth Rout; Feb 22nd, 2008 at 05:37 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    Thanks for checking it out.

    Wow, what a bind! - but I have a few more tricks up my sleeve - I'll post any positive results here for others.

    CJ

  19. #19
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Problem with Password protected Word files

    Hey Cyber,

    Can you confirm if you get the password prompt if you run the code in Office VBA? I was trying to re-create the behavior using VBA, and it didn't prompt for a password. That could lead to a solution if it doesn't. It would also prove that Word pays attention to where it's being automated from.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    In office 2002, with a bogus password set in the Open statement, instead of returning "Incorrect Password", all files are returning 4198, "command failed" so there is no way to segregate Password protected files from Non-protected files.

    My app runs against thousands of files, users cannot sit there entering password after password -

    Does anyone know how to find out if a file is password protected before the open statement? maybe with an API?

    Thanks

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Re: Problem with Password protected Word files

    This is the way to do it:
    Code:
                   Set oWDoc = oWApp.Documents.Open(fileName, , False, , "nonsense")
                    If skipDoc Then GoTo nextDoc
                    'process doc
    nextDoc:
                Set oWDoc = Nothing
                skipDoc = False
                Next 'loop get next doc
    Then do checks in your error routine
    Code:
    errRountine:
             Select Case Err.Number
                    Case 5408 'wrong password
                        Print #LOG, whatever
                        Err.Clear
                        skipDoc = True
                        resume next
    hope this helps.
    CJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width