Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCreate a Word document containing a picture that makes text to flow around it in Visual Basic .NET
DescriptionThis example shows how to create a Word document containing a picture that makes text to flow around it in Visual Basic .NET.
KeywordsMicrosoft Word,Word cell format,read from Word,Microsoft Office,Office,Word cell color,cell color,cell format,Word,right align, alignment, picture alignment
CategoriesVB.NET, Office
 

See the example Create a Word document with some formatting in Visual Basic .NET for basic instructions on using the Word application to create a Word document in Visual Basic .NET. This example extends that one to add a picture that allows text to flow around it and that is aligned in the page's upper right corner.

After creating the Word document and adding text to it as in the previous example, the program uses the following code to add and format the picture.

 
' Find the beginning of the document.
' For other pre-defined bookmarks, see:
'      http://support.microsoft.com/kb/212555
Dim start_of_doc As Object = "\startofdoc"

' Get a Range at the start of the document.
Dim start_range As Word.Range = _
    word_doc.Bookmarks.Item(start_of_doc).Range

' Add the picture to the Range's InlineShapes.
Dim picture_file As String = txtPicture.Text
Dim inline_shape As Word.InlineShape = _
    start_range.InlineShapes.AddPicture(picture_file)

' Format the picture.
Dim shape As Word.Shape = inline_shape.ConvertToShape()

' Scale uniformly by 50%.
shape.LockAspectRatio = _
    Microsoft.Office.Core.MsoTriState.msoTrue
shape.ScaleHeight(0.5F, _
    Microsoft.Office.Core.MsoTriState.msoTrue, _
    Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft)

' Wrap text around the picture's square.
shape.WrapFormat.Type = Word.WdWrapType.wdWrapSquare

' Align the picture on the upper right.
shape.Left = CSng(Word.WdShapePosition.wdShapeRight)
shape.Top = CSng(Word.WdShapePosition.wdShapeTop)
 
This code first defines a string containing the bookmark name "\startofdocument." This is a predefined bookmark that represents the start of the document. The code uses it to get a Range representing that start. It then adds the picture to the Range's InlineShapes collection.

The code then gets a Shape object representing the InlineShape. It sets the Shape's LockAspectRatio property to true so the picture will only resize uniformly and then scales it to 50% of its original size.

The code makes the Shape wrap text around itself and finally sets the Shape's Left and Top values to make the picture appear in the upper right corner of the page.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated