Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-25-2013, 08:05 AM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default Insert multiple files


Need to insert multiple txt files into a Word document. Can drag and drop from Explorer, but that just adds the filenames - need the actual text, as would occur with Insert File. Any way to accomplish this? Thanks! ...Steve
Reply With Quote
  #2  
Old 12-25-2013, 05:57 PM
fumei fumei is offline Insert multiple files Windows 7 64bit Insert multiple files Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Need more information.

Multiple text files one after the other?
Multiple text files into already existing locations (maybe with bookmarks)?
Multiple text files in a random order?
Multiple text files in a specific order?
Are the text files all in the same folder?


Try looking up using INCLUDETEXT fields.
Reply With Quote
  #3  
Old 12-25-2013, 06:14 PM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Quote:
Originally Posted by fumei View Post
Multiple text files one after the other?
Yes
Multiple text files in a specific order?
Yes
Are the text files all in the same folder?
Yes
I've been using Insert File (ALT-Y), but it gets tedious with more than a few files. Often need to insert a couple dozen files or more at a time.
Reply With Quote
  #4  
Old 12-25-2013, 07:13 PM
macropod's Avatar
macropod macropod is offline Insert multiple files Windows 7 32bit Insert multiple files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, but you haven't answered any of the key parts of what you were asked: how would an automatic process determine where these inserted files are to come from, in what order they should be inserted and where they should go?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-25-2013, 08:07 PM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Thanks Paul, let me try again - I have a blank page in Word and a folder containing nothing but the text files I want to insert (in alphabetical order). Instead of inserting these files one by one, I want Word to insert all of them, one after another, with a specified number of blank lines between each file. Not sure what other info to provide, but I'd be happy to try answering any questions you might have...
Reply With Quote
  #6  
Old 12-25-2013, 11:46 PM
macropod's Avatar
macropod macropod is offline Insert multiple files Windows 7 32bit Insert multiple files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, you could use a macro like:
Code:
Sub InsertFiles()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.txt", vbNormal)
With Selection
  While strFile <> ""
    .InsertAfter strFile & vbCr
    .InsertFile FileName:=strFolder & "\" & strFile, ConfirmConversions:=False, Link:=False
    .InsertAfter vbCr & vbCr
    .Collapse wdCollapseEnd
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Simply run the macro and select the folder containing the text files. All such files in that folder will be inserted at wherever you selection/insertion point happens to be.

Note: As coded, the macro also inserts the file names. If you don't want that, comment-out or delete the '.InsertAfter strFile & vbCr' line. For the pre-defined number of 'blank lines, simply add or delete ' & vbCr' entries in the '.InsertAfter vbCr & vbCr' line.

For macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-26-2013, 09:15 AM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Thanks Paul, sure appreciate the help! Just one other question

The files will be inserted from the folder C:\2

How do I specify this folder in the macro, so I don't have to browse for it each time?
Reply With Quote
  #8  
Old 12-26-2013, 06:26 PM
fumei fumei is offline Insert multiple files Windows 7 64bit Insert multiple files Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Code:
Sub InsertFiles()
Application.ScreenUpdating = False
Dim strFile As String
strFile = Dir("C:\2\*.txt", vbNormal)
With Selection
While strFile <> ""
.InsertAfter strFile & vbCr
.InsertFile FileName:=strFolder & "\" & strFile, ConfirmConversions:=False, Link:=False
.InsertAfter vbCr & vbCr
.Collapse wdCollapseEnd
strFile = Dir()
Wend
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #9  
Old 12-26-2013, 06:51 PM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Quote:
Originally Posted by fumei View Post
.InsertFile FileName:=strFolder & "\" & strFile, ConfirmConversions:=False, Link:=False
Thanks! Tried this with Test.txt file in \2 folder, got runtime error 5174 (file could not be found): \2Test.txt

Debug highlighted the line quoted here. Need to make some sorta change?

Thanks again!

...Steve
Reply With Quote
  #10  
Old 12-26-2013, 07:04 PM
fumei fumei is offline Insert multiple files Windows 7 64bit Insert multiple files Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Myfault. Needed to get rid of strFolder, as it was not used. BTW: I suspect you are not using Option Explicit as you should have got an error with strFolder to start with.

Code:
Sub InsertFiles()
Application.ScreenUpdating = False
Dim strFile As String
strFile = Dir("C:\2\*.txt", vbNormal)
With Selection
While strFile <> ""
.InsertAfter strFile & vbCr
.InsertFile FileName:=strFile, ConfirmConversions:=False, Link:=False
.InsertAfter vbCr & vbCr
.Collapse wdCollapseEnd
strFile = Dir()
Wend
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #11  
Old 12-26-2013, 10:47 PM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Hate to keep bothering you, still getting 5174 error - now reads: file could not be found: (2Test.txt)

Debug highlighting this line:
.InsertFile FileName:=strFile, ConfirmConversions:=False, Link:=False
Reply With Quote
  #12  
Old 12-27-2013, 02:58 AM
macropod's Avatar
macropod macropod is offline Insert multiple files Windows 7 32bit Insert multiple files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub InsertFiles()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
strFolder = "C:\2\"
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "*.txt", vbNormal)
With Selection
  While strFile <> ""
    .InsertAfter strFile & vbCr
    .InsertFile FileName:=strFolder & strFile, ConfirmConversions:=False, Link:=False
    .InsertAfter vbCr & vbCr
    .Collapse wdCollapseEnd
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End Sub
Note how minimal are the changes to the 'InsertFiles' sub from the code I originally posted.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 12-27-2013, 08:28 AM
weamish weamish is offline Insert multiple files Windows Vista Insert multiple files Office 2007
Advanced Beginner
Insert multiple files
 
Join Date: Dec 2011
Posts: 57
weamish is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Note how minimal are the changes to the 'InsertFiles' sub from the code I originally posted.
Uh-oh, does that mean I shoulda been able to figure this out myself?

Works great, thanks!
Reply With Quote
  #14  
Old 12-27-2013, 08:48 AM
macropod's Avatar
macropod macropod is offline Insert multiple files Windows 7 32bit Insert multiple files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by weamish View Post
Uh-oh, does that mean I shoulda been able to figure this out myself?
Not at all; I have no idea how proficient with VBA coding you might be. I was just trying to show how little the difference can be in the 'InsertFiles' sub between the two approaches.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 12-27-2013, 01:55 PM
fumei fumei is offline Insert multiple files Windows 7 64bit Insert multiple files Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

strFolder = "C:\2\"
If strFolder = "" Then Exit Sub

As strFolder is explicitly given a value prior to the IF statement, the IF statement will never be "".
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert multiple files find files and insert filepaths userman Excel Programming 3 05-11-2012 02:53 PM
Cannot insert audio files unless speakers/headphones are present. HELP! brennj4 PowerPoint 0 01-04-2012 11:05 AM
Macro to Insert Text Into Cells Having Multiple Lines revans611 Excel Programming 4 10-24-2011 10:15 AM
Insert multiple files PPT 2010 will only insert, not link to audio files. kevin3d PowerPoint 1 10-07-2011 09:17 PM
Insert multiple files convert multiple csv files to multiple excel files mit Excel 1 06-14-2011 10:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft