Consulting

Results 1 to 11 of 11

Thread: Search Folder and Subfolders for image and paste image.

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location

    Search Folder and Subfolders for image and paste image.

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Looping through subfolders is a considerably more complex undertaking. For some code you could adapt, see:
    http://www.msofficeforums.com/word-v...html#post61163
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location
    Quote Originally Posted by macropod View Post
    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.

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    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

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Amazing.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Amazing. Probably not helpful for the OP, but amazing.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Last edited by macropod; 08-17-2014 at 06:11 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

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