Insert documents from folder

A

Adam

Hello,

I'm "attempting" to make some code to insert different file types into a word doc then save it.

I have a folder that contains many .txt, .tif, .doc and .xls files.

I have been using the following (strFolderName being the user selected folder with the files ):-

Private Sub Document_Open()
Dim strFolderName As String
strFolderName = BrowseFolder("Select the folder you files are in?") & "\"

ChangeFileOpenDirectory strFolderName
Selection.InsertFile FileName:="textdoc.txt", Range:="", ConfirmConversions:=False, link:=False, attachment:=False
Selection.InlineShapes.AddPicture FileName:=strFolderName & "picture.tif", linktofile:=False, SaveWithDocument:=True
Selection.InsertFile FileName:=strFolderName & "word.doc", Range:="", ConfirmConversions:=False _
, link:=False, attachment:=False

Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "excel.xls", linktofile:=False, DisplayAsIcon:=False
Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "otherdoc.xls", linktofile:=False, DisplayAsIcon:=False
Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "another.xls", linktofile:=False, DisplayAsIcon:=False

Activedocument.Saveas Filename:="NEWDOC.doc", FileFormat:=wdFormatDocument _
, LockComments:=false, password:=""

End Sub

However, does anyone know some code I can use if I don't know the names of the files, and to automatically add all the files from the directory, in the order of txt files, tif files, doc files then xls files.


Thanks in advance for any help.
 
J

Jonathan West

Hi Adam,

These articles contain code you can adapt.

Find & ReplaceAll on a batch of documents in the same folder
http://word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

How to read the filenames of all the files in a directory into an
array
http://word.mvps.org/FAQs/MacrosVBA/ReadFilesIntoArray.htm

Getting access to the Document Properties of a Word file
http://word.mvps.org/FAQs/MacrosVBA/DSOFile.htm


--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup


Adam said:
Hello,

I'm "attempting" to make some code to insert different file types into a word doc then save it.

I have a folder that contains many .txt, .tif, .doc and .xls files.

I have been using the following (strFolderName being the user selected folder with the files ):-

Private Sub Document_Open()
Dim strFolderName As String
strFolderName = BrowseFolder("Select the folder you files are in?") & "\"

ChangeFileOpenDirectory strFolderName
Selection.InsertFile FileName:="textdoc.txt", Range:="",
ConfirmConversions:=False, link:=False, attachment:=False
Selection.InlineShapes.AddPicture FileName:=strFolderName & "picture.tif",
linktofile:=False, SaveWithDocument:=True
Selection.InsertFile FileName:=strFolderName & "word.doc", Range:="", ConfirmConversions:=False _
, link:=False, attachment:=False

Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "excel.xls", linktofile:=False, DisplayAsIcon:=False
Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "otherdoc.xls", linktofile:=False, DisplayAsIcon:=False
Selection.InlineShapes.AddOLEObject ClassType:="Excel.sheet.8", FileName:=strFolderName _
& "another.xls", linktofile:=False, DisplayAsIcon:=False

Activedocument.Saveas Filename:="NEWDOC.doc", FileFormat:=wdFormatDocument _
, LockComments:=false, password:=""

End Sub

However, does anyone know some code I can use if I don't know the names of
the files, and to automatically add all the files from the directory, in
the order of txt files, tif files, doc files then xls files.
 
A

Adam

Thanks for the help

I've managed to get this far, but I cannot work out how to repeat the array with different file types without getting errors, i've read all the other threads but they don't refer to inserting different file types. Help... Thank

Private Sub Document_Open(

Dim strFolderName As Strin
strFolderName = BrowseFolder("Select the folder your files are in?"

Dim MyFile As Strin
Dim Counter As Lon
Dim DirectoryListArray() As Strin
ReDim DirectoryListArray(1000

MyFile = Dir$(strFolderName & "\*.txt"
Do While MyFile <> "
DirectoryListArray(Counter) = MyFil
MyFile = Dir
Counter = Counter +
Loo

ReDim Preserve DirectoryListArray(Counter - 1

For Counter = 0 To UBound(DirectoryListArray
Debug.Print DirectoryListArray(Counter

ChangeFileOpenDirectory strFolderNam
Selection.TypeParagrap
Selection.InsertFile FileName:=DirectoryListArray(Counter), Range:="", ConfirmConversions:=False, link:=False, attachment:=Fals
Next Counte

End Sub
 
D

Dave Lett

Hi Adam

I tried to answer this last week, but my response never posted. Anyway, you might try something like the following

Dim strFolderName As Strin
strFolderName = BrowseFolder("Select the folder your files are in?"

Dim MyFile As Strin
Dim Counter As Lon
Dim DirectoryListArray() As Strin
ReDim DirectoryListArray(1000
Dim sArra
Dim iArray As Intege
''' populate sArray with the extensions of the file
''' you want to insert in the order you want to insert the
sArray = Array("\*.txt", "\*.doc", "\*.jpg", "\*.xls"

''' cycle through the array of file extension
For iArray = 0 To UBound(sArray
MyFile = Dir$(strFolderName & sArray(iArray)
Do While MyFile <> "
DirectoryListArray(Counter) = MyFil
MyFile = Dir
Counter = Counter +
Loo
ReDim Preserve DirectoryListArray(Counter - 1
For Counter = 0 To UBound(DirectoryListArray
Debug.Print DirectoryListArray(Counter
ChangeFileOpenDirectory strFolderNam
Selection.TypeParagrap
Selection.InsertFile FileName:=DirectoryListArray(Counter), Range:="", ConfirmConversions:=False, link:=False, attachment:=Fals
Next Counte
Next iArra

HTH
Dave
 

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