VBA Word Automation

Discussion in 'Programming & Software Development' started by gumbi, Jun 30, 2005.

  1. gumbi

    gumbi Member

    Jun 26, 2001
    Hi Guys,

    I am trying to get some software to search and replace through word to make an 'editable' invoice layout.

    The template i am using is here:

    I have it working, but it is very slow, taking 5-10 minutes. The code I am using to make the actual replacements appears to be working very poorly.

    It is written in delphi, as below, sorry it is not especially neat, i have been hacking on this for far too long:
    function ReplaceTxt(FromText, ToText : string) : boolean;
    var  r : OleVariant;
         myStoryRange : OleVariant;
         myShape : OleVariant;
         i : int;
         wdFindContinue, wdReplaceAll : integer;
      Result := false;
    { Just to Remember...
    #define wdMainTextStory  1
    #define wdFootnotesStory  2
    #define wdEndnotesStory  3
    #define wdCommentsStory  4
    #define wdTextFrameStory  5
    #define wdEvenPagesHeaderStory  6
    #define wdPrimaryHeaderStory  7
    #define wdEvenPagesFooterStory  8
    #define wdPrimaryFooterStory  9
    #define wdFirstPageHeaderStory  10
    #define wdFirstPageFooterStory  11
    //Method to do it via STORY types, cant figure out how to look through by each story but only ones which do not fail are 1 & 5
        wdFindContinue := 1;
        wdReplaceAll := 2;
        //Replace all in main story.
        myStoryRange := w.ActiveDocument.StoryRanges.Item(1);
        myStoryRange.Find.Execute (FromText, true, true, false, false, false, true, wdFindContinue, false, ToText, wdReplaceAll);
        //'All' in text boxes (this doesnt appear to work, only does one text box?)
        myStoryRange := w.ActiveDocument.StoryRanges.Item(5);
        myStoryRange.Find.Execute (FromText, true, true, false, false, false, true, wdFindContinue, false, ToText, wdReplaceAll);
        //This works, there are only 15 shapes but it makes it unfeasibly slow...
        for i := 1 to w.ActiveDocument.Shapes.Count do
          myShape := w.ActiveDocument.Shapes.Item(i);
          if myShape.TextFrame.HasText then
            myShape.TextFrame.TextRange.Find.Execute(FromText, true, true, false, false, false, true, 1, false, ToText, 2);
    References i have used.
    http://msdn.microsoft.com/library/d...vbawd11/html/woobjStoryRanges1_HV05213581.asp - MS Page with some examples incl story type
    http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm - story type example
    http://www.geocities.com/ResearchTriangle/9834/msword8.htm#WdStoryType - constants which are required.
    http://www.mail-archive.com/beginning_asp@p2p.wrox.com/msg10859.html - Shape example, where i stole this from.

    My problem is that I have difficulties making it update the text inside the text boxes, so to do this I have added code to iterate through all of the shapes on the page (there are 15) and this has significantly slowed it down, as i am making a few hundred replacements (as you can see from the template).

    Does anyone have any ideas to speed this up?

    Any help would be greatly appreciated as I have spent far too long on this.


Share This Page