Consulting

Results 1 to 5 of 5

Thread: Using Dir() to return name of all images

  1. #1

    Using Dir() to return name of all images

    Hello,

    I am trying to write a simple macro that will check for any files with the term 'logo' in it, but this code doesn't seem to be working. There is a logo.jpg file in the current directory. Any ideas?

    Sub Logocheck()
    
    Dim ImgName As String
    Dim ImgPath As String
    ImgPath = ThisDocument.Path
    ImgName = Dir(ImgPath & "logo.*")
    MsgBox ImgName
    
    
    End Sub
    The actual application of this is will be to insert any image file with the name 'logo' in it, into the header of the word document.

    This is only posted on this forum.

  2. #2
    Change the line
    ImgPath = ThisDocument.Path
    to
    ImgPath = ThisDocument.Path & "\"
    better still
    Sub Logocheck()
    Dim ImgName As String
    Dim ImgPath As String
        ImgPath = ThisDocument.Path
        ImgName = Dir$(ImgPath & "\*logo*")
        While ImgName <> ""
            MsgBox ImgName
            ImgName = Dir$()
        Wend
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Quote Originally Posted by gmayor View Post
    Change the line
    ImgPath = ThisDocument.Path
    to
    ImgPath = ThisDocument.Path & "\"
    better still
    Sub Logocheck()
    Dim ImgName As String
    Dim ImgPath As String
        ImgPath = ThisDocument.Path
        ImgName = Dir$(ImgPath & "\*logo*")
        While ImgName <> ""
            MsgBox ImgName
            ImgName = Dir$()
        Wend
    End Sub
    Perfect! Thank you for this.

    Works beautifully.

  4. #4
    Not sure if this would require a new thread, but i am trying to parlay this into inserting the found image into the header of my document. I am getting Run-time error '5152' This is not a valid filename on the bolded line below:

    Sub Logocheck()Dim oLogo As InlineShape
    Dim ImgName As String
    Dim ImgPath As String
    Dim oSec As Section
    Dim oHeader As HeaderFooter
        ImgPath = ThisDocument.Path
        ImgName = Dir$(ImgPath & "\*logo*")
        If ImgName = vbNullString Then
            MsgBox "Please note logo image was not found. You will need to manually insert the company logo into your final review."
        End If
        For Each oSec In ActiveDocument.Sections
            Set oHeader = oSec.Headers(wdHeaderFooterPrimary)
            With oHeader.Range
                .ParagraphFormat.Alignment = wdAlignParagraphRight
                Set oLogo = .InlineShapes.AddPicture(ImgName)
            End With
            With oLogo
                .LockAspectRatio = True
                .Height = 50
            End With
        Next oSec
    End Sub

  5. #5
    The issue is similar. ImgPath returns the name without the path. Change the line:
    Set oLogo = .InlineShapes.AddPicture(ImgPath)
    to
    Set oLogo = .InlineShapes.AddPicture(ImgPath & "\" & ImgName)
    also add
    Exit Sub
    after the message box.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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