Macro Programming

1

148steve

Can anyone help????

I am trying to create a macro that will look at a specific folder.

I can record the macro to the required folder, but it wants me to select a
file as part of the macro.

I want to be able to allow the selection of any file within the folder by
using the macros assigned to a custom toolbar.....

ALL HELP WOULD BE GRATEFULLY RECEIVED
 
D

Doug Robbins - Word MVP

Use

Dim fd As FileDialog
Dim myfile As String
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Title = "Select the file."
fd.AllowMultiSelect = False
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
myfile = .SelectedItems(1)
End If
End With

myfile will then contain the path\filename of the selected file
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
1

148steve

Hi Doug

Thanks for your response.

DO i just copy this into my attempted macro or create it as a new one???

I have tried the copy and paste but i still cant get it to work??

Steve
 
D

Doug Robbins - Word MVP

What version of Word are you using? It will not work with 2000.

It should work if you use it to replace everything BETWEEN the Sub
macroname() and End Sub of what you had created.

See the article "What do I do with macros sent to me by other newsgroup
readers to help me out?" at:

http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm

What have you done with myfile? If you put a command

Msgbox myfile

after the End With, a message box will be displayed that contains the
path\filename of the selected file.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
1

148steve

Hi Doug

I did what you said, but it still looks at the wrong folder. If i replace
the "myfile" with the locatin path of the required folder, the text turn RED.

I have copied the text below for you to have a look at if you have time
please and let me know where i am going wrong.

Sub insert_picture()

'insert_picture macro
'Macro recorded 03/10/08 by Steve Craig

Dim fd As FileDialog
Hope this helps???

Steve
 
D

Doug Robbins - Word MVP

What version of Word are you using. That code will only work with Word XP
and later. If I run the following here

Sub a()
Dim fd As FileDialog
Dim myfile As String
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Title = "Select the file."
fd.AllowMultiSelect = False
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
myfile = .SelectedItems(1)
End If
End With
MsgBox myfile
End Sub

A message box appears that displays the path and filename of the file that I
select.

If you are using Word 97 or 2000, use:

Dim logofile As String
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then
logofile = "Cancelled by User"
Else
logofile = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
MsgBox logofile

That code will work with any version form Word97 onwards.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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