We help IT Professionals succeed at work.

Problem creating second file with delphi app

GlobexCorp
GlobexCorp asked
on
205 Views
Last Modified: 2012-05-05
I have written a form in delphi which accepts user input and creates a letter in MS word with mail merge and a letter template.  It is working perfectly, but if I try to create a second letter while any letter created with the same template is open, it will not work, word does not launch with a new window.  If another doc created from another word template is open, it works perfectly.  The code for the app is available if required, I just don't want to fill the whole screen up with rubbish!!  The line which launches word with the necessary data is:

ShellExecute(Handle, 'open', 'data\testing.bat', nil, nil, SW_SHOW);

testing.bat just contains the following line:   c:\Progra~1\jodlet~1\data\jodletter.dot

The reason for using a batch file rather than just launching it directly from within delphi is that the batch file opens a doc based on the dot, but when I tried it directly from within delphi it opened the actual .dot file.  


Any help would be greatly appreciated, this project has me driven mental at this stage.  
Comment
Watch Question

Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2007

Commented:
ok, ok, if you'd make copies, then you wouldn't need the batch file of course... ;)

procedure TForm1.Button1Click(Sender: TObject);
var
  dotName:String;
begin
   Inc(Counter);
   dotName:='C:\dot\Fax'+IntToStr(Counter)+'.dot';
   CopyFile('C:\dot\Fax.dot',PAnsiChar(dotName),False);
   ShellExecute(Handle, 'open', PAnsiChar(dotName), nil, nil, SW_SHOW);
end;
Top Expert 2007

Commented:
And here's a version that would auto-increment the filename of the copies:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellApi;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

function IncFN(fn:String):String;
var
  del,i:Integer;
begin
    i:=1;
    Result:=fn;
    while FileExists(Result) do begin
       Result:=fn;
       del:=Lastdelimiter('.',Result);
       if del>0 then
          Insert(IntToStr(i),Result,del)
       else
          Insert(IntToStr(i),Result,Length(Result)+1);
       inc(i);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
OrigName, NewName:String;
begin
   OrigName:='C:\dot\Fax.dot';
   NewName:=IncFN(OrigName);
   CopyFile(PChar(OrigName),PChar(NewName),False);
   ShellExecute(Handle, 'open', PChar(NewName), nil, nil, SW_SHOW);
end;

end.

Author

Commented:
Thanks for the assistance.. the first one works, you still need the batch file to open it up or it just opens up the .dot file.  However, I'd like to inbuild the ability to clear down the fax.dot files.  I've added in that it copies fax.dot to a temp file, deletes all fax* files, and then copies fax.dot back over to itself upon launch.  However, if I reopen a the app while a letter created with, say, fax1.dot is open(which most of them would be created by, realistically), it doesn't work, we're back to the same old problem.  Is there any way I can ask it to check if fax1.dot exists and skip on to the next file name if it does?  

Thanks again!
Top Expert 2007

Commented:
>Is there any way I can ask it to check if fax1.dot exists and skip on to the next file name if it does?  
That's exactly what the function IncFN() from my last example does. You could combine it with using the batch file, e.g.:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellApi;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

function IncFN(fn:String):String;
var
  del,i:Integer;
begin
    i:=1;
    Result:=fn;
    while FileExists(Result) do begin
       Result:=fn;
       del:=Lastdelimiter('.',Result);
       if del>0 then
          Insert(IntToStr(i),Result,del)
       else
          Insert(IntToStr(i),Result,Length(Result)+1);
       inc(i);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
fn, OrigName, NewName:String;
F: TextFile;
begin
   fn:='testing.bat';
   OrigName:='C:\dot\Fax.dot';
   NewName:=IncFN(OrigName);
   CopyFile(PChar(OrigName),PChar(NewName),False);
   AssignFile(F, fn);
   Rewrite(F);
   Writeln(F,'echo off');
   Writeln(F,NewName);
   CloseFile(F);
   ShellExecute(Handle, 'open', PAnsiChar(fn), nil, nil, SW_SHOW);
end;

end.
Top Expert 2007

Commented:
>That's exactly what the function IncFN() from my last example does...
So if you give this function a filename like e.g. SomeFile.txt (with path, or it will check in the current folder), it will check for
SomeFile.txt
SomeFile1.txt
SomeFile2.txt
SomeFile3.txt ...and so on, until it finds a file name for which there's no existing file.

Author

Commented:
Thank you, that is perfect!  
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.