I'm getting error "Type mismatch" when trying to InsertBreak via Range using parameters (for instance, wdPageBreak or wdLineBreak). But it's ok, to InsertBreak parameterless. Am I the only one getting such a behaviour, or is it another Word API bug?
//MS Word VBA Reference
Set myRange = ActiveDocument.Paragraphs(2).Range
With myRange
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
//c++ code
HRESULT hr = pWordDoc->GetParagraphs()->Item(1)->GetRange()->
InsertBreak(&variant_t(Word::wdPageBreak)); //hr = 0x80020005 TypeMismatch
hr = pWordDoc->GetParagraphs()->Item(1)->GetRange()->
InsertBreak(&vtMissing); // hr = S_OK
If there's no way to get the C++ version of this part of Word's object model to function correctly you can work around it by using the ANSI character codes to insert certain kinds of breaks. (They can also be used to search/identify the breaks in a document's text.)
Page break: ANSI 12 (= press Ctrl+Enter)
Line break: ANSI 11 (= press Shift+Enter)
Paragraph break: ANSI 13 (= press Enter)
Note that ANSI 12 is also the character code for the various types of section break; a page break is the default, so you're OK inserting the ANSI code if a page break is what's wanted.
In order to insert a Section Break it's possible to use the Sections.Add
method. This accepts parameters indicating where the section break should be in the document and of what type. Method signature:
expression.Add(Range, Start)
Where expression
is a variable that represents a Sections collection. (wordDoc.Sections.Add
for example)
Valid values for Start
come from the WdSectionStart
enumeration:
wdSectionContinuous 0 Continuous section break.
wdSectionEvenPage 3 Even pages section break.
wdSectionNewColumn 1 New column section break.
wdSectionNewPage 2 New page section break.
wdSectionOddPage 4 Odd pages section break.
User contributions licensed under CC BY-SA 3.0