section add - insert file

B

bryan

I have tried this code and it works well if I select one checkbox. If I
select more than 1, it is not working correctly.
My template is a letterhead (top and bottom) as are my documents.
When I check box one, it adds temp2.doc correctly.
When I check box 2, it adds a blank letterhead after my template page, then
adds
temp3.doc as a continuation of temp2.doc rather than starting a new page.
I have in chk2 named myDoc1 and docrange1 but, that did not help
Here is the code for each check box:
Sub chk1()
'
' chk1 Macro
' Macro created 4/10/2008 by bjsorens
'
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
Dim myDoc As Document
Dim docrange As Range

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = Selection.Bookmarks("\Page").Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdSectionBreakNextPage
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "u:\temp2.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With
End If
End Sub

Sub chk2()
'
' chk1 Macro
' Macro created 4/10/2008 by bjsorens
'
If ActiveDocument.FormFields("Check2").CheckBox.Value = True Then
Dim myDoc As Document
Dim docrange As Range

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = Selection.Bookmarks("\Page").Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdSectionBreakNextPage
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "u:\temp3.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With
End If
End Sub

Would appreciate any help so that by checking more than 1 box, the next file
(document) added would append after the last.

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

If you always want the documents added at the end of the document in a new
section, use:

If ActiveDocument.FormFields("Check2").CheckBox.Value = True Then
Dim myDoc As Document
Dim docrange As Range

Set myDoc = ActiveDocument
With myDoc
.Unprotect
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertBreak wdSectionBreakNextPage
Set docrange = .Range
docrange.Collapse wdCollapseEnd
docrange.InsertFile "u:\temp3.doc"
.Protect wdAllowOnlyFormFields, NoReset
End With
End If


--
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
 
B

bryan

Thanks Doug,
Will this work no matter how many check boxes I have?
If have 5 different check boxes with the potential of including 5 different
documents they will append at the bottom?
In other words:
For each check box I use the same code as described here for check2?

Another question on this topic:
Let's say they inadvertantly check box 1 and it adds the document, they
uncheck box 1, is there then a way to delete?

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

It should work for any number of checkboxes, except that you are doing to
need to supply the name of the file to insert each time.

An immediate undo of the result of clicking a checkbox would be possible by
deleting the last Section of the document. It will be a tad more
complicated if another checkbox had been checked before the undo of the
action of checking a previous one is required.

--
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
 
B

bryan

Thanks Doug,
This worked for adding multiple documents.
I've been doing a lot of scripting of macros within word and find the forum
very helpful of aspects of Word that I am not familiar with.
What is a good source for learning the nuances of Word?

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

Dim rnge As Range
Set rnge = ActiveDocument.Sections.Last.Range
rnge.start = rnge.start - 1
rnge.Delete


--
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
 
B

bryan

On the protected form they could add multiple documents so in order to remove
one in the middle it would probably be best to have a "Refresh" macro button
on the form to remove all added documents and start over.
How would I remove "All" ? Would it be just removing the .Last ?

Also, Where is a good source for learning Word programming?

Thanks again,
Bryan
 
D

Doug Robbins - Word MVP

That would only delete the last document added, but you could use code to
count the number of check boxes that had been checked and then run code to
delete the last section that number of times.

Instead of re-inventing the wheel however, you may want to consider the
Boiler Plate Features of DataPrompter available from
http://www.wordsite.com/

--
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
 

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