Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development


Thread Tools
Old 30th June 2005, 2:30 PM   #1
gumbi Thread Starter
gumbi's Avatar
Join Date: Jun 2001
Location: Sydney
Posts: 326
Default VBA Word Automation

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/de...HV05213581.asp - MS Page with some examples incl story type
http://word.mvps.org/FAQs/MacrosVBA/...AllWithVBA.htm - story type example
http://www.geocities.com/ResearchTri...tm#WdStoryType - constants which are required.
http://www.mail-archive.com/beginnin.../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.

gumbi is offline   Reply With Quote


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +10. The time now is 10:58 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Micron21!