none
Extract All Embedded Files in All Folders and Save Each? Copy/Paste from PDF to Word? RRS feed

  • Question

  • I have most of what I need here, but I’m missing 2 important pieces. 

     

    #1)  I want to copy/paste from all PDF files in a folder and paste the copied data into a single Word file.  It works fine if I have ONLY Word docs in my folder.  When I have PDF files and Word files, the contents of the Word files are copied in fine, but the contents of the PDF files seem to come in as Chinese, and there is no Chinese in the PDF, so I have no idea where that’s coming from.

     

    #2)  I want to extract all embedded files (in all my Word files) and save the extracted/opened file into the folder.  Some embedded files are PDFs and some are Excel files.

     

     

    Here the code that I’m working with now.

     

     

    Sub Foo()
    Dim i As Long
    Dim MyName As String, MyPath As String
    Application.ScreenUpdating = False
    Documents.Add
    
    MyPath = "C:\Users\001\Desktop\Test\" ' <= change this as necessary
    MyName = Dir$(MyPath & "*.*") ' not *.* if you just want doc files
    On Error Resume Next
    Do While MyName <> ""
    If InStr(MyName, "~") = 0 Then
    Selection.InsertFile _
    FileName:="""" & MyPath & MyName & """", _
    ConfirmConversions:=False, Link:=False, _
    Attachment:=False
    
     
    Dim Myshape As InlineShape
    Dim IndexCount As Integer
    IndexCount = 1
    For Each Myshape In ActiveDocument.InlineShapes
        If Myshape.AlternativeText = PDFname Then
            ActiveDocument.InlineShapes(IndexCount).OLEFormat.Activate
        End If
        IndexCount = IndexCount + 1
    Next
    
    Selection.InsertBreak Type:=wdPageBreak
    End If
    
    On Error Resume Next
    Debug.Print MyName
    MyName = Dir ' gets the next doc file in the directory
    Loop
    
    End Sub


     

     

     

    If this has to be done using 2 Macros, that’s fine.  If I can do it in 1, that’s great too.

     


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.



    • Edited by ryguy72 Thursday, April 23, 2015 10:47 PM
    Thursday, April 23, 2015 4:02 PM

Answers

  • Hi ryguy72,

    >>When I have PDF files and Word files, the contents of the Word files are copied in fine, but the contents of the PDF files seem to come in as Chinese, and there is no Chinese in the PDF, so I have no idea where that’s coming from.<<

    Based on the code, you were insert the file via the code Selection.InsertFile. I am trying to reproduce this issue however failed. I suggest that you insert the PDF file manually to see whether this issue relative to the specific file. You can insert PDF file via Insert->Text->Object->Text from file.

    If this issue also could reproduced manually, I would suggest that you reopen a new thread in forum to narrow down whether this issue relative to the specific PDF file or Word application.

    >> I want to extract all embedded files (in all my Word files) and save the extracted/opened file into the folder.  Some embedded files are PDFs and some are Excel files.<<

    We can save the embedded spreadsheet via Excel object model. Here is an example that check the whether the inlineshape is an embedded workbook and save it to the disk for you reference:

    If Application.ActiveDocument.InlineShapes(1).OLEFormat.ClassType = "Excel.Sheet.12" Then
    
    Application.ActiveDocument.InlineShapes(1).OLEFormat.DoVerb xlPrimary
    Application.ActiveDocument.InlineShapes(1).OLEFormat.Object.SaveAs "C:\workbook1.xlsx"
    Application.ActiveDocument.InlineShapes(1).OLEFormat.Object.Close
    
    End If

    And since the Word object model doesn't provide API to save the embedded PDF, I would suggest that you get more effective response from PDF support forum to see whether it supports automation. If yes, we can export the PDF as embedded spreadsheet like code absolve.

    Hope it is helpful.

    Regards & Fei



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ryguy72 Friday, April 24, 2015 11:59 AM
    Friday, April 24, 2015 6:14 AM
    Moderator

All replies

  • Hi ryguy72,

    >>When I have PDF files and Word files, the contents of the Word files are copied in fine, but the contents of the PDF files seem to come in as Chinese, and there is no Chinese in the PDF, so I have no idea where that’s coming from.<<

    Based on the code, you were insert the file via the code Selection.InsertFile. I am trying to reproduce this issue however failed. I suggest that you insert the PDF file manually to see whether this issue relative to the specific file. You can insert PDF file via Insert->Text->Object->Text from file.

    If this issue also could reproduced manually, I would suggest that you reopen a new thread in forum to narrow down whether this issue relative to the specific PDF file or Word application.

    >> I want to extract all embedded files (in all my Word files) and save the extracted/opened file into the folder.  Some embedded files are PDFs and some are Excel files.<<

    We can save the embedded spreadsheet via Excel object model. Here is an example that check the whether the inlineshape is an embedded workbook and save it to the disk for you reference:

    If Application.ActiveDocument.InlineShapes(1).OLEFormat.ClassType = "Excel.Sheet.12" Then
    
    Application.ActiveDocument.InlineShapes(1).OLEFormat.DoVerb xlPrimary
    Application.ActiveDocument.InlineShapes(1).OLEFormat.Object.SaveAs "C:\workbook1.xlsx"
    Application.ActiveDocument.InlineShapes(1).OLEFormat.Object.Close
    
    End If

    And since the Word object model doesn't provide API to save the embedded PDF, I would suggest that you get more effective response from PDF support forum to see whether it supports automation. If yes, we can export the PDF as embedded spreadsheet like code absolve.

    Hope it is helpful.

    Regards & Fei



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ryguy72 Friday, April 24, 2015 11:59 AM
    Friday, April 24, 2015 6:14 AM
    Moderator
  • Ok.  Thanks for the tips.  Very helpful.


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Friday, April 24, 2015 11:59 AM