PDA

View Full Version : VBA to convert Password protected Word Doc into PDF.



Jagdev
09-22-2015, 06:56 AM
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/showthread.php/170290-Code-is-not-working-on-protected-word-doc
I am able to Rename the file, but not able to convert it into PDF.
Regards,
JD

SamT
09-22-2015, 01:40 PM
VBA Express does not help with cracking or bypassing passwords.

Jagdev
09-22-2015, 07:56 PM
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

Paul_Hossler
09-23-2015, 06:38 AM
@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

Jagdev
09-24-2015, 03:49 AM
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

gmayor
09-26-2015, 09:17 PM
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.

Jagdev
09-29-2015, 04:56 AM
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

Jagdev
09-29-2015, 09:13 AM
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.:banghead:
Regards,
JD

SamT
09-29-2015, 03:17 PM
It may not make a difference but try this. Added & ".pdf"


ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName & ".pdf", ExportFormat:=wdExportFormatPDF

Jagdev
09-30-2015, 09:56 AM
Hi Sam,
Nope it didn’t make any difference to it.
Regards,
JD

Jagdev
10-15-2015, 03:55 AM
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.:banghead:
Regards,
JD