View Full Version : Search Folder and Subfolders for image and paste image.
l0aded
08-15-2014, 12:54 PM
I currently have a macro used to copy the first "word" of a line, search a specific directory for that "word".jpg and paste it in the spot. The code is fairly primitive and has to be exact and will not search the folders sub-folders for that "word". Is it possible using VBA macros to search the sub-folders?
My current code is the following, it will search the specified directory for whatever "VARIABLEcounter" is set to and paste that image.
InsertImage Trim(counter)
and
Sub InsertImage(VARIABLEcounter)
On Error Resume Next
Selection.InlineShapes.AddPicture FileName:= _
"C:\Users\***Picture Test\" & VARIABLEcounter & ".jpg", LinkToFile:=False, _
SaveWithDocument:=True
End Sub
Thanks
macropod
08-15-2014, 03:55 PM
Looping through subfolders is a considerably more complex undertaking. For some code you could adapt, see:
http://www.msofficeforums.com/word-vba/20289-word-macro-copy-different-tables-into-one.html#post61163
l0aded
08-15-2014, 04:27 PM
Looping through subfolders is a considerably more complex undertaking. For some code you could adapt, see:
Thanks for putting me in the right direction. The code is definitely more complex than I expected haha. I'll try to dissect it and see if I can put it to use.
gmayor
08-16-2014, 04:07 AM
You can download an add-in from my web site that will will handle the folder processing, and can be used with a custom process. The custom process macro needs to be in a specific format, and the macro you have quoted is not yet up to that task, but it could be modified - though the variable value would have to be better described.
A oneliner suffices:
Sub M_snb()
c00="Sydney is the town; its image has to be loaded from the (sub)folders"
InsertImage split(c00)(0))
End Sub
Sub InsertImage(c01)
Selection.InlineShapes.AddPicture split(createobject("wscript.shell").exec("cmd /c Dir ""C:\Users\***Picture Test\" & c01 & ".jpg"" /b/s/a"),stdout.readall,vbCrLf)(0), False, True
End Sub
fumei
08-16-2014, 11:02 PM
Amazing.
fumei
08-16-2014, 11:05 PM
Amazing. Probably not helpful for the OP, but amazing.
macropod
08-17-2014, 05:59 PM
What would have been even nicer is if it actually worked. But it doesn't. The code crashes because it's poorly written. And, as usual, there's no variable declarations or error-checking - so it would crash anyway if Option Explicit is used or the file isn't found.
Try:
Sub InsertImage()
Dim StrImg As String, ObjWSH As Object, StrFile As String
StrImg = Split(Trim(ActiveDocument.Paragraphs.First.Range.Text), " ")(0)
Set ObjWSH = CreateObject("wscript.shell")
StrFile = ObjWSH.Exec("Cmd /c Dir ""C:\Users\%UserName%\'Balance of path to top-most folder'\" & StrImg & ".jpg"" /B/S").StdOut.ReadAll
If UBound(Split(StrFile, vbCrLf)) > 0 Then Selection.InlineShapes.AddPicture Split(StrFile, vbCrLf)(0), False, True
End Sub
Replace 'Balance of path to top-most folder' as appropriate. The 'C:\Users\%UserName%\' part of the expression gets the user's home folder.
fumei
08-17-2014, 07:55 PM
Like I said...not helpful as usual. snb, what on earth do you get out of those posts? I mean, as they generally cause no satisfaction for anyone else, I hope that at least you get something from them.
macropod
08-17-2014, 08:14 PM
Even his one-liners can only be considered that for very large values of one...
Here's a true on-line version, including error checking, but I don't know why anyone would bother with such code (unless the name's snb, but then there'd be no error-checking):
Sub InsertImage()
If UBound(Split(CreateObject("wscript.shell").Exec("Cmd /c Dir ""C:\Users\%UserName%\Documents\Attachments\" & Split(Trim(ActiveDocument.Paragraphs.First.Range.Text), " ")(0) & ".jpg"" /B/S").StdOut.ReadAll, vbCrLf)) > 0 Then Selection.InlineShapes.AddPicture Split(CreateObject("wscript.shell").Exec("Cmd /c Dir ""C:\Users\%UserName%\Documents\Attachments\" & Split(Trim(ActiveDocument.Paragraphs.First.Range.Text), " ")(0) & ".jpg"" /B/S").StdOut.ReadAll, vbCrLf)(0), False, True
End Sub
PS: The line's so long the code tags here can't handle it properly...
fumei
08-18-2014, 11:51 PM
Ooo, ooo, my side is hurting from laughing. Very large values of one?
l0aded, please ignore the little sidebar that macropod and I inserted into the thread...and you can safely ignore just about anything that snb posts (although admittedly not everything). Hopefully you get something from the thread that works for you.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.