Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

macro to add wildcard file names to a Word file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry K - 13 Nov 2007 17:35 GMT
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!
Signature

Larry K

Jay Freedman - 13 Nov 2007 18:20 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Does anyone know how to make a macro that will add multiple files to
> an opened Word file? Here is the example:
[quoted text clipped - 17 lines]
>
> Thanks for any advice and help with syntax and possibilities!
Larry K - 13 Nov 2007 19:06 GMT
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

Signature

Larry K

> Modify the macro at
> http://www.word.mvps.org/FAQs/MacrosVBA/InsertFileNames.htm. Replace the
[quoted text clipped - 43 lines]
> >
> > Thanks for any advice and help with syntax and possibilities!
Jay Freedman - 13 Nov 2007 19:19 GMT
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?

> 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
[quoted text clipped - 89 lines]
>>>
>>> Thanks for any advice and help with syntax and possibilities!
Larry K - 13 Nov 2007 20:04 GMT
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.
Signature

Larry K

> The code itself is correct -- when I copied it from your message and pasted
> it into my VBA editor, it ran properly.
[quoted text clipped - 96 lines]
> >>>
> >>> Thanks for any advice and help with syntax and possibilities!
Jay Freedman - 13 Nov 2007 20:17 GMT
Can you run any macros at all? Follow the instructions at
http://www.gmayor.com/installing_macro.htm to see whether you can get the
example there to run.

> 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
[quoted text clipped - 101 lines]
>>>>>
>>>>> Thanks for any advice and help with syntax and possibilities!
Larry K - 14 Nov 2007 19:47 GMT
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,
Signature

Larry K

> Can you run any macros at all? Follow the instructions at
> http://www.gmayor.com/installing_macro.htm to see whether you can get the
[quoted text clipped - 105 lines]
> >>>>>
> >>>>> Thanks for any advice and help with syntax and possibilities!
Jay Freedman - 14 Nov 2007 21:34 GMT
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.

> 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
[quoted text clipped - 113 lines]
>>>>>>>
>>>>>>> Thanks for any advice and help with syntax and possibilities!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.