#1
|
|||
|
|||
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
|
#2
|
|||
|
|||
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. |
#3
|
|||
|
|||
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.
|
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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...
|
#6
|
||||
|
||||
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 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] |
#7
|
|||
|
|||
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? |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
Quote:
Debug highlighted the line quoted here. Need to make some sorta change? Thanks again! ...Steve |
#10
|
|||
|
|||
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 |
#11
|
|||
|
|||
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 |
#12
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Quote:
Works great, thanks! |
#14
|
||||
|
||||
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] |
#15
|
|||
|
|||
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 "". |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
PPT 2010 will only insert, not link to audio files. | kevin3d | PowerPoint | 1 | 10-07-2011 09:17 PM |
convert multiple csv files to multiple excel files | mit | Excel | 1 | 06-14-2011 10:15 AM |