Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports Word = Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices
   Dim oWord As Word.Application
     Dim oDoc As Word.Document
     oWord = CreateObject("Word.Application")
         oDoc = oWord.Documents.Open("Test.doc")
          oWord.Visible = False
         oDoc.SpellingChecked = False
         oWord.ScreenUpdating = False
         oWord.CheckLanguage = False
         oDoc.GrammarChecked = False
         oWord.AutoCorrect.CorrectCapsLock = False
         oWord.AutoCorrect.CorrectDays = False
         oWord.AutoCorrect.CorrectHangulAndAlphabet = False
         oWord.AutoCorrect.CorrectInitialCaps = False
         oWord.AutoCorrect.CorrectKeyboardSetting = False
         oWord.AutoCorrect.CorrectSentenceCaps = False


In my code I am reading paragraph and replace each string with other string.
so what else remaining so that I increase my code execution speed.

VB
  sReplace = "AbCD"
ofind = oWord.Selection.Find
  For Each oPara1 In oWord.ActiveDocument.Paragraphs
               myRange = oPara1.Range
               Dim str1 As String = oPara1.Range.Text
               Dim str2 As String = Midstr(str1)'remove space form string
               If str2 <> Nothing Then
                   bResult = IsUnicode(str2)
                   If bResult = True Then
                   Else
                       With ofind
                           .ClearFormatting()
                           .Text = str2
                           .Replacement.ClearFormatting()
                           .Replacement.Text = sReplace
                           .MatchCase = True
                           .MatchWholeWord = True
                           .Wrap = Word.WdFindWrap.wdFindContinue
                           .Execute(Replace:=Word.WdReplace.wdReplaceAll)
                       End With
                   End If

               End If
               oWord.Selection.MoveDown(Unit:=Word.WdUnits.wdParagraph, Count:=1)
           Next



[Edited]Code is wrapped in "pre" tag[/Edited]
[Edit: DMA: question was lost in code block]
Posted
Updated 25-Nov-11 21:52pm
v3
Comments
DaveAuld 26-Nov-11 3:51am    
And what do you mean by Speed Up? what appears to be slow? Speed is relative, if you are doing this on an old dated PC, then getting a new shiny faster i7 and heaps of memory and SSD's will speed it up, but somehow don't think that is the sort of answer you want!
RICKY_Xxx 26-Nov-11 6:11am    
I known it depend on Machine.i am new in word programming.i search for that is there another way to do the above code that give me best performance?
YvesDaoust 27-Nov-11 12:20pm    
Given that you are using the built-in function for text replacement, there is little chance that you could achieve better performance from a Word macro.

If you really experience a perfomance problem (huge text/massive number of replacements), doing the replacements using other tools can be an option.

1 solution

Keep in mind: automation is inherently relatively slow.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900