using wildcard in file path

E

ephelps

i'm using office 2003 and trying to paste an image from a file. The
actual image name is constant, "theone.jpg" but the actual subfolder
is different and is something like "c:\documents\data\232 - 123 Sesame
St\pictures\." The name of the subfolder "232 - 123 Sesame St" is
variable. I want to be able to insert the picture using a wild card
such as "c:\documents\data\232 * \pictures\" but that returns in
error. What is the correct way to insert a wild card in the file path?

Here is the code I have so far that isn't working:
Sub Macro1()

Dim mypath As String
Dim filename As String
Dim fullpath As String

filename = "theone.jpg"
mypath = "c:\documents\data\232 * \pictures\"
MsgBox (mypath)
Selection.InlineShapes.AddPicture (mypath & filename)
End Sub
 
J

Jay Freedman

i'm using office 2003 and trying to paste an image from a file. The
actual image name is constant, "theone.jpg" but the actual subfolder
is different and is something like "c:\documents\data\232 - 123 Sesame
St\pictures\." The name of the subfolder "232 - 123 Sesame St" is
variable. I want to be able to insert the picture using a wild card
such as "c:\documents\data\232 * \pictures\" but that returns in
error. What is the correct way to insert a wild card in the file path?

Here is the code I have so far that isn't working:
Sub Macro1()

Dim mypath As String
Dim filename As String
Dim fullpath As String

filename = "theone.jpg"
mypath = "c:\documents\data\232 * \pictures\"
MsgBox (mypath)
Selection.InlineShapes.AddPicture (mypath & filename)
End Sub

There is *no* way to use a wildcard in the path of AddPicture. Every
time you call it, you *must* supply a complete valid path. Your macro
needs some way to get that path, either by asking the user to supply
the path (or browse for the file) or by searching the disk for a file
whose name matches the filename variable.

If you want to use the browse option, look at how this works:

Sub demo()
Dim dlg As Dialog
Dim FileName As String
Dim FilePath As String
Dim FullPathAndName As String
Dim done As Boolean

FileName = "theone.jpg"
done = False

Set dlg = Dialogs(wdDialogInsertPicture)
With dlg
.Name = FileName
Do
If .Display <> -1 Then
' user canceled
done = True
Else
FilePath = WordBasic.FilenameInfo$(.Name, 5)
FullPathAndName = FilePath & FileName
If Len(Dir(FullPathAndName)) > 0 Then
' browse was successful
done = True
End If
End If
Loop Until done
End With

If Len(FullPathAndName) > 0 Then
On Error Resume Next
Selection.InlineShapes.AddPicture (FullPathAndName)
End If
End Sub

For information about using built-in dialogs, see
http://www.word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm.

For information about the FilenameInfo function, see
http://www.word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top