Consulting

Results 1 to 11 of 11

Thread: VBA to convert Password protected Word Doc into PDF.

  1. #1
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post VBA to convert Password protected Word Doc into PDF.

    Hi Experts,
    I am looking for a fix on this query from last couple of months and really disappointed to not find a concrete solution to my query. I have the word documents which are password protected. I don’t know the password and would like to convert these protected files into PDF using VBA code. I have code which rename the doc file which the content within it and convert it into PDF. This is working fine for doc without password. Currently for password doc, I am following manual process of opening these file one on one and print the doc as PDF and convert them in to PDF and save. I just want to know is this a dead lock and there is no way to deal with this situation. I already created the thread and cross posting it here - http://windowssecrets.com/forums/sho...ected-word-doc
    I am able to Rename the file, but not able to convert it into PDF.
    Regards,
    JD

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    VBA Express does not help with cracking or bypassing passwords.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Sam
    Thanks for your input on this thread. So it means without knowing the password we cannot convert password protected word document into PDF.

    Regards
    JD

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    @Jagdev --

    Q1 -- which password: 1) the VBA project password, or 2) the document password (like for fill-in forms, etc.)?

    Q2 -- Is this a one time thing or ongoing?


    I created a simple docm, password protected the VBA project, and also the document. I can still save it as a PDF (using 2010) without knowing any of the passwords or cracking the document.
    In the docm attachment, the pw = 'password' for the VBA project, and for the document

    So you might write a macro in a separate document (or add-in) that opens the PW protected document, saves it as PDF, closes it, and then gets the next one. That way you can get the PDF converted file without knowing or cracking the PW (As SamT says, 'We don't do that here' [even if it's your own document and you forgot your password])


    I am following manual process of opening these file one on one and print the doc as PDF and convert them in to PDF and save.
    This would be (if it works) just automating your manual process
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Paul,
    Thanks for your feedback.
    I have checked the protected documents and there is no VBA code added in them. I believe it is the document which is password protected. The documents I am working on are system generated and contain data in Table structure. When I am trying to run the VBA which I have to rename it and convert them into VBA. The VBA gets into hanging mode and keeps on loading for protected documents only. Is there any easy way to deal with it. I do not want to crack the password just want this documents to be converted into PDF.

    This is a daily activity.

    @Paul - Sorry to inform you but I am not able to understand the instruction provided in the macro document in the above thread.

    Please let me know if I am unclear here.
    Regards,
    JD

  6. #6
    Paul's document sample is protected against editing and does not have any restriction on saving. If the document was password protected against opening, then you wouldn't be able to open it without the password, so you couldn't save it. How have you 'checked the protected documents'?

    If the documents do not contain sensitive information, can you post one to the forum so we can see what it is you are dealing with. However if they are protected against opening we would not be able to assist with that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location

    Post

    Hi Gmayor,
    Thanks for your input on this thread. I am able to open the protected file, but not able to edit it. I have the code which renames the word doc and the code is working fine on the protected document as well. When the code tries to convert the protected doc from word to PDF I see that the file is open and the code gets hanged. I am also able to open the protected document without any issue. Sorry I won’t be able to share the document because of its confidentiality. I took the screenshot of the edited section and it is attached with the mail.
    Also, find the code I am using to rename and converting the word file to PDF and also deleting the word doc after converting them to PDF.

    The line of code where the macro gets hanged is this one

    ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF

    Sub RenameDocumentsTreaty()
    Application.ScreenUpdating = False
    Dim strFldr As String, strDocNm As String, strFile As String, strNewNm As String, wdDoc As Document
    Dim FSO As Object, objFile As Object
    Dim fs As Object
        Dim oFolder As Object
        Dim oFile As Object
        Dim strDocName As String
        Dim intPos As Integer
        Dim fileType As String
        Dim MyFile As String
        On Error Resume Next
        fileType = "PDF"
        Set fs = CreateObject("Scripting.FileSystemObject")
    strDocNm = ActiveDocument.FullName
    strFldr = InputBox("Please add folder link for treaty doc")
    If strFldr = "" Then Exit Sub
    Set FSO = CreateObject("Scripting.FileSystemObject")
    strFldr = strFldr & "\"
    strFile = Dir(strFldr & "*.doc", vbNormal)
    While strFile <> ""
      If strFldr & strFile <> strDocNm Then
        Set wdDoc = Documents.Open(FileName:=strFldr & strFile, AddToRecentFiles:=False, Visible:=False)
        With wdDoc
          strNewNm = .SelectContentControlsByTitle("DC\Name")(1).Range.Text _
              & "_" & .SelectContentControlsByTitle("DC\BrokerReference1")(1).Range.Text _
              & .SelectContentControlsByTitle("DC\BrokerReference2")(1).Range.Text _
              & "_" & .SelectContentControlsByTitle("DC\NettAmt")(1).Range.Text _
              & "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
          'strNewNm = strNewNm & "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
          .Close SaveChanges:=False
        End With
        If FSO.FileExists(strFldr & strNewNm) Then
          ActiveDocument.Range.InsertAfter "Unable to create:" & Chr(11) & strFldr & strNewNm & Chr(11) & "File Exists" & vbCr
        Else
          Set objFile = FSO.GetFile(strFldr & strFile)
          objFile.Name = strNewNm
        End If
      End If
      strFile = Dir()
    Wend
    Set oFolder = fs.GetFolder(strFldr)
      
        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 oFolder
          
            fileType = "PDF"
                strDocName = strDocName & ".pdf"
                ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
                d.Saved = True
           
            d.Close
            ChangeFileOpenDirectory oFolder
        Next oFile
            
     MyFile = Dir$(strFldr & "\*.doc*")
     Do While MyFile <> ""
         KillProperly strFldr & "\" & MyFile
        MyFile = Dir$(strFldr & "\*.docx")
     Loop
        Set wdDoc = Nothing: Set objFile = Nothing: Set FSO = Nothing
    Application.ScreenUpdating = True
    End Sub
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function
    Public Sub KillProperly(Killfile As String)
         If Len(Dir$(Killfile)) > 0 Then
             SetAttr Killfile, vbNormal
             Kill Killfile
         End If
     End Sub


    Regards,
    JD
    Attached Images Attached Images
    Last edited by SamT; 09-29-2015 at 03:13 PM. Reason: repaired CODE Tags

  8. #8
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi All
    I finally understand what exactly you all mean. I created a sample word doc and restrict its access and later tried the code it works and the file got converted to PDF without any issue. I am surprised with why my system generated protected word doc is not getting converted.
    Regards,
    JD

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It may not make a difference but try this. Added & ".pdf"

    ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName & ".pdf", ExportFormat:=wdExportFormatPDF
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Sam,
    Nope it didn’t make any difference to it.
    Regards,
    JD

  11. #11
    VBAX Contributor
    Joined
    Nov 2014
    Posts
    121
    Location
    Hi Experts,
    I performed few tests on my lock file and totally confused with the outcome. To rename this file I pick 3 table cell as per the below code. What I did I copy all the three texts from a locked file and paste them on a new separate word document files. I tweaked the code and try to run the below set of codes and fortunately the test yield positive result and the individual document gets renamed and converted to PDF file successfully. That ends my excitement went I perform the test again on my system generated file the same old error of code getting hanged occurs again.
    Is there something wrong with the system files? I am again turned to the point I started with.
    Regards,
    JD

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
  •