Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post

    Post Code is not working on protected word doc

    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", "File Conversion", "TXT"))
                Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "pdf")
            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

  2. #2
    Silver Lounger
    Join Date
    Mar 2014
    Location
    Forever West
    Posts
    2,072
    Thanks
    0
    Thanked 259 Times in 248 Posts
    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.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post
    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

  4. #4
    Silver Lounger
    Join Date
    Mar 2014
    Location
    Forever West
    Posts
    2,072
    Thanks
    0
    Thanked 259 Times in 248 Posts
    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.

  5. #5
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    Just to be certain here--do you have access to the password?
    Kim

  6. #6
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    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 link at msoft.

    Cheers
    G
    Geof
    Last edited by geofrichardson; 2015-07-30 at 15:49.

  7. #7
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post

    Post

    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

  8. #8
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    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

  9. #9
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post

    Post

    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

  10. #10
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts
    Geo,

    Try changing Documents.Close to ActiveDocument.Close

    Does that help?

  11. The Following User Says Thank You to kmurdock For This Useful Post:

    geofrichardson (2015-08-04)

  12. #11
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post
    Hi Kmurdoc
    Even using it is making the macro hang.
    Regards,
    JD

  13. #12
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    Hi Jaggi

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

  14. #13
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    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:\Data\test\jaggi\"
    '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
    Last edited by geofrichardson; 2015-08-03 at 15:42.

  15. #14
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    Thanks, I missed that. Dohl.

  16. #15
    2 Star Lounger
    Join Date
    Feb 2015
    Posts
    128
    Thanks
    19
    Thanked 1 Time in 1 Post
    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

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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