Creating a Large Document using Insert File Link

N

NYSA-HD

I am creating a 2000 page book from several smaller files. I have a
directory where there is a file named for each date the file was created.
For example, 20060102, 20060105, etc.... Where the filenames are YYYYMMDD.
There may not be a file for every date, but all files are named by date. I
would like to avoid master documents because of corruption issues. My
thought was to create a document with my headers and page numbers for each
months worth of files. In each month document I need a macro that would go
out and look to the directory and insert each file in the month directory
into the new file in sequencial order as a linked file. The field codes I am
looking to drop are { INCLUDETEXT "U:\\Project\\20060102.dot"}, etc... so
that the macro would just read the files in the folder and insert the
includetext field for each file in the folder in order. When I look at the
final document for each month it would be all data for all dates, but I could
update the files individually and would automatically update the month
document.

Is anything like this possible? Is there a better way to do such a document
creation?
 
J

Jezebel

1. It would help if you explained why you need to combine the files in the
first place. What will you do with the compound document that you can't do
with the individual files?

2. 2000 pages isn't so massive anyway, unless the files contain a lot of
graphics or complex tables. You could just build an ordinary document.

3. Why are you working with .dot files?
 
N

NYSA-HD

Basically we have a group of inputters who record data that gets written in a
journal for each date a meeting takes place. We have automated their job by
creating .dot files for each form type for the journal entry and have text
files inserted by macros that feed the majority of the templates. When they
open Word we have a form where they enter the date and it grabs the text and
completes the form and asks them any variable questions. It auto saves the
file by date in a network folder. At the end of each year we need to combine
all of these individual files into one document w/ page numbers and common
headers and footers - also a TOC and Index with a title page. We don't see
an easy way to page number w/o combining somehow. The document does not have
graphics...mostly straight text w/ index markers and styles. There are
macros which run our application, but they don't need to be there when it is
combined.

What do you think is the best way to handle this?
Have I provided enough background info.
 
J

Jezebel

1. Your inputters should be using the the .dot files as templates, so the
files you get back should be .doc files. If not, you haven't explained to
them well enough how to install the and use the templates.

2. From your description, simply combining all the docs into one big one
should be fine, particularly as it's just a yearly one-off and presumably
you don't need to do anything with the finished document except print it.
 
N

NYSA-HD

I'm sorry, you were right the outputted files are .doc files named for the
date YYYYMMDD.doc

What is the easiest way to combine them all in date order w/o manually
inserting each file?
 
D

Doug Robbins - Word MVP

If you put all of the documents in a folder by themselves, a macro
containing the following code should insert each of them into a new document
in the date order:

Dim MyPath As String

Dim MyName As String

Dim Source As Document, Target As Document

Dim SourceFile As Range

Dim i As Long

Dim FileList As Document

Set FileList = Documents.Add

'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

MyName = Dir$(MyPath & "*.*")

Do While MyName <> ""

Selection.InsertAfter MyName & vbCr

MyName = Dir

Loop

'Sort the list of files

FileList.Range.Sort SortFieldType:=wdSortFieldAlphanumeric,
FieldNumber:="Paragraphs"

'Delete the empty paragraph that will be at the top of the list of files

FileList.Paragraphs(1).Range.Delete

'Start a new document into which each of the others will be inserted

Set Target = Documents.Add

'Iterate through the list of files, getting the name of each file, opening
it

'and inserting its contents into the Target document

For i = 1 To FileList.Paragraphs.Count

Set SourceFile = FileList.Paragraphs(i).Range

SourceFile.End = SourceFile.End - 1

Set Source = Documents.Open(MyPath & SourceFile.Text)

Target.Range.InsertAfter Source.Range.FormattedText

Source.Close wdDoNotSaveChanges

Next i



Also see the article "Print all documents in a given folder to a single
print file" at:

http://www.word.mvps.org/FAQs/MacrosVBA/PrintAllDocsInFldr.htm




--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

NYSA-HD

This is exactly what I wanted, but I have one issue. The doc files I am
inserting have inherited macros. How can the code below insert the files w/o
the macros or better yet as Links to the original documents? When I run the
code as is all my dialogue boxes from the macros pop up, and I will no longer
need them at this point.

I really appreciate the help with this.
 
D

Doug Robbins - Word MVP

Set the macro security level to High (Tools>Macro>Security), temporarily if
necessary, then the macros in the documents will be ignored.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

NYSA-HD

This solution doesn't work b/c they are all trusted macros. Any other ideas
to change the macro to insert files as linked objects?
 
D

Doug Robbins - Word MVP

OK, I thought it was the macro security warning to which you were referring,
but on re-reading you post see what it is.

See the article "How can I prevent Word from running macros automatically
when I create a new instance of Word, open a Word document or create a new
one?" at:

http://www.word.mvps.org/FAQs/InterDev/DisableAutoMacros.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

NYSA-HD,

Using...
WordBasic.DisableAutoMacros 0
WordBasic.DisableAutoMacros 1
You might be able disable and enable auto macros respectively and still use
Doug's macro without dialogs popping up as each file is opened.
See link below for source of this information.

http://tinyurl.com/fe5ds
 
N

NYSA-HD

I tried the suggestion of Doug's and Russ, but it still ran the macros. I
tried it before the Target command and after. I also tried it at the
beginning under the Dim statements. I think I need to disable all macros in
the documents inserted not just the auto ones. How would I do that?
 
D

Doug Robbins - Word MVP

Just what macros are being run?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

NYSA-HD

Oops my mistake. It is an auto open macro:

Sub AutoOpen()
'StartForm.Show
StartupMacro
End Sub

I did try placing the code you and Russ mentioned but it still tried to run.
Maybe I placed it wrong?
 
D

Doug Robbins - Word MVP

Try putting the WordBasic.DisableAutoMacros 1 before the following line of
code:

Set Source = Documents.Open(MyPath & SourceFile.Text)

I think though that if you place the documents in a Folder location that is
not trusted and you set the Macro Security Level to high, then the macros in
those documents should be ignored without any message being displayed.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

NYSA-HD

I modified the code as noted below adding the disable macros and also my own
line to uprotect each document before inserting. Also a line to close the
filelist document when done. The only thing is now I have a new issue. Each
of my files has hidden text and special formatting. The macro runs and
inserts the files in date order, but when the files are inserted all of the
hidden text is unhidden and all of my formatting is gone. It says insert
formatted text in the macro but that isn't happening. What to do next?
Below is my modified macro I am running at this stage. Maybe using
insertfile instead of document.open? Thanks for all the help.

Sub InsertFilesTest()

Dim MyPath As String

Dim MyName As String

Dim Source As Document, Target As Document

Dim SourceFile As Range

Dim i As Long

Dim FileList As Document

Set FileList = Documents.Add

'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

MyName = Dir$(MyPath & "*.*")

Do While MyName <> ""

Selection.InsertAfter MyName & vbCr

MyName = Dir

Loop

'Sort the list of files

FileList.Range.Sort SortFieldType:=wdSortFieldAlphanumeric,
FieldNumber:="Paragraphs"

'Delete the empty paragraph that will be at the top of the list of files

FileList.Paragraphs(1).Range.Delete

'Start a new document into which each of the others will be inserted

Set Target = Documents.Add

'Iterate through the list of files, getting the name of each file,
'disabling its macros, opening each file, uprotecting it
'and and inserting its contents into the Target document

For i = 1 To FileList.Paragraphs.Count

Set SourceFile = FileList.Paragraphs(i).Range

SourceFile.End = SourceFile.End - 1

WordBasic.DisableAutoMacros 1

Set Source = Documents.Open(MyPath & SourceFile.Text)

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

Target.Range.InsertAfter Source.Range.FormattedText

Source.Close wdDoNotSaveChanges

Next i
FileList.Close wdDoNotSaveChanges
End Sub
-----------------------------------------------------------
 
R

Russ

FYI,
NYSA-HD,

Using...
WordBasic.DisableAutoMacros 0
WordBasic.DisableAutoMacros 1

The message link I referenced has confusing sentence structure. So to be
clear:
WordBasic.DisableAutoMacros 1 ==== Disables Auto Macros
WordBasic.DisableAutoMacros 0 ==== Enables Auto Macros
 
R

Russ

NYSA-HD,
See a code alteration below.
I modified the code as noted below adding the disable macros and also my own
line to uprotect each document before inserting. Also a line to close the
filelist document when done. The only thing is now I have a new issue. Each
of my files has hidden text and special formatting. The macro runs and
inserts the files in date order, but when the files are inserted all of the
hidden text is unhidden and all of my formatting is gone. It says insert
formatted text in the macro but that isn't happening. What to do next?
Below is my modified macro I am running at this stage. Maybe using
insertfile instead of document.open? Thanks for all the help.

Sub InsertFilesTest()

Dim MyPath As String

Dim MyName As String

Dim Source As Document, Target As Document

Dim SourceFile As Range

Dim i As Long

Dim FileList As Document

Set FileList = Documents.Add

'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

MyName = Dir$(MyPath & "*.*")

Do While MyName <> ""

Selection.InsertAfter MyName & vbCr

MyName = Dir

Loop

'Sort the list of files

FileList.Range.Sort SortFieldType:=wdSortFieldAlphanumeric,
FieldNumber:="Paragraphs"

'Delete the empty paragraph that will be at the top of the list of files

FileList.Paragraphs(1).Range.Delete

'Start a new document into which each of the others will be inserted

Set Target = Documents.Add

'Iterate through the list of files, getting the name of each file,
'disabling its macros, opening each file, uprotecting it
'and and inserting its contents into the Target document

For i = 1 To FileList.Paragraphs.Count

Set SourceFile = FileList.Paragraphs(i).Range

SourceFile.End = SourceFile.End - 1

WordBasic.DisableAutoMacros 1

Set Source = Documents.Open(MyPath & SourceFile.Text)

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

Target.Range.InsertAfter Source.Range.FormattedText
Replace the InsertAfter line above; try:
Target.Range.Collapse Direction:=wdCollapseEnd
Target.Range.FormattedText = Source.Range.FormattedText

The example in VBA shows a similar pattern with the left side of equation
(container?) also as FormattedText. But doesn't seem to work with
InsertAfter.
 
N

NYSA-HD

NO- This just seems to open everything up and create a new file w/ just the
last file inserted. So far, we like the following code, but the issue we
need to resolve is having it display the entire file if the link spans more
than one page. Any ideas? If the file goes longer than one page it just
inserts the view of the first page.

Sub InsertFilesTest()
Dim MyPath As String
Dim MyName As String
Dim Source As Document, Target As Document
Dim SourceFile As Range
Dim i As Long
Dim FileList As Document
Set FileList = Documents.Add
'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
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Selection.InsertAfter MyName & vbCr
MyName = Dir
Loop
'Sort the list of files
FileList.Range.Sort SortFieldType:=wdSortFieldAlphanumeric,
FieldNumber:="Paragraphs"
'Delete the empty paragraph that will be at the top of the list of files
FileList.Paragraphs(1).Range.Delete
'Start a new document into which each of the others will be inserted
Set Target = Documents.Add
'Iterate through the list of files, getting the name of each file,
'disabling its macros, opening each file, uprotecting it
'and and inserting its contents into the Target document as a link
For i = 1 To FileList.Paragraphs.Count
Set SourceFile = FileList.Paragraphs(i).Range
SourceFile.End = SourceFile.End - 1
WordBasic.DisableAutoMacros 1
Selection.EndKey unit:=wdStory
Selection.InlineShapes.AddOLEObject ClassType:="Word.Document.8",
FileName:=MyPath & SourceFile.Text, LinkToFile:=True
Next i
'Close FileList Document
FileList.Close wdDoNotSaveChanges
End Sub
 
R

Russ

NYSA-HD,
NO- This just seems to open everything up and create a new file w/ just the
last file inserted.
....You said "The macro runs and inserts the files in date order, but when
the files are inserted all of the hidden text is unhidden and all of my
formatting is gone. It says insert formatted text in the macro but that
isn't happening. What to do next? Below is my modified macro I am running
at this stage."
....And I suggested replacing one line with two to get the formatting.
My first line should have done what '.InsertAfter' implies by collapsing to
the end of the target file and the second line should have brought in new
*formatted* text at the end of the file during each iteration of the loop.
Maybe you didn't use my collapse line in the macro that you were 'running at
that stage.'
 

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