• Code is not working on protected word doc

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Code is not working on protected word doc

    Author
    Topic
    #501162

    Hi Experts
    I have the below code which convert the word documents into PDF. It works fine for the documents which are not password protected. I recently received list of document which are password protected and the below code fails to convert the word doc into PDF. Could you please guide me with the way to deal with this issue.

    Code:
    Sub wordtopdf()
    ‘with export to PDF in Word 2007
        Dim fs As Object
        Dim oFolder As Object
        Dim tFolder As Object
        Dim oFile As Object
        Dim strDocName As String
        Dim intPos As Integer
        Dim locFolder As String
        Dim fileType As String
        On Error Resume Next
        locFolder = InputBox(“Enter the folder path to DOCs”, “File Conversion”, “C:myDocs”)
        Select Case Application.Version
            Case Is = 12
                Do
                    fileType = UCase(InputBox(“Change DOC to TXT, RTF, HTML or PDF”, “File Conversion”, “TXT”))
                Loop Until (fileType = “TXT” Or fileType = “RTF” Or fileType = “HTML” Or fileType = “PDF” Or fileType = “pdf”)
        End Select
        Application.ScreenUpdating = False
        Set fs = CreateObject(“Scripting.FileSystemObject”)
        Set oFolder = fs.GetFolder(locFolder)
        ‘Set tFolder = fs.CreateFolder(locFolder & “Converted”)
        ‘Set tFolder = fs.GetFolder(locFolder & “Converted”)
        For Each oFile In oFolder.Files
            Dim d As Document
            Set d = Application.Documents.Open(oFile.Path)
            strDocName = ActiveDocument.Name
            intPos = InStrRev(strDocName, “.”)
            strDocName = Left(strDocName, intPos – 1)
           ‘ ChangeFileOpenDirectory tFolder
            Select Case fileType
            Case Is = “TXT”
                strDocName = strDocName & “.txt”
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
            Case Is = “RTF”
                strDocName = strDocName & “.rtf”
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
            Case Is = “HTML”
                strDocName = strDocName & “.html”
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
            Case Is = “PDF”
                strDocName = strDocName & “.pdf”
                ‘ *** Word 2007 users – remove the apostrophe at the start of the next line ***
                ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
                
            End Select
            d.Saved = True
            d.Close
            
            ChangeFileOpenDirectory oFolder
        Next oFile
        ‘ActiveDocument.Saved = True
        Application.ScreenUpdating = True
        
       MsgBox (“Documents are successfully Converted!”)
    End Sub
    
    

    Regards,
    JD

    Viewing 12 reply threads
    Author
    Replies
    • #1517786

      I’ve never been able to get around secured documents that someone else passworded, they must have had a reason. Could to it the ‘old school’ way, Select all the text in the document, Copy then Paste into a new document then convert that. I know everything can be done using features of programs but then there’s always a stumbling block.

      Before you wonder "Am I doing things right," ask "Am I doing the right things?"
    • #1518110

      Hi Experts

      I tried the old procedure as well of copying the data from protected document to new document and then run the macro it still getting hang and doesn’t convert the word file into PDF

      Regards,
      JD

    • #1518122

      I just opened a couple of .doc files and used the Save As feature in Word 14 [Office 2010] to save both as .pdf files which then opened in Adobe Reader, didn’t see any issue but they were simple text. It seems the issue is back to the files being password-protected.

      Before you wonder "Am I doing things right," ask "Am I doing the right things?"
    • #1518136

      Just to be certain here–do you have access to the password?
      Kim :bananas:

    • #1518155

      Hi

      The following line in you code means that the routine has to open the document. Password required !!

      Code:
      Set d = Application.Documents.Open(oFile.Path)
      

      Do all the documents have the same password? A forlorn hope.

      I guess you dont have access to the hard copies. If you did then you could scan them to pdf.

      Nice to know the security works !

      You can include the password in the code that opens a document.
      It is not recommended to hard code this in a routine however.

      see this linkat msoft.

      Cheers
      G
      Geof

    • #1519106

      Hi Experts
      Thanks for valuable input on this thread.
      @Kmurdock – I don’t have the password with me. These documents are system generated.
      @Geofrichardson – I tried to replace the above code with
      Documents.Open (oFile.Path), ReadOnly:=True
      It converts the document to PDF, but when I am trying to add the code to close the document.
      Documents.close
      It won’t convert the document into PDF. I think we care close to it, but not finding proper way to get it workable.

      Code:
         For Each oFile In oFolder.Files
              ‘Dim d As Document
              Application.Documents.Open (oFile.Path), ReadOnly:=True
              ‘Documents.Open (oFile.Path), ReadOnly:=True
              strDocName = ActiveDocument.Name
              intPos = InStrRev(strDocName, “.”)
              strDocName = Left(strDocName, intPos – 1)
              ChangeFileOpenDirectory oFolder
            
              fileType = “PDF”
                  strDocName = strDocName & “.pdf”
      
                  ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
                  
             
              Documents.Close
              ChangeFileOpenDirectory oFolder
          Next oFile
      
      • #1519110

        Hi

        I will admit to be confounded. I didn’t give you a hope in Hades of getting close.

        It converts the document to PDF, but when I am trying to add the code to close the document.

        Do you actually see a .pdf created in addition to the .dot(?)
        If so all bets are off. What happens with

        documents.close saveChanges:=wdDoNotSaveChanges

        See this link at msoft about the Close method

        Cheers
        G

    • #1519113

      Hi Geo
      When I am trying to apply the close code with the below loop the code gets hanged and doesn’t display any result. When I comment the close code it run and convert my word doc into pdf successfully, but here to my macro word doc gets hanged after giving me the end result is it because the password protected word doc is opened in the back. I tested the code on 2 document only one got converted and then the code gets hanged.

      Code:
        For Each oFile In oFolder.Files
              Dim d As Document
              ‘Application.Documents.Open (oFile.Path), ReadOnly:=True
              Documents.Open (oFile.Path), ReadOnly:=True
              strDocName = ActiveDocument.Name
              intPos = InStrRev(strDocName, “.”)
              strDocName = Left(strDocName, intPos – 1)
              ChangeFileOpenDirectory oFolder
            
              fileType = “PDF”
                  strDocName = strDocName & “.pdf”
      
                  ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
                  
             
              ‘Documents.Close saveChanges:=wdDoNotSaveChanges
              ChangeFileOpenDirectory oFolder
          Next oFile

      Regards,
      JD

    • #1519192

      Geo,

      Try changing Documents.Close to ActiveDocument.Close

      Does that help? :bananas:

    • #1519198

      Hi Kmurdoc
      Even using it is making the macro hang.
      Regards,
      JD

    • #1519217

      Hi Jaggi

      Please comment out on error resume next.
      Run the macro again to see the error dialog.
      G

    • #1519228

      Hello Jaggi

      I used the following stripped down version of your code. Very simple.
      I have hard coded a path for simplicity. Converted docs are saved to the same location.
      If I have two word documents (word 2010) and one document is password protected the macro pauses and requests a password.

      If there is a pdf in the folder it seems to hang before failing.

      Always disable resume next for debugging purposes.
      Pay attention to the use of document vs documents and pay particular attention to the set commands in your code.

      G

      Code:
      Sub jaggiTest()
      Dim fs As Object
      Dim oFolder As Object
      Dim tFolder As Object
      Dim oFile As File
      Dim strDocName As String
      Dim intPos As Integer
      Dim locFolder As String
      'On Error Resume Next
      
      locFolder = "D:Datatestjaggi"
      'Application.ScreenUpdating = False
          Set fs = CreateObject("Scripting.FileSystemObject")
          Set oFolder = fs.GetFolder(locFolder)
          For Each oFile In oFolder.Files
             Application.Documents.Open (oFile.Path)
              strDocName = ActiveDocument.Name
              intPos = InStrRev(strDocName, ".")
              strDocName = Left(strDocName, intPos - 1)
              ActiveDocument.ExportAsFixedFormat OutputFileName:=locFolder & strDocName, ExportFormat:=wdExportFormatPDF
                  
              ActiveDocument.Close
          Next oFile
          'Application.ScreenUpdating = True
          
         MsgBox ("Documents are successfully Converted!")
      End Sub
      
    • #1519362

      Hi Geo
      This is still not working. The issue with the code is that it is getting hanged no error message occurs. I do get the loading sign and the macro doc file gets into not responding mode.
      Regards,
      JD

      • #1519456

        Hi Jaggi

        We have to get to a point that we know is working.

        But first : – Are you 100% confident there is not a dialog awaiting your response hiding in a background window? Nothing new on the task bar?

        Please place the following minimalist code into a new module in a new macro enabled document.

        Code:
        Sub jaggiTest2()
        Dim fs As Object
        Dim oFolder As Object
        Dim oFile As File
        Dim locFolder As String
        Dim i as integer
        
        locFolder = "D:Datatestjaggi"
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set oFolder = fs.GetFolder(locFolder)
            For Each oFile In oFolder.Files
                Debug.Print oFile.Path
                i = i + 1
            Next oFile
           MsgBox (i & " Documents Counted")
        End Sub
        

        Set a reference to Microsoft Scripting Runtime. This is found under the Tools menu in the VB Editor, Tools>>references.
        You will have to scroll through the list to find it. The attached image will not match your screen exactly.
        41567-MSWorReferencesDialog

        Create the folder D:datatestjaggi or amend the line that sets the locFolder variable. If you amend this line include the final trailing “”

        Copy 2 or 3 files into the locFolder – use .doc? files. None password protected.

        Run the code.
        Hopefully you will see a list of file names printed to the immediate window and a msgbox informing you of the count.

        If this is working then

        Replace the loop with the following excerpt to test the conversion.

        Code:
            For Each oFile In oFolder.Files
               Application.Documents.Open (oFile.Path)
                strDocName = ActiveDocument.Name
                intPos = InStrRev(strDocName, ".")
                strDocName = Left(strDocName, intPos - 1)
                ActiveDocument.ExportAsFixedFormat OutputFileName:=locFolder & strDocName, ExportFormat:=wdExportFormatPDF
                    
                ActiveDocument.Close
            Next oFile
        

        IF this is working then delete the pdf documents and place a password protected .doc(?) file in the test folder and rerun the code.

        I predict that you will be prompted for a password.
        If you do not supply the password MS Word should display an alert, “Command Failed”. You should be able to choose “Debug” and the line of code attempting to open the document will be highlighted.

        At this point you should have run full circle. You have proved that you need the password.

        Testing Highlights

        I have found that trying to convert .xps files with this routine causes the macro to crash.
        On the occasion when a .pdf was present in the test folder the system tried to open my pdf management package. It appeared to hang. I could close the pdfPro and then the macro stopped and displayed the error notifications. Of course it was trying to open the pdf doc.

        Geof

    • #1519651

      Hi Geo
      I tested the first 2 loops and they are working perfectly fine, but when I tried to run the 2 code on password protected docx file is throws following attached error msg on the following line highlighted in the attached files as well.
      Regards,
      JD

    Viewing 12 reply threads
    Reply To: Code is not working on protected word doc

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: