Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

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

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 30th June 2005, 1:30 PM   #1
gumbi Thread Starter
Member
 
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:
http://www.gumbi.net/files/invoice.dot

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:
Code:
function ReplaceTxt(FromText, ToText : string) : boolean;
var  r : OleVariant;
     myStoryRange : OleVariant;
     myShape : OleVariant;
     i : int;
     wdFindContinue, wdReplaceAll : integer;
begin
  Result := false;
  try
{ 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
    begin
      myShape := w.ActiveDocument.Shapes.Item(i);

      if myShape.TextFrame.HasText then
      begin
        myShape.TextFrame.TextRange.Find.Execute(FromText, true, true, false, false, false, true, 1, false, ToText, 2);
      end;
    end;

  finally
  end;
end;
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.

Chris
gumbi is offline   Reply With Quote

Join OCAU to remove this ad!
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

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 11:42 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!