macro to add wildcard file names to a Word file

L

Larry K

Does anyone know how to make a macro that will add multiple files to an
opened Word file? Here is the example:
In an open file I want to add the following files to it using a macro. The
files are:
1a.doc
1b.doc
1c.doc
2.doc and so forth.
I would like the macro to add files named 1*.doc, 2*.doc, etc. to the opened
file.
When I try to use a wildcard file name instead of listing all individual
file names then the macro will not work. If I list each file name then it
works fine.

The questions are (1) can Word handle wildcards in macros or (2) if not, how
could I construct a conditional macro to do the same thing?

A conditional macro would go something like: If 1a.doc exists then add it.
If 1b.doc exists then add it. etc.

Thanks for any advice and help with syntax and possibilities!
 
J

Jay Freedman

Modify the macro at
http://www.word.mvps.org/FAQs/MacrosVBA/InsertFileNames.htm. Replace the
lines

MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop

with these lines:

For LeadingDigit = 1 To 9
MyName = Dir$(MyPath & LeadingDigit & "*.doc")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop
Next LeadingDigit

and add the line

Dim LeadingDigit As Integer

at the top with the other Dim statements.

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

Larry K

Thank you Jay. Below is the modified macro but it will not run. It opens the
debug file and points to the first line. Even if I fill in the brackets of
the first line with a folder name it does not work. Suggestions?
----------------------
Sub InsertNamesOfFilesInAFolder()

Dim MyPath As String
Dim MyName As String
Dim LeadingDigit As Integer

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

'get files from the selected path
'and insert them into the doc
For LeadingDigit = 1 To 9
MyName = Dir$(MyPath & LeadingDigit & "*.doc")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop
Next LeadingDigit

'collapse the selection
Selection.Collapse wdCollapseEnd

End Sub
 
J

Jay Freedman

The code itself is correct -- when I copied it from your message and pasted
it into my VBA editor, it ran properly.

Is the row of hyphens actually in your code? If so, remove it. Otherwise,
exactly what is the text of the error message that appears when you try to
run the macro?
 
L

Larry K

It doesn't show an error code. It opens the Microsoft Visual Basic file and
has a yellow error pointing to the first line of the code which is the sub
name. No the line of hypnens is not in the code.
 
L

Larry K

No, Jay I couldn't get the example at the website to run although my own
macros that are recorded run just fine. Actually, I couldn't tell what it was
supposed to do - I say it didn't run but what I mean is that it didn't do
anything that I could detect and I couldn't tell from looking at the code
what it is designed to do. Can you tell me what it is supposed to do and then
maybe I can detect if it did that. Thanks,
 
J

Jay Freedman

That macro is designed to update any and all fields that are in the active
document. If there aren't any fields, then it won't do anything visible.
Still, you didn't get an error message, so that means the machinery inside
Word that handles macros probably isn't broken or misconfigured.

Other than such a misconfiguration, I can't think of any reason you would
get the VBA editor opening and highlighting a perfectly good line of code.
Possibly if you start with a new template and follow the instructions in
Graham's page about pasting in the code, using the macro from
http://www.word.mvps.org/FAQs/MacrosVBA/InsertFileNames.htm, you'll get a
better result this time.
 

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