PDA

View Full Version : Get Pictures from Word Documents in All Sub Folders



prkhan56
08-25-2021, 02:04 PM
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”.



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

DocAElstein
08-26-2021, 11:40 AM
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/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=15614&viewfull=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/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=15614&viewfull=1#post15614 ) does not error, but I may have missed something due to my lack of experience with Word VBA.

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.




.....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/showthread.php/1324-Loop-Through-Files-In-A-Folder-Using-VBA?p=6175&viewfull=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/showthread.php/1324-Loop-Through-Files-In-A-Folder-Using-VBA?p=10420&viewfull=1#post10420
https://excelfox.com/forum/showthread.php/1324-Loop-Through-Files-In-A-Folder-Using-VBA?p=10421&viewfull=1#post10421
https://excelfox.com/forum/showthread.php/1324-Loop-Through-Files-In-A-Folder-Using-VBA?p=10422&viewfull=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:
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


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

prkhan56
08-31-2021, 12:29 PM
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.

DocAElstein
09-02-2021, 12:11 PM
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

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.



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


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

' 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

prkhan56
09-02-2021, 12:43 PM
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

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

DocAElstein
09-03-2021, 03:12 PM
Hi,


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/functions/kill-statement.htm ) will error.




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




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


Running the complete macro will ( before it errors ) produce this, which I expect is similar to what you have seen:
3615 3614 3616


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

prkhan56
09-06-2021, 12:49 PM
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.

DocAElstein
09-06-2021, 02:30 PM
OK, no problem, I will take another look in a few days time

prkhan56
09-08-2021, 12:09 AM
OK, no problem, I will take another look in a few days time

Thanks a lot

DocAElstein
09-16-2021, 10:49 PM
Hi
I have done a little internet research and also done some testing,
( https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15637&viewfull=1#post15637
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15638&viewfull=1#post15638
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15639&viewfull=1#post15639 https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15640&viewfull=1#post15640
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15641&viewfull=1#post15641
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15642&viewfull=1#post15642
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15643&viewfull=1#post15643
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15644&viewfull=1#post15644
https://excelfox.com/forum/showthread.php/2348-Just-Testing-Passing-info-between-Word-and-Excel?p=15645&viewfull=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.


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
https://i.postimg.cc/LqZ5bjrk/6-jpgs-after-running-Sub-Get-Picturesfrom-Word-Document.jpg (https://postimg.cc/LqZ5bjrk)
3627


See how you get on with that.


Alan

prkhan56
09-21-2021, 06:59 PM
Dear Alan,
I was testing your macro on different PCs.

Unfortunately it does not create the folder "MovedToHere"
I am enclosing two images which is what the macro creates
1 - Keep Duplicate Records.htm file
2 - Keep Duplicate Records_files (folder)

The folder contains the images which is shown in MacroTest2.png attached
It is similar to what the previous macro was giving.

I am totally loss at words what to say now.

I really appreciate all your time and help in this matter.

Thanks a lot

DocAElstein
09-22-2021, 03:23 PM
Hello

I have no idea where your problem is.

I think you will need to step through the macro in Debug mode yourself and follow through yourself to locate where the problem is


Example: I try the macro again…

This code line
DocWithImgs.SaveAs FileName:=strPath & "" & strDocumentName & ".htm", FileFormat:=wdFormatHTML
makes the .htm file and the _____-Files
https://i.postimg.cc/66ZRRYJK/Made-2-KEEP-DUPLICATE-RECORDS-htm.jpg https://i.postimg.cc/J4WXQZMk/Files-are-made.jpg

This code bit makes the directory MovedToHere
If Dir(strPath & "\MovedToHere", vbDirectory) = "" Then MkDir strPath & "\MovedToHere"
https://i.postimg.cc/YSygNPCr/Directory-Moved-To-Here-is-made.jpg

If you step through this code section, you should see it looping and copying files

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 <> ""
As it loops you will see the folder MovedToHere being filled
https://i.postimg.cc/mrrCH4Rp/Directory-Moved-To-Here-being-filled.jpg
https://i.postimg.cc/6pcCqqsG/Directory-Moved-To-Here-being-filled.jpg
https://i.postimg.cc/bJDnmRZQ/Directory-Moved-To-Here-being-filled.jpg
https://i.postimg.cc/6qCvX5Lq/Directory-Moved-To-Here-being-filled.jpg
https://i.postimg.cc/cHpn283P/Directory-Moved-To-Here-being-filled.jpg

https://i.postimg.cc/x1nK9HZ2/Directory-Moved-To-Here-filled.jpg




This code bit
If Not Dir(strPath & "" & FileFlder & "\*.*") = "" Then Kill strPath & "" & FileFlder & "\*.*"
empties the ___-Files folder
https://i.postimg.cc/BQxTWNMp/File-folder-has-had-all-files-killed.jpg






If you do not know how to step through in Debug F8 mode in VBA , then …..
Get into the vbeditor ( Key Alt+F11 from Excel ) , and click anywhere in the macro
https://i.postimg.cc/kGrB53fH/get-into-macro.jpg

Hit key F8


This will step you through the macro , one line at a time
https://i.postimg.cc/R0DWjnf4/Steping-through-in-F8.jpg
https://i.postimg.cc/d1Zhvtmd/Steping-through-in-F8.jpg
https://i.postimg.cc/fbL32vV8/Steping-through-in-F8.jpg
https://i.postimg.cc/XYxGkPYZ/Steping-through-in-F8.jpg
3631363236333634



Etc…

At each point, you can check if files and folders are being made, filled or killed , etc., as they should be



Alan

prkhan56
09-22-2021, 06:49 PM
Dear Alan,
I have run the code as per your instruction.
It creates the folder as per my previous post.
I am attaching two images herewith.

The first one is Debug and the second one is Breakpoint.
The macro comes out at the breakpoint and does not create any folder "MovedToHere".

Thanks once again for all your time and help.

DocAElstein
09-22-2021, 07:39 PM
Hi

.....
The macro comes out at the breakpoint and does not create any folder "MovedToHere"..
I don't quite understand what you mean by this.

Do you mean that the macro errors at this line?

Documents(strDocumentName & ".htm").Close



Where do you have the macro? In which document do you have the macro?

prkhan56
09-23-2021, 12:05 AM
Hi Alan,
Yes as per the screen shot I sent to you, the macro comes out on the Breakpoint image.
It does not create any folder "MovedToHere" and move the images in this folder.
Hope it is clear now.

The macro is in the Word File inserted as a Module which is open at the time of running the macro

DocAElstein
09-23-2021, 12:25 AM
Hi

.., the macro comes out ...I am still not sure what you meant? What does ..." the macro comes out..." mean? Does the macro stop with an error message. If so , what error message is given?
_.__________________________


The macro is in the Word File inserted as a Module which is open at the time of running the macro
Which Word file? - If it is in the file which is saved as .htm , then when the file is closed, the macro is gone so nothing further will happen.
_.______________________

You could try to remove the close and kill lines , ( ' comment them out ), and then see what happens.
Like this:

' 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"

prkhan56
09-23-2021, 01:32 PM
Hi Alan,
I tested the code after commenting the lines as per your instructions.

The code was copied in the Word file and also in the Normal template.
Finally it created the 3 folders but before that the following happened.

When I run the code - see attached images
1) Message Box popped up - Something went wrong. There is no new folder produced. I press Ok then step 2
2) Run-time error permission denied - I press Debug then Step 3
3) It highlights the code (see yellow) - image Error

So now the MovedToHere folder is created but there are no images in this folder.

For your information the original Macro used to keep the Word Document open and create the Folder MovedToHere and the images were there in the folders.

Thanks a lot for all the time and help.

DocAElstein
09-23-2021, 02:01 PM
Hi
I think the issues are getting far too complicated now for me to help with at this distance over the internet in a help forum.
As I mentioned at the outset , I don’t have a lot of experience with these sort of Word codings.

As I expect you will realise, if you have read and understood the coding, that Message Box is telling you that …. something went wrong – the folder, MovedToHere , was not made as it should have been.
That problem does not happen here on any of my computers.

But then you say the folder was made. It’s all very confusing and not really possible for me to help here without spending many many hours of testing to try and repeat the error and problems that you are getting.


“permission denied” sounds like you are not being allowed to delete the files. There may be some file or folder protection issues of which I have no experience with.


I don’t think I can help any more. I am out of ideas. I lack the experience with this sort of Word and Image coding. Sorry.



Your best option would be to find someone to help you in person, or try at other forums where a lot more experienced people are. Remember to refer them to this Thread, as I mentioned previously.

I think also, you should now understand all about the coding and how it works. So you should be able to experiment yourself and find the problem.
If you do not understand any of the coding, I will gladly explain anything again in more detail.

Possibly you should start again yourself: Begin by writing smaller code bits to do some of the things, like making a folder, maybe another code to copy a file etc. , etc. Convince yourself you know what is going on.

Good luck
If you get it all sorted, please tell us where and how. Tell us also please if you post the question in other forums. Thanks.
Alan


P.s. …
...
The code was copied in the Word file ....
You still have not told me what word file??? – see my previous comments.

prkhan56
09-27-2021, 04:46 PM
Hello Alan,
The code is in the file you sent me back.
Anyway, I want to thank you for all the time and help you did for me.
I will put it on other forum as my expertise in VBA is very minimal.

Thanks once again.

DocAElstein
09-27-2021, 05:10 PM
Hi

The code is in the file you sent me backThat explains why the macro stops when at the close - The macro is there , running, but then you close it, so its gone.
If you run a macro that is in a document and that macro closes the document, then nothing can happen after the close because both the document and macro is no longer there.
It is like driving a car on a journey, and on the way, in the middle of the journey, God comes and takes your car and the engine away. So the journey stops

Like this
WORD: [document and start macro running ]
WORD: [document and macro running ]
WORD: [document and macro running - macro closes the document ]
WORD: [ _________________________ ] - all is gone. There is nothing there - no document , no running macro. Macro cannot finish - there is no macro - its gone because you closed it

Important: You can stop a macro like this by closing the document with the macro in it. But it is dangerous to do it.
It is like if you are in a plane and want to land. So you take the engine away. The plane will come down. If you are lucky it will come down and stop. If you are unlucky the plane may crash. You may die.
You can stop a macro like this by closing the document with the macro in it. If you are lucky the macro will stop. If you are unlucky your computer may crash. It may die.



Also: A .docx cannot hold a macro - you can try it - try to save a .docx file with a macro in it. - It will not work
A macro can go in normal template or in a docm file

Good luck
Please let us know how you get on.
Alan

prkhan56
10-04-2021, 04:32 PM
Dear Alan,
I had some issues with my laptop and just got it fixed.

I had mentioned in my previous post #17 that I have test the macro both in the file as well is the Normal Template, probably you missed it.

Now what I am going to tell would surprise you as it has surprised me as well.
When I run the macro the folder 2_ KEEP DUPLICATE RECORDS files got created with the error and debug message as posted previously.

The file was in the download folder and the macro was in the Normal template.

When I run the macro. All my files - I mean all docx, xlsx, pptx, jpgs etc etc moved to some where I don't know where.

There is really strange. I am just sharing this with you that this has happened in my download folder.
I have tried looking for it in other folders and I could not find it anywhere in my hard drive.

All the folders are intact but the files have moved to some where :( I have not idea what so ever.

Not a single files is there in the download folder. Only folder remains

I really appreciate all the help and time you have taken to look up in this matter. But now this files disappearing is something I thought I would share with you.

Thanks once again.

DocAElstein
10-04-2021, 11:18 PM
Hi

...I had mentioned in my previous post #17 that I have test the macro both in the file as well is the Normal Template, probably you missed it.
I did not miss it, but I did not know what file you meant by the file . But you cleared that up in post #19, so all was then OK, I understood after post #19.




Did you see and understand my last reply here, post #20 https://excelfox.com/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15657&viewfull=1#post15657 ?
I tried to explain in post #20 (https://excelfox.com/forum/showthread.php/2761-Get-Pictures-from-Word-Documents-in-All-Sub-Folders?p=15657&viewfull=1#post15657) that it is bad and stupid to put the macro in that .docx file

You should never put any macro in a _.docx file
You should never put a macro in a particular file if that particular file is closed by that macro. -
.....
If you run a macro that is in a document and that macro closes the document, then nothing can happen after the close because both the document and macro is no longer there.
It is like driving a car on a journey, and on the way, in the middle of the journey, God comes and takes your car and the engine away. So the journey stops

Like this
WORD: [document and start macro running ]
WORD: [document and macro running ]
WORD: [document and macro running - macro closes the document ]
WORD: [ _________________________ ] - all is gone. There is nothing there - no document , no running macro. Macro cannot finish - there is no macro - its gone because you closed it
.....





...... All my files - I mean all docx, xlsx, pptx, jpgs etc etc moved to some where I don't know where.

There is really strange. ......
I have tried looking for it in other folders and I could not find it anywhere in my hard drive.

All the folders are intact but the files have moved to some where :( I have not idea what so ever.

.....

I have sometime experienced that problem. Sometime that might happen if the place you intended saving is temporarily not available. Sometimes when this happens the files are saved in some default place. Exactly where will be different for every computer.
On my computers they might go somewhere like
C:\Users\Elston\Documents
Or
C:\Windows\System32
Or
C:\Program Files\Google\Chrome\Application\49.0.2623.112
Those are just some examples

Every computer will be a bit different. ( But it often goes to the same place, so if you do find where, it would be wise to make a note of the path, as I have done )

One way to find w]here this strange place is, is to save something firstly on an external USB stick. Then remove the USB stick and try to save again. Sometime your computer will then try to save in the strange place because it cannot find the correct place, because you removed the correct place.( Foa a particular computer, it often goes to the same place, so if you do find where, it would be wise to make a note of the path )

Alan

prkhan56
10-05-2021, 12:50 PM
Dear Alan,

Thanks for your reply and also sharing your experience of similar problem.
I am aware of the docx and docm files and their respective properties.

My question is, why did it happened after running the macro in the Normal Template. My knowledge of VBA is very minimal so I could not what happened after running the code.

In case the files have been moved then it should be found on the system (somewhere). I checked the path you suggested. I tried various utilities to check whether it is moved or deleted but to no success.

It is just not there any where on the system.
Is the code renaming the files and moving or deleting them?

Hope you understand my point now.

DocAElstein
10-06-2021, 01:17 PM
Hi

.., why did it happened after running the macro in the Normal Template.I don’t know. I have not yet experienced that problem.


...
Is the code renaming the files and moving or deleting them? The macro uses the VBA Name Statement
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/name-statement - ...... The Name statement renames a file and moves it to a different directory or folder, if necessary ……

The exact processes involved may be a secret that Microsoft would not ell us. But you can test yourself and experiment yourself with simple coding so as to learn and understand what finally happens.

Example 1: Move
Make a folder, anywhere, for example on your desktop https://i.postimg.cc/SQDZbxwk/Make-a-folder-anywhere-for-example-on-your-desktop.jpg
Put two folders in it https://i.postimg.cc/Gt7zFXkH/Put-two-folders-in-it.jpg
Put any file in SubFolder1 https://i.postimg.cc/2y82LsLs/Put-any-file-in-Sub-Folder1.jpg

So now we have a file in SubFolder1 and SubFolder2 is empty https://i.postimg.cc/bNrmyjhb/a-file-in-Sub-Folder1-and-Sub-Folder2-is-empty.jpg

Now run a simple test macro like this

Sub TestWhatVBANameStatementDoes()
Dim strPath As String: Let strPath = "C:\Users\Elston\Desktop\Temporary\" ' --- CHANGE TO SUIT YOUR PATH
Name strPath & "SubFolder1\Neues Textdokument.txt" As strPath & "SubFolder2\Neues Textdokument.txt"
End Sub

After running that macro you will see that the file is now in the other folder only https://i.postimg.cc/XJgsJjqK/the-file-is-now-in-the-other-folder-only.jpg

Before running macro:
https://i.postimg.cc/JGwNxvt4/a-file-in-Sub-Folder1-and-Sub-Folder2-is-empty.jpg (https://postimg.cc/JGwNxvt4)3648



After running macro:
https://i.postimg.cc/zbVC21n3/the-file-is-now-in-the-other-folder-only.jpg (https://postimg.cc/zbVC21n3)3649



Example 2: Move and rename
Make a folder, anywhere, for example on your desktop https://i.postimg.cc/SQDZbxwk/Make-a-folder-anywhere-for-example-on-your-desktop.jpg
Put two folders in it https://i.postimg.cc/Gt7zFXkH/Put-two-folders-in-it.jpg
Put any file in SubFolder1 https://i.postimg.cc/2y82LsLs/Put-any-file-in-Sub-Folder1.jpg

So now we have a file in SubFolder1 and SubFolder2 is empty https://i.postimg.cc/bNrmyjhb/a-file-in-Sub-Folder1-and-Sub-Folder2-is-empty.jpg

Now run a simple test macro like this

Sub TestWhatVBANameStatementDoes()
Dim strPath As String: Let strPath = "C:\Users\Elston\Desktop\Temporary\" ' --- CHANGE TO SUIT YOUR PATH
Name strPath & "SubFolder1\Neues Textdokument.txt" As strPath & "SubFolder2\New Name.txt"
End Sub

After running that macro you will see that the file is now in the other folder only https://i.postimg.cc/9fjmHhkt/the-file-is-now-in-the-other-folder-only-and-has-a-new-name.jpg and also it has a new name

Before running macro:
https://i.postimg.cc/JGwNxvt4/a-file-in-Sub-Folder1-and-Sub-Folder2-is-empty.jpg (https://postimg.cc/JGwNxvt4)3648



After running macro:
https://i.postimg.cc/NKpqRWB5/the-file-is-now-in-the-other-folder-only-and-has-a-new-name.jpg (https://postimg.cc/NKpqRWB5)3650


Alan