Log in

View Full Version : Difference btw Selection.InsertFile and Selection.Copy



GRCNC
11-01-2016, 02:39 PM
Hi - I'm trying to put together a macro that will open up several files and consolidate them all into 1 file. The macro I'm needing needs to open up the files and bring over an exact copy of the source file. (IE: Layout, formatting, etc needs to stay the same). Additionally, all of the files are 'locked for editing', with certain sections of the file editable and certain sections locked. I've got a great macro (provided by this forum :)) that is bringing over the files into the 1 document. It is working in that: the 'locked for editing' sections are working correctly. However, the layout is not coming over 100% exactly the same. Same sections have double spacing (it shouldn't) thus, sections are being pushed onto an additional page. Here is the code I'm using for this:



Private Sub cmdOK_Click()
'''this macro is working correctly in that the correct areas are locked for editing.
'''this macro is NOT working correctly in that the layout is not coming over 100%.


Dim docProposal As String
docProposal = ActiveDocument.Name

Dim strPath As String
Dim oRng As Range

'Opens the File and inserts the entire document at the current selection
Selection.InsertFile FileName:=strPath & "RFK%20New%20Creator/File%20Intro.docx"
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.InsertParagraphAfter
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.Collapse direction:=wdCollapseEnd

End Sub


So, I tried to change up the code to bring over the formatting correctly. The code below does the exact opposite of the code above. It is bringing over the layout 100% correct, but the entire document is 'locked for editing'. (IE: the ranges that should be available to edit - and is working correctly in the code above - is not working here and locked). Here is the code:


Private Sub cmdOK_Click()
'''this macro is working correctly with bringing over the layout.
'''this macro is NOT working correctly in that the whole document is locked for editing.

Dim docProposal As String
docProposal = ActiveDocument.Name

Dim strPath As String
Dim oRng As Range

'Opens the File and inserts the entire document at the current selection

Documents.Open FileName:=strPath & "RFK%20New%20Creator/File%20Intro.docx", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""


Selection.WholeStory
Selection.Copy
Windows("Blank New Proposal.docm").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.InsertParagraphAfter
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.Collapse direction:=wdCollapseEnd

End Sub


anybody have any idea what would be causing this? Any advice on how to get both parts working correctly? Not sure why Selection.InserFile would change the layout.

Any help would be GREATLY APPRECIATED. This is driving me crazy...:banghead:

thanks,
GRCNC

gmayor
11-02-2016, 02:13 AM
Without knowing what is in the documents and how they are formatted differently from one another, it is difficult to guess what the formatting changes entail. However, if you insert a file into a document that is partially protected, the inserted file (using your macro) will be inserted at the end of the unprotected section the cursor is in. Any protected documents you insert will lose their protection, so you could end up with part of the first document protected, but positioned after the inserted documents.

The only way to do this, and hope that it works as intended, is to unprotect the first document, insert the subsequent documents at the end, each separated by section breaks (the insertion will remove the protection on the inserted documents), and then reprotect the new sections that need to be protected.

Even then you have the potential disparity between documents produced using different templates to consider.

If you unprotect the first document, http://www.gmayor.com/Boiler.htm will insert the documents at the end separated by section breaks, but it will not reprotect as it does not take account of which sections need to be protected. With disparate documents this is as good as it gets.

If you are always inserting the same set of documents, with the same number of sections, it shouldn't be too difficult to create a macro to protect the relevant parts of the combined document.