Insert PageBreak In Word Using VB 2005

L

Lokesh

Hi,
I'm developing a utility in Visual Basic 2005 (Professional Edition),
which can capture screen-shots and insert it in a word docuement. The below
is the code to insert the captured image in Word ( Word 2003) Document,

Imports Microsoft.Office.Core
Imports Microsoft.Vbe.Interop
Imports Word

' Inside the Sub ......
Dim WordApp As Word.Application
Dim doc As Word.Document
WordApp = New Word.Application
WordApp.Visible = False
doc = New Word.Document
doc.InlineShapes.AddPicture("filename_1.jpg", LinkToFile:=False,
SaveWithDocument:=True)
doc.ActiveWindow.Selection.MoveRight()
doc.ActiveWindow.Selection.TypeParagraph()
' ***************** The below Line (Pagebreak) throws an
Compilation Error, Refer below Explanation *****************
doc.ActiveWindow.Selection.InsertBreak(Type:=Word.wdpagebreak)
doc.InlineShapes.AddPicture("filename_2.jpg", LinkToFile:=False,
SaveWithDocument:=True)
doc.SaveAs("WordDocFilename.doc")
doc.Close()
WordApp = Nothing

MsgBox(" Screen Shot Captured")

In the above code, a compilation Error is Thrown as , " wdpagebreak" is not
a member of Word. But the same I tried in Word 2003 Macro, it works fine.
Kindly provide me a solution for the above.

Thanks In Advance,
Lokesh R
 
C

Cindy M.

Hi =?Utf-8?B?TG9rZXNo?=,
doc.ActiveWindow.Selection.InsertBreak(Type:=Word.wdpagebreak)
You haven't fully qualified the Enum:

Word.WdBreakType.wdPageBreak

If you do it right, Intellisense will give you a list. If you're
not getting the list, alarm bells should go off. If you're
unsure what comes between "Word" and the enum (wdPageBreak)
search the Enum in the Word Object Browser (Word's VB Editor,
then press F2).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
L

Lokesh

Hi Cindy,
Thanks for your reply. It worked !! Now, I'm trying to insert a
single screen-shot per page, when I tried with two sample screen-shots, they
were placed contigously (continously) one-after-other and a blank page is
inserted at the end. But the same code I tried in Word Macro, it places the
screen-shot one per page. Kindly review the code (in the above post) and let
me know the possible reason for the same.

Thanks,
Lokesh R
 
C

Cindy M.

Hi =?Utf-8?B?TG9rZXNo?=,
Now, I'm trying to insert a
single screen-shot per page, when I tried with two sample screen-shots, they
were placed contigously (continously) one-after-other and a blank page is
inserted at the end. But the same code I tried in Word Macro, it places the
screen-shot one per page. Kindly review the code (in the above post) and let
me know the possible reason for the same.
Working as you do with the Selection object is always critical. Much better is
to work with the RANGE object (think of a range like an invisible selection,
but can't be accessed by a user). FWIW, I think the problem you're running into
is because you don't specify the Range argument when inserting the pictures

Work in a document more like this:

Dim rng as Word.Range = doc.Range
'Put the "point" after all content in the range
rng.Collapse Word.wdCollapseDirection.wdCollapseEnd
doc.InlineShapes.AddPicture("filename_1.jpg", LinkToFile:=False,
SaveWithDocument:=True, Range:=rng)
rng.Collapse Word.wdCollapseDirection.wdCollapseEnd
rng.InsertBreak(Type:=Word.WdBreakType.wdpagebreak)
rng.Collapse Word.wdCollapseDirection.wdCollapseEnd
doc.InlineShapes.AddPicture("filename_1.jpg", LinkToFile:=False,
SaveWithDocument:=True, Range:=rng)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
L

Lokesh

Hi Cindy,

Now I'm able to insert page break, but the text in the document and
screen-shots are getting inserted as below,

The Text is inserted in the first page, the image in the
second page, immediately after the image (in the same page) the next comment
(text) and the second image in the next page, so on and so forth.

The code I've used is as below,

Imports Microsoft
Imports Word

' Inside the Sub .....

Dim Rng As Word.Range
Rng = doc.Range
Rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Rng.Text = Trim(Text_ToBe_inserted_In_theDoc) & chr(13)
Rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
doc.InlineShapes.AddPicture("filename.png", LinkToFile:=False,
SaveWithDocument:=True, Range:=Rng)
Rng.Text = Chr(13)
Rng.InsertBreak(Type:=Word.WdBreakType.wdPageBreak)
Rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

End Sub

I Expect the document to be as below,
1. The text and after that the image, both in a single page.
2. The next text and image in the next page, and so-on...

Kindly help me in inserting the text and image in the above format.

Thanks In Advance,
Lokesh R
 
C

Cindy M.

Hi =?Utf-8?B?TG9rZXNo?=,
Rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
doc.InlineShapes.AddPicture("filename.png", LinkToFile:=False,
SaveWithDocument:=True, Range:=Rng)
Rng.Text = Chr(13)
This last line will either replace the picture with the paragraph
mark, or put the paragraph mark before the picture, depending on
how Word is working. Put an rng.Select in, just after the
AddPicture method, then a breakpoint on the following line. What's
selected?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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