Vb.net word bookmarks

FCapo used Ask the Experts™

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
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

>>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.
Top Expert 2015
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.
You might be able to time portions of the code to find the bottle neck. ..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial