Results 1 to 11 of 11

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    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]

Posting Permissions

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