Results 1 to 10 of 24

Thread: Get Pictures from Word Documents in All Sub Folders

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi
    I am not sure if I can help much further , because the only reason(s) that I know of, to cause a line like strPath & "" & strDocumentName & ".htm*" to error is( are ) :
    _ if there ere no file of the type strDocumentName & ".htm*" at the specified path. But the macro makes one file there with this line
    Code:
     ActiveDocument.SaveAs strPath & "\" & strDocumentName, wdFormatHTML, , , , , True       ' The macro seems to save the active document under its existing name, at the existing place, but with the extension type changed to .htm , so you would have then the active document, if it was MyDoc.doc now saved as MyDoc.htm - …. It is not clear to me why that is being done??
     
    So there should be at least that file to be killed. So I am puzzled.
    I am not saying necessarily that there are not other reasons to cause that error, but if there are , then I am not aware of them.
    ( _ Also the code line would error if

    I possibly need more information from you, but I don’t know what information to ask for since I do not fully understand what it is you are trying to do. - You will be familiar with all of your project and all that you are doing, but I know only what you have told me. I may be missing some important details.
    It might be one of those chicken and Egg situations.. – Me not knowing fully what your problem is until after you have a cure, since I am not 100% familiar with all that you are doing.



    Quote Originally Posted by prkhan56 View Post
    ....I found this code on this group which used to work but now it is giving run time error '53': File not found and highlights the following:
    Kill strPath & "" & strDocumentName & ".htm*"
    It used to work and move the images to a folder “MovedToHere”.
    Can you clarify: Do you mean it used to work for you? Do you mean that you previously have run the macro and had the macro working without error?




    You could try this quick test. It might help take us further, although I am not sure how, because I don’t relay know what’s going on..
    This modified macro should tell you if you have a file to be killed, just before the code line to kill which is giving you the error.
    It should tell you that you have at least one file there.

    Code:
    Option Explicit '                                                                                                        I AM IN WORD!!!!
    Sub GetPicturesFromWordDocument_2() '  https://excelfox.com/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15617#post15617
    Dim strFile As String, strFileType As String, strPath As String, strOriginalFile As String, strDocumentName As String
    Dim lngLoop As Long
     Let strFileType = "*.png;*.jpeg;*.jpg;*.bmp" 'Split with semi-colon if you want to specify more file types
    
     Let strOriginalFile = ActiveDocument.FullName
     Let strDocumentName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1) ' The macro stores the current active document name ( but without the extension type) in strDocumentName. So if the active document was MyDoc.doc , then strDocumentName will have MyDoc in it.
     Let strPath = ActiveDocument.Path                                                       ' We also store the path to the current active document in strPath
    
     ActiveDocument.SaveAs strPath & "\" & strDocumentName, wdFormatHTML, , , , , True       ' The macro seems to save the active document under its existing name, at the existing place, but with the extension type changed to .htm , so you would have then the active document, if it was MyDoc.doc now saved as MyDoc.htm - …. It is not clear to me why that is being done??
     
        If Dir(strPath & "\MovedToHere", vbDirectory) = "" Then MkDir strPath & "\MovedToHere"  ' The macro makes a folder, MovedToHere in the same place as where the current active document is. I have slightly modified this code line, to prevent it erroring if the folder already exists: It only makes the folder if the folder does not exist. - If you try to make a folder when it already exists, then that would chuck up an error
        
        For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";")) ' ========================    The main outer loop is doing the following:   It is looping 4 times, going through all your file extension types, .png .jpeg .jpg and .bmp. ( The loop control variable, lngLoop , is going from 0 To 3 )    For each file extension type it is looking for files which are in a folder which, using the same example, would have a name like MyDoc_files . That folder is looked for at the same path as the current active document.So for example, the first loop is looking for files of the extension type .png in that folder
         Let strFile = Dir(strPath & "\" & strDocumentName & "_files\" & Split(strFileType, ";")(lngLoop))
            Do While strFile <> ""   '     The purpose of the Do While __ Loop _ loop is to keep going while you still find files of the extension type currently being looked for.
             Name strPath & "\" & strDocumentName & "_files\" & strFile As strPath & "\MovedToHere\" & "New " & strFile  '   Each of the files you find gets copied to folder, MovedToHere and has its name modified a bit to have the text "New " added at the start, like for example, Filex.png would become New Filex.png
             Let strFile = Dir  '   The use of Dir on its own, without any bracket ( ) stuff tells VBA to look again for the next file of the same type and in the same place that it looked the last time
            Loop
        Next lngLoop ' ============================================================================================
        
     ActiveDocument.Close 0         ' Once we have finished doing all that copying, we close the current active document. It is not clear to me why that is being done. In particular it’s not clear to me why it is done at this point. We could have closed it immediately after we created it, since we have done nothing with it since creating it
     Documents.Open strOriginalFile ' We now open the original file we had open at the start of running the macro. Its not clear to me why we do that, other than maybe to get back to having the same file open and active that we had when we started running the macro.
     
    ' Test section
    Dim FileExistTest As String: Let FileExistTest = Dir(strPath & "\" & strDocumentName & ".htm*", vbNormal)
        If FileExistTest = "" Then MsgBox Prompt:="You don't seem to have any files to be killed": Debug.Print "You don't seem to have any files to be killed"
     
        Do While FileExistTest <> ""
         MsgBox Prompt:="You have this file to be killed:   " & FileExistTest: Debug.Print "You have this file to be killed:   " & FileExistTest
         Let FileExistTest = Dir
        Loop
     
     Kill strPath & "\" & strDocumentName & ".htm*"
        If Not Dir(strPath & "\" & strDocumentName & "_files\*.*") = "" Then Kill strPath & "\" & strDocumentName & "_files\*.*"  '    Kill strPath & "" & strDocumentName & "_files\*.*" could error , if, for example, you had only had files of the type .png .jpeg .jpg or .bmp originally in that folder with the name like MyDoc_files . The reason for that is because the VBA Name statement renames a file, in other words it moves , or in other words it copies the file to somewhere and then deletes the original. So effectively it will be removing all files of the type .png .jpeg .jpg or .bmp from that folder.     So I have modified that code line so that it only tries to delete files if there are any files there to delete. I expect the reason the code line is there is so that the next code line works. – This next code line, RmDir strPath & "" & strDocumentName & "_files" , tries to delete the original folder, and that code line would error if any files were in that folder.
     RmDir strPath & "\" & strDocumentName & "_files"                                                                                 '   https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/rmdir-statement
    'strFile = vbNullString  '   These last few lines are not needed in VBA. These code lines were considered good practice in programming earlier, I think. Possibly they may have sometimes been needed previously. I am not sure.
    'strFileType = vbNullString
    'strPath = vbNullString
    'lngLoop = Empty
         
    End Sub
    This is the new bit
    Code:
    ' Test section
    Dim FileExistTest As String: Let FileExistTest = Dir(strPath & "\" & strDocumentName & ".htm*", vbNormal)
        If FileExistTest = "" Then MsgBox Prompt:="You don't seem to have any files to be killed": Debug.Print "You don't seem to have any files to be killed"
     
        Do While FileExistTest <> ""
         MsgBox Prompt:="You have this file to be killed:   " & FileExistTest: Debug.Print "You have this file to be killed:   " & FileExistTest
         Let FileExistTest = Dir     '   The use of Dir on its own, without any bracket ( ) stuff tells VBA to look again for the next file of the same type and in the same place that it looked the last time
        Loop
     



    Alan
    Last edited by DocAElstein; 09-02-2021 at 12:19 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

  2. #2
    Junior Member
    Join Date
    Aug 2021
    Posts
    13
    Rep Power
    0
    Quote Originally Posted by DocAElstein View Post




    Can you clarify: Do you mean it used to work for you? Do you mean that you previously have run the macro and had the macro working without error?




    You could try this quick test. It might help take us further, although I am not sure how, because I don’t relay know what’s going on..
    This modified macro should tell you if you have a file to be killed, just before the code line to kill which is giving you the error.
    It should tell you that you have at least one file there.
    ........

    This is the new bit
    Code:
    ' Test section
    Dim FileExistTest As String: Let FileExistTest = Dir(strPath & "\" & strDocumentName & ".htm*", vbNormal)
        If FileExistTest = "" Then MsgBox Prompt:="You don't seem to have any files to be killed": Debug.Print "You don't seem to have any files to be killed"
     
        Do While FileExistTest <> ""
         MsgBox Prompt:="You have this file to be killed:   " & FileExistTest: Debug.Print "You have this file to be killed:   " & FileExistTest
         Let FileExistTest = Dir     '   The use of Dir on its own, without any bracket ( ) stuff tells VBA to look again for the next file of the same type and in the same place that it looked the last time
        Loop
     
    Dear Alan
    Thanks a lot for taking time and submitting the test code.

    It pops up with a message box saying you don't have any files to be killed and when pressed Ok it highlights the same line as mentioned in my previous post.
    Yes, this macro used to work in the past absolutely fine. But for some reason it started behaving like this.

    I have noticed few things it does when you Click End on the Error dialogue box

    It creates two folders as follows: (I am attaching a sample word file on which I tested your code)
    Folder1 - 2_files - in this folder you will see that there are images (but they are in duplicates one in png and one in jpg format - this is also strange behaviour!), also you will see some xml and thmx files here.

    Folder2 - MovedToHere - there are no files here.

    and one file is created which is not associated with any other application.

    The sample file is attached herewith for you to test.

    Thanks once again for taking out the time to help me.
    Attached Files Attached Files
    Last edited by DocAElstein; 09-03-2021 at 09:44 AM.

  3. #3
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi,
    Quote Originally Posted by prkhan56 View Post

    Thanks a lot for taking time and submitting the test code.
    It pops up with a message box saying you don't have any files to be killed and when pressed Ok it highlights the same line as mentioned in my previous post.
    ....
    As you probably realise, the purpose of the test coding is to see if VBA thinks it sees a file of the type which the erroring code line tries to kill ( delete ). The test coding does not appear to see any files of that type, Hence, as we know, the Kill Statement ( https://bettersolutions.com/vba/func...-statement.htm ) will error.




    Quote Originally Posted by prkhan56 View Post
    ...The sample file is attached herewith for you to test..
    Thanks for the file.
    I think I see now some of the information which I was missing: I wrote a few times already that I could not understand why you save the active file with this code line
    ActiveDocument.SaveAs strPath & "" & strDocumentName, wdFormatHTML, , , , , True
    If you do that code line for when the active file is a very simple file, for example a Word file , MyFile.doc , with just some simple plain text in it, then you get a simple file saved which simply has its name changed to MyFile.htm. At least that was my experience so far: I have only limited experience with these sort of Word / html things
    Now I see something for the first time:
    I see now that when you do that code line for when the active file is a Word file which contains images, like your 2. KEEP DUPLICATE RECORDS.docx , then you no longer get a simple file saved which simply has its name changed to 2. KEEP DUPLICATE RECORDS.htm
    Instead you get a lot of other stuff, including some image files.

    So that explains some of the mystery. If you were not aware that this happens, then I am puzzled that you have got this far, and am puzzled that you ever got the macro to work…
    If you were aware of this, then you should have told me. If you ask for help on a forum, you should always gives as much information as possible: It’s best to assume the person trying to help you has no idea of what you are doing or wanting to do or how you are attempting to do it.

    So , what’s going on….
    I lack the experience with these Word and Word VBA issues.
    But my initial uneducated guess is that Word will produce different file types when you save with the .htm extension type and that possibly exactly what you get will depend on many factors. This might explain why you get varying results. I don’t really have the experience to help here. I am seeing this phenomena for the first time.




    Quote Originally Posted by prkhan56 View Post
    ... in duplicates one in .png and one in .jpg format - this is also strange behaviour!), ....
    I think I may have heard of that phenomena before, but I can’t recall where and when. This is possibly coming back to my previous point above, that….. my initial uneducated guess is that Word will produce different file types when you save with the .htm extension type and that possibly exactly what you get will depend on many factors. This might explain why you get varying results.





    So I am working in Word 2007, and when I run the macro with 2. KEEP DUPLICATE RECORDS.docx active, then the code line ActiveDocument.SaveAs strPath & "" & strDocumentName, wdFormatHTML, , , , , True produces this
    2_ KEEP DUPLICATE RECORDS_docx gices these files when saved as _htm.JPG2_ KEEP DUPLICATE RECORDS_docx gives these files when saved as _htm.JPG


    Running the complete macro will ( before it errors ) produce this, which I expect is similar to what you have seen:
    2_ KEEP DUPLICATE RECORDS_docx gives these files.JPG 2_ KEEP DUPLICATE RECORDS_docx gives these files.JPG 2_ KEEP DUPLICATE RECORDS_docx gives these files when saved as _htm.JPG


    Those last 3 screen shots are the same as the previous 2 except that we have also made the directory , MovedToHere , using this code line __ If Dir(strPath & "\MovedToHere", vbDirectory) = "" Then MkDir strPath & "\MovedToHere"




    So it all makes sense to me, now. I see where the problem is coming from.
    _ No file is produced with the .htm extension, Hence the macro will error if no other files of that type are present.
    _ Nothing gets moved to the directory , MovedToHere , because you are looking in the wrong place for those files: The files are in that strange named folder 2-files. ( My Excel is German so Files is translated to Dateien )

    I think I probably could re write the macro so that it does what you want, at least here in my Word 2007 version. But it might not work in your system if the code line ActiveDocument.SaveAs strPath & "" & strDocumentName, wdFormatHTML, , , , , True produces slightly different files

    I do have many different versions of Word and many different computers. I could probably write a macro and check that it works in all systems. But that would be a lot of work for me which I don’t have the time to do all that just now.
    The solution would probably involve looking at all sub folders to make sure we find any files anywhere. Once again this increases the complexity and so increases the work needed.





    You might be best advised to try at one of the other forums.
    There are lots more people there and you might be lucky to find someone that has tried to do exactly what you are doing , had similar problems, and has an efficient solution already.
    I could probably come up with a new solution, but it may not be the best.
    ( If you get the problem solved at another forum , it would be helpful to tell us about it, for the benefit of anyone else seeing this Thread in the future ).

    Otherwise I will take another look at this in a few days when I have some free time


    I would also advise that you take some time to understand the current macro, and possibly add your own explaining 'comment notes .

    Alan
    Last edited by DocAElstein; 09-03-2021 at 04:00 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

  4. #4
    Junior Member
    Join Date
    Aug 2021
    Posts
    13
    Rep Power
    0
    Dear Alan,
    Thanks for all the explanation and time to have look at the macro.
    The code I posted was from the Excel group on this forum only and that is the reason I posted it initially on the Word and then later on the Excel Forum.

    I did a search on Google before posting the problem on this forum and unfortunately could not find a suitable solution.
    This was the one which was suiting my requirement, which is not working now.

    I would rather wait for your free time and see what you come up with.

    Thanks once again.

Similar Threads

  1. Replies: 1
    Last Post: 08-26-2021, 11:42 AM
  2. Replies: 3
    Last Post: 07-09-2020, 02:17 AM
  3. Replies: 7
    Last Post: 08-24-2015, 10:58 PM
  4. Replies: 9
    Last Post: 07-26-2013, 02:34 PM
  5. Replies: 1
    Last Post: 10-16-2012, 01:53 PM

Posting Permissions

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