Vb.net word bookmarks

Hi,

I've added many bookmarks in a word document, which I'm using as placeholders for text data that will be sent from my vb.net app.

From my app, when I click report, the following takes places:

        Dim appWord As New Word.Application
        Dim docWord As New Word.Document

        docWord = appWord.Documents.Add("s:\canevasSoumission.docx")
        appWord.Visible = True

            docWord.Bookmarks.Item("aFraisForNombre").Range.Text = GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForNombre"))
            docWord.Bookmarks.Item("aFraisForTarif").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForTarif")))
            docWord.Bookmarks.Item("aFraisForCoutant").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForCoutant")))
            docWord.Bookmarks.Item("aFraisForVendant").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForVendant")))

Open in new window


Once the word document opens up, I can see each bookmark being replaced with the text from my VB app, the thing is the progression is slow, being that I have hundreds of bookmarks, it can take quite some time for the word document to be filled.

I have another app which I've used with the same programming concept, when the word document is being generated the bookmarks get replaced at lightning speed...

I'm wondering if I've done something wrong with the code as to why it's slow...

thanks for the help
FCapoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>I have another app which I've used with the same programming concept, when the word document is being generated the bookmarks get replaced at lightning speed...


As I don't have the code - then my guess is:  yes, you have done something wrong - just what I haven't a clue.
0
Jacques Bourgeois (James Burger)PresidentCommented:
When working with any COM application, there are a few trick that you can use, the most important one being to have as few dots as possible in your code. Every dot is a communication back and forth between your application, the interop, OLE, the COM application. And these communications are what makes the thing so slow. They involve many layers as well as conversions for types that are different between COM and .NET, such as dates.

There are many ways to minimize dots, such as using a With structure:
		With docWord.Bookmarks
			.Item("aFraisForNombre").Range.Text = GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForNombre"))
			'You can have other code that does not have to do with Bookmarks here
			.Item("aFraisForTarif").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForTarif")))
			.Item("aFraisForCoutant").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForCoutant")))
			.Item("aFraisForVendant").Range.Text = FormatCurrency(GetStringFromObject(soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)("aFraisForVendant")))
		End With

Open in new window

With sometimes do wonders, but sometimes, you won't see any difference. The VB optimizer will sometimes use With by itself if you did not do it. It's a case by case. This might be what makes the difference between you application that is fast and the one that is not.

Or use a variable for an object that you use in many lines:
		Dim item As <Type> = soumissionBindingSourceA1.Item(soumissionBindingSourceA1.Position)
		With docWord.Bookmarks
			.Item("aFraisForNombre").Range.Text = GetStringFromObject(item("aFraisForNombre"))
			'You can have other code that does not have to do with Bookmarks here
			.Item("aFraisForTarif").Range.Text = FormatCurrency(item("aFraisForTarif")))
			.Item("aFraisForCoutant").Range.Text = FormatCurrency(item("aFraisForCoutant")))
			.Item("aFraisForVendant").Range.Text = FormatCurrency(item("aFraisForVendant")))
		End With

Open in new window

And since you use a template to start with, try to move as much of the operations as you can in VBA macros in the template. Instead writing 25 lines of codes in .NET, with 25 communications between the application and Word, you can often have one VBA macro that receives 25 parameters. This means only one communication instead of 25, and this makes a huge difference.
0
DrTribosCommented:
You might be able to time portions of the code to find the bottle neck. ..
http://www.experts-exchange.com/articles/17159/Timing-Multiple-Chunks-of-Code-VBA.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.