View Full Version : VBA to insert image in current directory regardless of image type
noslenwerd
12-23-2019, 12:29 PM
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
macropod
12-23-2019, 01:01 PM
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?
noslenwerd
12-23-2019, 01:37 PM
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"
macropod
12-23-2019, 03:00 PM
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/showthread.php?49539-Macro-to-modify-header-in-multiple-word-documents&p=308901&viewfull=1#post308901
noslenwerd
01-10-2020, 09:08 AM
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
macropod
01-10-2020, 02:26 PM
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.