PDA

View Full Version : Convert multiple non saved word documents into pdf



frio27
02-18-2019, 03:33 AM
Hello.

I have made this macro to convert multiple word documents into pdf.
The word documents are not saved.

Here the code:

Sub Certificadospdf()
Dim archivo As Document


For Each archivo In Documents
Dim strName As String


ActiveDocument.Save
strName = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - 4)
strName = strName & "pdf"

ActiveDocument.ExportAsFixedFormat OutputFileName:=strName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, to:=99, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=True
ActiveDocument.Close (False)


Next archivo

End Sub






The problem is that this macro save the first document in word and pdf format (that is correct) but didnt close this document. It close the next without saving it.
So for example, if I have 10 opened word documents, this macro save and convert only the documents: 1,3,5,7 and 9. And it close the rest of the documents without converting them.

Thanks!

macropod
02-18-2019, 06:28 PM
If the Word documents aren't saved, where are their names coming from?

frio27
02-19-2019, 12:11 AM
If the Word documents aren't saved, where are their names coming from?
They come from the first line of the word, I want to happen this because the first lines of my words are the serial number and I want to save with this name.

macropod
02-19-2019, 01:31 AM
OK, but nothing in your code indicates any attempt to get that line; all it's got is ActiveDocument.Save, which will give names like 'Document1', Document2', etc.

It's also not apparent where you're getting the path from - ActiveDocument.Save will simply save the unsaved document to Word's default save path. An unsaved document has no path, however. Hence ActiveDocument.FullName after ActiveDocument.Save will merely return the default save path plus 'Document1', Document2', etc. So, what should the save path be?

frio27
02-19-2019, 02:01 AM
OK, but nothing in your code indicates any attempt to get that line; all it's got is ActiveDocument.Save, which will give names like 'Document1', Document2', etc.

It's also not apparent where you're getting the path from - ActiveDocument.Save will simply save the unsaved document to Word's default save path. An unsaved document has no path, however. Hence ActiveDocument.FullName after ActiveDocument.Save will merely return the default save path plus 'Document1', Document2', etc. So, what should the save path be?

If you save a document for first time it takes the name from the first line of the document, at least in Word 2010.
When i run the macro it ask me where i want to save the document, so I just have to press enter once for each document.

My problem is that the macro doesnt save all documents. Just saves one and closes the next, so it just works for the half of the documents.

macropod
02-19-2019, 04:38 AM
If you save a document for first time it takes the name from the first line of the document, at least in Word 2010.
Strictly speaking, that's an over-simplification. What gets used for the default name in that scenario may or may not be a whole line.


Regardless, do you want a document saved as a result of this process, or just the PDF? And where do you want it saved?

frio27
02-19-2019, 04:48 AM
Strictly speaking, that's an over-simplification. What gets used for the default name in that scenario may or may not be a whole line.


Regardless, do you want a document saved as a result of this process, or just the PDF? And where do you want it saved?


Yes it is a simplification. It really takes the carachters until the first punctuation sign.

I just need the PDF saved with the name taken for the first characters until the first punctuation sign.
Its not a problem where i want to save them.
For example it can be in a specific folder on the deskop: "C:\Users\Desktop\certificates"

macropod
02-19-2019, 12:56 PM
In that case, try:

Sub CertificadosPDF()
Application.ScreenUpdating = False
Dim strPath As String, strName As String
strPath = "C:\Users\" & Environ("Username") & "\Desktop\Certificates\"
Do While Documents.Count > 0
With Documents(1)
strName = Split(.Range.Paragraphs.First.Range.Text, vbCr)(0) & ".pdf"
.SaveAs2 FileName:=strPath & strName, FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
Loop
Application.ScreenUpdating = True
End Sub

frio27
02-20-2019, 08:58 AM
In that case, try:

Sub CertificadosPDF()
Application.ScreenUpdating = False
Dim strPath As String, strName As String
strPath = "C:\Users\" & Environ("Username") & "\Desktop\Certificates\"
Do While Documents.Count > 0
With Documents(1)
strName = Split(.Range.Paragraphs.First.Text, vbCr)(0) & ".pdf"
.SaveAs2 FileName:=strPath & strName, FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
Loop
Application.ScreenUpdating = True
End Sub

Thanks, you have helpme with these macro. But it gives me some error.

I have made some changes and i dont know why it doesnt work, my actually macro:


Sub CertificadosPDForiginalmodificado()
Application.ScreenUpdating = False
Dim strPath As String, strName As String
strPath = "C:\Users\Desktop\certificates\"
Do While Documents.Count > 0
With Documents(1)

strName = ActiveDocument.Paragraphs(3).Range.Text
ActiveDocument.SaveAs2 FileName:=strPath & strName & "pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
Loop
Application.ScreenUpdating = True
End Sub


Also I have this macro that works but I have to click on "save" for every document when the saving window appears.



Sub CertificadosPDFguardarunoporuno()
Application.ScreenUpdating = False
Dim strPath As String, strName As String
strPath = "C:\Users\Desktop\certificates\"
Do While Documents.Count > 0
With Documents(1)
ActiveDocument.Save
strName = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - 4)
strName = strName & "pdf"

ActiveDocument.ExportAsFixedFormat OutputFileName:=strName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=99, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=True
ActiveDocument.Close (False)
End With
Loop
Application.ScreenUpdating = True
End Sub

macropod
02-20-2019, 01:39 PM
Thanks, you have helpme with these macro. But it gives me some error.
Well, if you don't care to say what the error is, I'm not going to play a guessing game trying to fix it...

frio27
02-21-2019, 12:18 AM
Well, if you don't care to say what the error is, I'm not going to play a guessing game trying to fix it...

Sorry, Its my first time in a forum.

The error appears is " Compile error, method or data member not found"

And it remarks the part ".Text"

macropod
02-21-2019, 12:22 AM
Code fixed. Try it now.

frio27
02-21-2019, 12:34 AM
Code fixed. Try it now.

It seems that the error disapears but now appears another.

In this case it appears "Run time error 4198"

And remark the line ".SaveAs2 FileName:=strPath & strName, FileFormat:=wdFormatPDF, AddToRecentFiles:=False"

macropod
02-21-2019, 12:43 AM
Do you have a folder named 'Certificates' on your Desktop? Note that the name is case-sensitive.

What do you get if you insert:
MsgBox strName
after:
strName = Split(.Range.Paragraphs.First.Range.Text, vbCr)(0) & ".pdf"

frio27
02-21-2019, 12:53 AM
Do you have a folder named 'Certificates' on your Desktop? Note that the name is case-sensitive.

What do you get if you insert:
MsgBox strName
after:
strName = Split(.Range.Paragraphs.First.Range.Text, vbCr)(0) & ".pdf"

Yes I have this folder.
For example if I put this code:


Sub CertificadosPDForiginal()
Application.ScreenUpdating = False
Dim strPath As String, strName As String
strPath = "C:\Users\jadiaz\Desktop\certificates\"
Do While Documents.Count > 0
With Documents(1)
strName = Split(.Range.Paragraphs.First.Range.Text, vbCr)(0) & ".pdf"
ActiveDocument.SaveAs2 FileName:=strPath & "hello hello", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
Loop
Application.ScreenUpdating = True
End Sub

(note that I have change the strName for "hello hello")
It saves the files on the folder "certificates".

If i do what you propose with the msgbox appears the name that i want for the files.

frio27
02-21-2019, 07:43 AM
Do you have a folder named 'Certificates' on your Desktop? Note that the name is case-sensitive.

What do you get if you insert:
MsgBox strName
after:
strName = Split(.Range.Paragraphs.First.Range.Text, vbCr)(0) & ".pdf"

It finally works

Thank you very much.

If you ever come to Spain I invite you to have dinner.

Thanks :)

macropod
02-21-2019, 01:50 PM
The fact the code works now that you've changed strPath only goes to show the folder name on your desktop is certificates, not Certificates.