Consulting

Results 1 to 6 of 6

Thread: VBA to insert image in current directory regardless of image type

  1. #1

    VBA to insert image in current directory regardless of image type

    Hello,

    I am writing a macro that will dynamically insert any image with the word "logo" from the current directory. It is then inserted into the header of each page.

    The code below works if the file is named logo.jpg, but I would like to add logic to include it if it is logo.png, logo.gif, logo.JPG etc.

    Any tips?

    
    Sub DSSHeaderImage()
    
    
    Dim oSec As Section
    Dim oHeader As HeaderFooter
    Dim oLogo As InlineShape
    Dim imgpath As String
    
        imgpath = ThisDocument.Path
        imgpath = imgpath & "\" & "logo.jpg"
    
    
        For Each oSec In ActiveDocument.Sections
            Set oHeader = oSec.Headers(wdHeaderFooterPrimary)
            With oHeader.Range
                .ParagraphFormat.Alignment = wdAlignParagraphRight
                Set oLogo = .InlineShapes.AddPicture(imgpath)
            End With
        Next oSec
    End Sub
    This question only posted here. Thank you

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    And what if there's more than one file whose name is 'logo'? In any event, your code is seriously flawed it will result in multiple images in the primary header where you have linked Sections. Plus, what about documents that have a 'different first page' and/or 'different odd and even' setup?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Quote Originally Posted by macropod View Post
    And what if there's more than one file whose name is 'logo'? In any event, your code is seriously flawed it will result in multiple images in the primary header where you have linked Sections. Plus, what about documents that have a 'different first page' and/or 'different odd and even' setup?
    Thank you for the reply.

    Ultimately we would coach our teams to only having one image in the directory. Without getting overly complicated, our team works a new "case" with each customer which means a new directory for each customer. So ultimately the only three files that will exist in their current directory would be

    - Excel document that serves as a note taking document
    - Word document that populates content based on the information from the excel sheet
    - A logo file named logo.(any relevant image extension).

    So essentially I want to have code that does something like:

    IF logo.png exists in current directory, insert into headers
    ELSE IF logo.jpg exists in current directory, insert into headers
    ELSE IF logo.gif exists in current directory, insert into headers
    ELSE msgbox "No logo files found in current directory"

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case, use the Dir function to test for files named 'logo.*' and go from there. See, for example: http://www.vbaexpress.com/forum/show...l=1#post308901
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Good morning macropod.

    I gave it a try and came up with this, but I am not even getting a message box pop up.

    Also, logo.jpg does exist in the word doc directory.

    Function FileThere(FileName As String) As Boolean
        FileName = ThisDocument.Path
        FileName = FileName & "\" & "logo.*"
        
        FileThere = (Dir(FileName) > "")
        
        If FileThere(FileName) Then
            MsgBox "Image found!"
        Else
            MsgBox "File Not There!"
        End If
    End Function

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Your code and how you're using it is amystery to me.

    Why are you passing 'FileName' to the function, then ignoring that and substituting ThisDocument.Path? And did you test what Dir(FileName) actually returns?

    In any event, since the code presumably resides in a template, ThisDocument.Path would return the template's path, not the document's - and you couldn't use ActiveDocument.Path until the document has been saved. Moreover, I can't see that a Function that merely returns a boolean result is of any benefit.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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