Improve OLE automation speed

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everyone

I work with Microsoft Word By creating a document using OLE automation. Everything works well, except it works slowly

For example, I create a table using OLE object. I already disabled Spell and Grammar checking, Pagination, prevent Screen updating. Of cause, it makes some effect, by still if I, for example, opens Word menu and leave it open, creation of table works faster

Please, help me. What I also has to disable in Word options to improve performance of OLE automation

Best regards
Igor
 
Hi Igor,

We would need more information on exactly what you are doing (show us the
code would be a good way) to be able to tell if there is any other way by
which you can speed up the process.

For a start though avoid the use of the Selection object. Use the Range
object whenever you can.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Igor said:
Hi everyone!


I work with Microsoft Word By creating a document using OLE automation.
Everything works well, except it works slowly.
For example, I create a table using OLE object. I already disabled Spell
and Grammar checking, Pagination, prevent Screen updating. Of cause, it
makes some effect, by still if I, for example, opens Word menu and leave it
open, creation of table works faster.
 
Hi

Here is my code where i create table
I work from VS .NET with C

public bool CreateTable(Word.Document oDocument

Word.Range oIRange
Word.Table oITable;
object oDefaultTableBehavior = 0
object oAutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
object oDirection = Word.WdCollapseDirection.wdCollapseEnd
object oBreak = Word.WdBreakType.wdLineBreak
bool bPagination
bool bCheckSpellingAsYouType
bool bCheckGrammarAsYouType
bool bSuggestSpellingCorrections
bool bSuggestFromMainDictionaryOnly
bool bCheckGrammarWithSpelling
bool bShowReadabilityStatistics
bool bIgnoreUppercase
bool bIgnoreMixedDigits
bool bIgnoreInternetAndFileAddresses
bool bStrictInitialAlefHamza
bool bStrictFinalYaa;

if ( oDocument == null ) return false

oIRange = Find("Some value")
if ( oIRange == null ) return false

oITable = oDocument.Tables.Add(oIRange, iRows, iCols, ref oDefaultTableBehavior, ref oAutoFitBehavior)
// Prevent Screen Updatin
oDocument.Application.ScreenUpdating = false;

bPagination = oDocument.Application.Options.Pagination; oDocument.Application.Options.Pagination = false
// Stop Spell And Grammar Checkin
bCheckSpellingAsYouType = oDocument.Application.Options.CheckSpellingAsYouType; oDocument.Application.Options.CheckSpellingAsYouType = false
bCheckGrammarAsYouType = oDocument.Application.Options.CheckGrammarAsYouType; oDocument.Application.Options.CheckGrammarAsYouType = false
bSuggestSpellingCorrections = oDocument.Application.Options.SuggestSpellingCorrections; oDocument.Application.Options.SuggestSpellingCorrections = false
bSuggestFromMainDictionaryOnly = oDocument.Application.Options.SuggestFromMainDictionaryOnly; oDocument.Application.Options.SuggestFromMainDictionaryOnly = false
bCheckGrammarWithSpelling = oDocument.Application.Options.CheckGrammarWithSpelling; oDocument.Application.Options.CheckGrammarWithSpelling = false
bShowReadabilityStatistics = oDocument.Application.Options.ShowReadabilityStatistics; oDocument.Application.Options.ShowReadabilityStatistics = false
bIgnoreUppercase = oDocument.Application.Options.IgnoreUppercase; oDocument.Application.Options.IgnoreUppercase = true
bIgnoreMixedDigits = oDocument.Application.Options.IgnoreMixedDigits; oDocument.Application.Options.IgnoreMixedDigits = true
bIgnoreInternetAndFileAddresses = oDocument.Application.Options.IgnoreInternetAndFileAddresses; oDocument.Application.Options.IgnoreInternetAndFileAddresses = true
bStrictInitialAlefHamza = oDocument.Application.Options.StrictInitialAlefHamza; oDocument.Application.Options.StrictInitialAlefHamza = false
bStrictFinalYaa = oDocument.Application.Options.StrictFinalYaa; oDocument.Application.Options.StrictFinalYaa = false

oDocument.Application.DisplayStatusBar = false
oDocument.Application.DisplayScrollBars = false

tr

for( int iRow = 0; iRow < iRows; iRow++

for( int iCol = 0; iCol < iCols; iCol++
{
Application.DoEvents()

oIRange = oITable.Cell(iRow + 1, iCol + 1).Range
// I have set different format. These format is for example only
Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
oITable.Cell(iRow + 1, iCol + 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalTop;
oIRange.Bold = 1

oIRange.InsertAfter("Some string")
}


catc
{
// Restore Spell Checking
oDocument.Application.Options.CheckSpellingAsYouType = bCheckSpellingAsYouType
...
oDocument.Application.Options.StrictFinalYaa = bStrictFinalYaa

oDocument.Application.Options.Pagination = bPagination

oDocument.Application.DisplayStatusBar = true
oDocument.Application.DisplayScrollBars = true

// Allow Screen Refres
oDocument.Application.ScreenUpdating = true

return true
}
 
So what you are doing is inserting a table into a document and then
formatting and populating each cell of the table with some text.

I wonder whether it might not be quicker to pass a string with the elements
in each row separated with a vbTab and each row terminated with a vbCr and
then convert the text to a table.

I also wonder whether the routine of turning everything off is taking longer
that just leaving it the way it is.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Igor said:
Hi!

Here is my code where i create table.
I work from VS .NET with C#


public bool CreateTable(Word.Document oDocument)
{
Word.Range oIRange;
Word.Table oITable;
object oDefaultTableBehavior = 0;
object oAutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
object oDirection = Word.WdCollapseDirection.wdCollapseEnd;
object oBreak = Word.WdBreakType.wdLineBreak;
bool bPagination;
bool bCheckSpellingAsYouType;
bool bCheckGrammarAsYouType;
bool bSuggestSpellingCorrections;
bool bSuggestFromMainDictionaryOnly;
bool bCheckGrammarWithSpelling;
bool bShowReadabilityStatistics;
bool bIgnoreUppercase;
bool bIgnoreMixedDigits;
bool bIgnoreInternetAndFileAddresses;
bool bStrictInitialAlefHamza;
bool bStrictFinalYaa;

if ( oDocument == null ) return false;

oIRange = Find("Some value");
if ( oIRange == null ) return false;

oITable = oDocument.Tables.Add(oIRange, iRows, iCols, ref
oDefaultTableBehavior, ref oAutoFitBehavior);
// Prevent Screen Updating
oDocument.Application.ScreenUpdating = false;

bPagination = oDocument.Application.Options.Pagination;
oDocument.Application.Options.Pagination = false;
// Stop Spell And Grammar Checking
bCheckSpellingAsYouType =
oDocument.Application.Options.CheckSpellingAsYouType;
oDocument.Application.Options.CheckSpellingAsYouType = false;
bCheckGrammarAsYouType =
oDocument.Application.Options.CheckGrammarAsYouType;
oDocument.Application.Options.CheckGrammarAsYouType = false;
bSuggestSpellingCorrections =
oDocument.Application.Options.SuggestSpellingCorrections;
oDocument.Application.Options.SuggestSpellingCorrections = false;
bSuggestFromMainDictionaryOnly =
oDocument.Application.Options.SuggestFromMainDictionaryOnly;
oDocument.Application.Options.SuggestFromMainDictionaryOnly = false;
bCheckGrammarWithSpelling =
oDocument.Application.Options.CheckGrammarWithSpelling;
oDocument.Application.Options.CheckGrammarWithSpelling = false;
bShowReadabilityStatistics =
oDocument.Application.Options.ShowReadabilityStatistics;
oDocument.Application.Options.ShowReadabilityStatistics = false;
bIgnoreUppercase = oDocument.Application.Options.IgnoreUppercase;
oDocument.Application.Options.IgnoreUppercase = true;
bIgnoreMixedDigits = oDocument.Application.Options.IgnoreMixedDigits;
oDocument.Application.Options.IgnoreMixedDigits = true;
bIgnoreInternetAndFileAddresses =
oDocument.Application.Options.IgnoreInternetAndFileAddresses;
oDocument.Application.Options.IgnoreInternetAndFileAddresses = true;
bStrictInitialAlefHamza =
oDocument.Application.Options.StrictInitialAlefHamza;
oDocument.Application.Options.StrictInitialAlefHamza = false;
bStrictFinalYaa = oDocument.Application.Options.StrictFinalYaa;
oDocument.Application.Options.StrictFinalYaa = false;
 
Hi Igor

Another thing that would speed up the code is to set the paragraph and cell
alignments and the Bold attribute for the entire table once, instead of
setting them individually for each cell.

If you use Doug's idea of inserting plain text and converting it to a table,
the ConvertToTable method will return the Table object that you assign to
the variable olTable. Then you can do the formatting:

olTable.Range.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphLeft
olTable.Range.Cells.VerticalAlignment = _
Word.WdCellVerticalAlignment.wdCellAlignVerticalTop
olTable.Range.Bold = True

Even if you continue to use a loop to insert the text into the table, taking
the formatting out of the loop will help.
 
Back
Top