Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2005, 03:20 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default VBA Word is very slow with vb.net

I write to a word doc from my vb.net code. The code draws some of autoshapes, formats the shapes, writes text to them and format the text, auto resize the shape as text goes on increasing. When i run the code using F11 it works fine but if i do F5 shapes overlap each other.

I think the problem is because the shapes takes time to draw and autoresize and so their positions are not calculated properly.

Can anybody suggest something?
 
Old May 5th, 2005, 03:39 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I do a lot of this kind of stuff, but haven't encountered (or tried) creating shapes in Word VBA via .NET

Some immediate workarounds spring to mind:
 
  • if the shapes you're creating aren't dependant on calculations of some sort, then consider using the AutoText feature in Word
  • if you're using Word 2003, you can create the objects far more securely via WordML (Xml)

hope this helps..
 
Old May 5th, 2005, 03:58 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is some of the code extract

************************************************** **********
Private objWord = new Word.Application
objWord.Documents.Add()

leftPos = 27.0#
topPos = 30.0#

objWord.ActiveDocument.Shapes.AddShape(msoShapeFol dedCorner, leftPos, topPos, bucketWidth, 10.0#).Select()
            objWord.ActiveWindow.Selection.ShapeRange.TextFram e.TextRange.Select()
objWord.ActiveWindow.Selection.Collapse()
objWord.ActiveWindow.Selection.ShapeRange.Select()
            objWord.ActiveWindow.Selection.ShapeRange.Fill.For eColor.RGB = RGB(255, 253, 153)
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.MarginLeft = 0.0#
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.MarginRight = 0.0#
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.MarginTop = 0.0#
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.MarginBottom = 0.0#
objWord.ActiveWindow.Selection.ShapeRange.Relative HorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizo ntalPositionColumn
objWord.ActiveWindow.Selection.ShapeRange.Relative VerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVertical PositionParagraph
objWord.ActiveWindow.Selection.ShapeRange.WrapForm at.AllowOverlap = True
objWord.ActiveWindow.Selection.ShapeRange.WrapForm at.Side = Word.WdWrapSideType.wdWrapBoth
objWord.ActiveWindow.Selection.ShapeRange.WrapForm at.Type = Word.WdWrapType.wdWrapNone
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.AutoSize = Microsoft.Office.Core.MsoTriState.msoTrue
objWord.ActiveWindow.Selection.ShapeRange.TextFram e.WordWrap = Microsoft.Office.Core.MsoTriState.msoTrue

For ..... some condition
          objWord.ActiveWindow.Selection.TypeText(ideaCounte r & ". " & _dvIdeas.Item(counter).Item("IdeaText"))
                objWord.ActiveWindow.Selection.TypeParagraph()

Next
************************************************** **********

This writes to as many as 100 pages or even more.

Since this is a desktop application, it cannot be assured that the client will be using Word 2003 or not.
 
Old May 5th, 2005, 04:23 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

are you suggesting that the calls to create the ShapeRange object are being executed hundreds of times in an interation?

I would definitely consider setting up the shape as an AutoText in the first place. You can either do this manually, or at least create the shape only once, and then add that shape to the AutoText collection. You could then just insert the AutoText (via one line of code), and modify the resulting shape as necessary.

Either way, as a rule of thumb it's pretty inelegant, and ineffecient, to use the Selection object as you're doing. Did you originally record this via a VBA macro?

From you're code sample I would recommend spending much more time investigating the Word object model. There are many more low-level objects to use that might improve the performance and resilience of what you're trying to achieve
 
Old May 5th, 2005, 05:01 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes i recorded this with VBA macro.

I'd try to find some alternative to this. If you come across any other solution do let me know.





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net slow compared to vb6 jpwollman Pro Visual Basic 2005 8 January 17th, 2008 12:54 AM
VB.Net Word Automation deboe Pro Visual Basic 2005 0 November 26th, 2007 09:57 PM
Word Parsing - VB.net pauljohns353 VB.NET 0 February 20th, 2007 07:39 AM
formatting of a word document with vb.net krmilburn General .NET 0 April 18th, 2006 06:47 AM
Word to HTML Converter in VB.NET? Ron Howerton VB.NET 2002/2003 Basics 0 February 18th, 2004 03:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.