Consulting

Results 1 to 17 of 17

Thread: Convert multiple non saved word documents into pdf

  1. #1
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location

    Convert multiple non saved word documents into pdf

    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!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    If the Word documents aren't saved, where are their names coming from?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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?
    Last edited by macropod; 02-19-2019 at 01:42 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by frio27 View Post
    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?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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"

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Last edited by macropod; 02-21-2019 at 12:21 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by frio27 View Post
    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...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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"

  12. #12
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Code fixed. Try it now.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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"

  14. #14
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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"
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  15. #15
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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.

  16. #16
    VBAX Regular
    Joined
    Feb 2019
    Posts
    16
    Location
    Quote Originally Posted by macropod View Post
    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

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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