Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Get Pictures from Word Documents in All Sub Folders

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    13
    Rep Power
    0

    Get Pictures from Word Documents in All Sub Folders

    Dear All,
    I posted this last week on the Word Group but did not get any help so I am posting in the Excel section

    I am using Office 2013.

    I have a folder with many sub folders having Word and Excel files.

    Excel file does not have images but the word documents under each sub folder have many images which I would like to move under each respective folders.

    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”.

    Code:
    Sub GetPicturesFromWordDocument()
    
        Dim strFile As String
        Dim strFileType As String
        Dim strPath As String
        Dim lngLoop As Long
        Dim strOriginalFile As String
        Dim strDocumentName As String
        strOriginalFile = ActiveDocument.FullName
        strDocumentName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1)
        strPath = ActiveDocument.Path
        ActiveDocument.SaveAs strPath & "\" & strDocumentName, wdFormatHTML, , , , , True
        strFileType = "*.png;*.jpeg;*.jpg;*.bmp" 'Split with semi-colon if you want to specify more file types.
         
        MkDir strPath & "\MovedToHere"
        For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";"))
            strFile = Dir(strPath & "\" & strDocumentName & "_files\" & Split(strFileType, ";")(lngLoop))
            Do While strFile <> ""
                Name strPath & "\" & strDocumentName & "_files\" & strFile As strPath & "\MovedToHere\" & "New " & strFile
                strFile = Dir
            Loop
        Next lngLoop
        ActiveDocument.Close 0
        Documents.Open strOriginalFile
        Kill strPath & "\" & strDocumentName & ".htm*"
        Kill strPath & "\" & strDocumentName & "_files\*.*"
        RmDir strPath & "\" & strDocumentName & "_files"
        strFile = vbNullString
        strFileType = vbNullString
        strPath = vbNullString
        lngLoop = Empty
         
    End Sub
    I want to fix this code and also amend to run on all the sub folders.
    Can someone fix this issue for me please.
    Thanks in advance

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi prkhan56
    Welcome to ExcelFox

    I am sorry you have had no reply.
    We don’ t have many Word experts popping by excelfox much these days.

    I don’t know much about Word VBA, and have never done anything with images so I don’t really understand what is wanted here. I don’t see the relation to images , pictures , “moving images”.

    I have manipulated Word files with some VBA code working from Excel. Sometime my files were saved as extension type .htm – those files were normal word files with a lot of text and tables in them and the coding handled them the same as any files of extension type .doc or .docx or .docm

    So I am not really so well qualified to help on what you want, but I will have a go…..




    I took a look at your macro , Sub GetPicturesFromWordDocument() ,
    I have rewritten, or rather just re arranged slightly the macro and made some minor changes as I went along and added some 'comments . I did this to help me understand what is going on.
    ( Here is my version: https://excelfox.com/forum/showthrea...ll=1#post15614 )

    Here is a walk through my version:
    For the sake of explanation, let me assume that when you run this macro you have a Word document open , which is active, and it has the name MyDoc.doc

    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.
    We also store the path to the current active document in strPath
    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??

    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. – The reason why I did this is because, if you try to make a folder when it already exists, then that would chuck up an error

    __ 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 run of the loop (lngLoop = 0 ) , is looking for files of the extension type .png in that folder
    ____ 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. ( 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 )
    ____ 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 ( Note: actually we are not really copying – we are moving – the original file gets effectively deleted )

    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

    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.

    Now we go on to killing ( deleting ) a few things.
    The code line Kill strPath & "" & strDocumentName & ".htm*" does not error for me. I can not see why it should, since it is trying to delete all files of the extension type .htm , html etc. in the folder where we made like our MyDoc.htm
    Since we should have at least that one file there, MyDoc.htm , then that at least should be there to be deleted. ( The code line would error if there are no files of the type .htm , html etc. there: Kill will error if it can’t find files of the type you are trying to kill )
    The next code line, 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.

    The last few lines are not needed in VBA. Those code lines were considered good practice in programming earlier, I think. Possibly they may have sometimes been needed previously. I am not sure.

    I am not sure if I can help much further, since I cannot reproduce your error. The macro version of mine ( Here: https://excelfox.com/forum/showthrea...ll=1#post15614 ) does not error, but I may have missed something due to my lack of experience with Word VBA.
    Quote Originally Posted by prkhan56 View Post
    I want to fix this code ...... Can someone fix this issue ...
    I cant fix the code for you , because I cannot see the problem with it. But I am also not 100% sure of why some things are being done in the macro.




    Quote Originally Posted by prkhan56 View Post
    .....and also amend to run on all the sub folders.....
    I don’t think you can amend a macro like this one to do that. The reason for me saying that is that the main process we are using to look at, and get at files, is the Dir function, and in particular the code line of Dir within a loop. This restricts us to one “folder level”.
    We are using a fairly simple macro, like the one you are using.
    Its this sort of thing: https://excelfox.com/forum/showthrea...ull=1#post6175
    To look at sub folders we would usually use a different macro type, one which uses recursion. This sort of thing:
    https://excelfox.com/forum/showthrea...ll=1#post10420
    https://excelfox.com/forum/showthrea...ll=1#post10421
    https://excelfox.com/forum/showthrea...ll=1#post10422

    As you can see, that is a rather complex thing. Depending on your knowledge of VBA, that could be a rather time consuming thing to get across to you, especially as we don’t have the simpler issue fixed of why you are getting the error in the simpler macro

    I expect it could take me a long time to help you further. I am busy all this week, and could take another look for you next week.

    Alternatively you might want to try one of the other forums where a lot more people usually are, and certainly more people clued up on Word VBA
    Here a couple of places :
    https://www.excelforum.com/word-programming-vba-macros/
    http://www.eileenslounge.com/viewforum.php?f=26


    Please note that most forums have what they call a “cross posting rule”. This means that you should tell everyone everywhere about where else you have posted the same question.
    So for example you should pass on these URL link to your questions here
    https://excelfox.com/forum/showthread.php/2760-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15605#post15605
    https://excelfox.com/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15613#post15613

    One last tip here: If you are posting for the first time at some forums then a spam filter may sometimes prevent you posting those links. To get over that you need to disguise them when posting. You could add some spaces like this
    h t t p s:/ /excelfox . com/forum/showthread.php/2760-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15605#post15605
    h t t p s:/ /excelfox . com/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15613#post15613

    Or alternatively try fooling the filter by posting using some BB code for black color to disguise the link – that way the filter does not see the link, but it comes out in the final post as you want it. So you would post this:
    htt[color=Black]p[/color]s:[color=Black]/[/color]/excelfox[color=Black].c[/color]om/forum/showthread.php/2760-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15605#post15605
    htt[color=Black]p[/color]s:/[color=Black]/[/color]excelfox[color=Black].c[/color]om/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15613#post15613



    Hope that is some help for you. If you have not got the problem solved by next week , then if you post again here and fill us in on what you have done in the meantime, then I will have another try for you..

    Alan
    Last edited by DocAElstein; 08-26-2021 at 11:54 AM.
    ….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!!

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    13
    Rep Power
    0
    Dear Alan,
    Thanks for your time and attempt.
    I tried your code but unfortunately it is giving the same error as per the previous code posted by me.

    I am not sure what is the reason.

    I tried it on a different machine also but no success.

    If you wish you can rewrite it according to your expertise.
    I have not posted it on any other forum(s).

    Thanks once again for your time and help.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    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!!

  5. #5
    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.

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    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!!

  7. #7
    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.

  8. #8
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    OK, no problem, I will take another look in a few days time
    ….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!!

  9. #9
    Junior Member
    Join Date
    Aug 2021
    Posts
    13
    Rep Power
    0
    Quote Originally Posted by DocAElstein View Post
    OK, no problem, I will take another look in a few days time
    Thanks a lot

  10. #10
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi
    I have done a little internet research and also done some testing,
    ( https://excelfox.com/forum/showthrea...ll=1#post15637
    https://excelfox.com/forum/showthrea...ll=1#post15638
    https://excelfox.com/forum/showthrea...ll=1#post15639 https://excelfox.com/forum/showthrea...ll=1#post15640
    https://excelfox.com/forum/showthrea...ll=1#post15641
    https://excelfox.com/forum/showthrea...ll=1#post15642
    https://excelfox.com/forum/showthrea...ll=1#post15643
    https://excelfox.com/forum/showthrea...ll=1#post15644
    https://excelfox.com/forum/showthrea...ll=1#post15645
    )

    I remain somewhat puzzled that the macro that you initially gave ever worked!
    But , never mind that for now!

    I think I have just about learnt enough to be able to make a macro to Get Pictures from Word Document.

    I suggest that we concentrate initially on convincing ourselves that we can make a macro to Get Pictures from Word Document.
    If we are then confidentand happy that we can do that, then we can move on later to the issue of Word Documents in All Sub Folders


    Run the following macro when you have the attached returned file open.

    Code:
    Sub GetPicturesfromWordDocument()
           '       Documents.Open FileName:="F:\Excel0202015Jan2016\ExcelFox\Word\prkhan56\2. KEEP DUPLICATE RECORDS.docx" ' This wont error if document file is already open
    Rem 1 Our  File with possibly images in it
    Dim DocWithImgs As Document: Set DocWithImgs = ActiveDocument ' This is a personal preferrence of mine to do it like this, as i do not like to use  ActiveDocument   too much in coding incase some other documant becomes accidentally active. Doing this, I only need to make sure the documant that I am intersted in is active initially
    Dim strPath As String: Let strPath = DocWithImgs.Path
    Dim strOriginalFile As String: Let strOriginalFile = DocWithImgs.FullName ' This is the full path and file name of the current Active document. This should be the file from which you want to extract the images
    Dim strDocumentName As String: Let strDocumentName = Left(DocWithImgs.Name, InStrRev(DocWithImgs.Name, ".") - 1) '  this will be the active
    Rem 2 save file as extension type   .htm  - this will produce a  .htm  file  and a  folder with, amoungst other things, files of any images in the document.  In English Excel this folder will have the name   strDocumentName & "-Files"   In German it will be   strDocumentName & "-Dateien"
     DocWithImgs.SaveAs Filename:=strPath & "\" & strDocumentName & ".htm", FileFormat:=wdFormatHTML
     Documents(strDocumentName & ".htm").Close  ' The purpose of the  Save As    .htm  was to get thee new folder made, that's all, so we can close that file now, and then kill (delete) it
     Kill strPath & "\" & strDocumentName & ".htm"
    Rem 3 check we have a new Folder  with a name like     strDocumentName & "-......  "   -  In English Excel this folder should have the full name and path of     strPath & "\" & "2. KEEP DUPLICATE RECORDS-Files"                In German Excel it should  be    strPath & "\" & "2. KEEP DUPLICATE RECORDS-Dateien"
        If Dir(strPath & "\" & strDocumentName & "-*", vbDirectory) = "" Then
         MsgBox prompt:="Something went wrong. There is no new folder produced"
        Else
        Dim FileFlder As String: Let FileFlder = Dir(strPath & "\" & strDocumentName & "-*", vbDirectory)
        End If
    Rem 4 copy all  .jpg  images to a  new  folder    MovedToHere
        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
    Dim strFile As String: Let strFile = Dir(strPath & "\" & FileFlder & "\*.jpg", vbNormal) '  look for first  .jpg   file, if there is one
        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 & "\" & FileFlder & "\" & 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 ' While strFile <> ""
    Rem 5 remove the directory made by the  SaveAs  .htm
           If Not Dir(strPath & "\" & FileFlder & "\*.*") = "" Then Kill strPath & "\" & FileFlder & "\*.*"
     RmDir strPath & "\" & FileFlder
    End Sub
    After running that macro, you should see that your 6 image files are in the folder MovedToHere

    6 jpgs after running Sub GetPicturesfromWordDocument().JPG


    See how you get on with that.


    Alan
    Attached Files Attached Files
    Last edited by DocAElstein; 09-23-2021 at 12:22 AM.

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
  •