vba When inserting file, margins in new section di

2019-08-17 08:46发布

问题:

I want to insert DocB' into DocA using the VBA code: Selection.InsertFile DocB.

DocA is a normal document in every regard.

DocB has 2 sections. The first is 'normal' stuff. The second section (properly marked with a Section Break/Next Page) has a new set of left and right margins.

When DocB is inserted into DocA with the above VBA command, all formatting (fonts, spacing, etc.) EXCEPT that the margins in the second section of DocB don't carry into DocA. (The Section Break/New Page marker is still present and visible in the 'assembled' document.)

Why are my margins being erased/ignored with a Selection.InsertFile command?

(I can manually copy and paste the content of DocB into DocA without issue.)

回答1:

Word stores margin settings, headers, footers and similar layout information in the section breaks. For the last section of a document (or the only section if there are no section breaks) this information is stored "in" the last paragraph mark.

When you copy/paste a document, that last paragraph mark with the section formatting is included.

When you use VBA to insert a file, that last paragraph mark is cut off. That's why you're losing the margins. In order to retain that information, before you insert the file you need to add a section break (probably continuous) to the end of the document. This is basically what the Master-/Subdocument functionality does, by the way.