SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 34
Thread: Generate MS Word .doc file?
-
Jun 5, 2005, 04:47 #1
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help with COM? (still not fixed!)
I have the following script to generate a Microsoft Word file (actually it just fills in the bookmarks in template.doc):
PHP Code:<?
//1. Instanciate Word
$word = new COM("word.application") or die("Unable to instantiate Word");
//2. specify the MS Word template document
$template_file = "c:/avishai/php/manager/invoice/template.doc";
//3. open the template document
$word->Documents->Open($template_file);
//4. put '734' in the 'id' bookmark
$id = 734;
//5. get the bookmark and create a new MS Word Range (to enable text substitution)
$bookmarkname = "id";
$objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);
$range = $objBookmark->Range;
//6. now substitute the bookmark with actual value
$range->Text = $id;
//7. save the template as a new document (c:/reminder_new.doc)
$new_file = "c:/avishai/php/manager/invoice/test.doc";
$word->Documents[1]->SaveAs($new_file);
//8. free the object
$word->Quit();
$word->Release();
$word = null;
?>
Warning: (null)(): Invoke() failed: Type mismatch. Argument: 2 in c:\avishai\php\manager\invoice\process.invoice.php on line 19
And the file does not get produced.
Any ideas???? Thanks in advance,
ShaiLast edited by shaiphp; Jun 5, 2005 at 12:07.
-
Jun 5, 2005, 06:29 #2
- Join Date
- Mar 2003
- Location
- England, UK
- Posts
- 2,906
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well you're obviously using a third-party class, there is so many things that could be wrong here. You are best asking for support by the class author
-
Jun 5, 2005, 06:40 #3
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't think I am using a third party class...I definitely haven't downloaded one so it must be built in to PHP.
-
Jun 5, 2005, 09:06 #4
-
Jun 5, 2005, 09:19 #5
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for confirming, Dylan. I don't actually know how exactly my script communicates with MS Word, but it does!! I will look into how COM works at some point!
The thing I forgot to mention was that when the script runs, I get a dialog box from Word asking if I want to save the current document before exiting. This would imply that the file IS changed, but it is then exited without doing the Save As. If I say 'Yes' on the dialog box, the file saves with the new content. However, I need it to Save As!
In fact, I don't know if this makes it easier, but after the Word file is created I just need it to be attached to an email using mail() and then the file will be deleted! So it may be easier to just save the file somewhere temporary? I don't mind either way.
Also, if you know of a tutorial that will tell me how to create and save Word documents then I will try that. RTF will do as well.
Thanks so much and I hope you can help,
Shai
-
Jun 5, 2005, 09:30 #6
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
By the way, I am using PHP 4(.3.4). I have heard that PHP 5 uses slightly different OOP - could this be the problem? If so, how do I fix it? I do not want to upgrade to PHP 5 at this time.
-
Jun 5, 2005, 12:08 #7
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Still not fixed - any more suggestions??
Thanks
-
Jun 5, 2005, 12:22 #8
What happens if you get rid of the
PHP Code:$word = null;
-
Jun 5, 2005, 12:41 #9
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Nope - doing that makes no difference!
I really appreciate your help and I hope we will be able to solve this problem,
Shai
-
Jun 5, 2005, 12:48 #10
Line 19 is
PHP Code:$word->Documents[1]->SaveAs($new_file);
-
Jun 5, 2005, 12:49 #11
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes
-
Jun 5, 2005, 12:51 #12
Where did you get this script from?
-
Jun 5, 2005, 12:54 #13
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 5, 2005, 12:55 #14
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What happens if you get rid of the bookmark part? As in, just open, save, cleanup.
- Nathan
-
Jun 5, 2005, 12:56 #15
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Same problem
-
Jun 5, 2005, 12:57 #16
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
except that because you are not making any changes, the dialog box asking if you want to save changes doesn't pop up
-
Jun 5, 2005, 12:59 #17
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What OS are you running? What version of PHP? What version of Apache (or is it IIS)?
- Nathan
-
Jun 5, 2005, 13:01 #18
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Windows, PHP 4.3.4, Apache 1.3.29
-
Jun 5, 2005, 13:04 #19
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Which version of Windows? Which version of Office?
- Nathan
-
Jun 5, 2005, 13:06 #20
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Windows 98SE, Office 97
-
Jun 5, 2005, 13:07 #21
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't think Windows 98 supports COM. Actually, I don't think Office 97 does either. I'll see if I can confirm that, though.
- Nathan
-
Jun 5, 2005, 13:08 #22
- Join Date
- May 2005
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Very outdated software, I'm not suprised it isnt working!
-
Jun 5, 2005, 13:09 #23
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am pretty sure they do. Apart from anything else, when I click 'Yes' on the dialog box (to overwrite the template.doc file with the new content), it DOES work. The problem is that I can't save AS. So surely that means that COM is working?
-
Jun 5, 2005, 13:10 #24
- Join Date
- Feb 2005
- Posts
- 131
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Dale, do you have any contributions to make or just useless comments about my software??
-
Jun 5, 2005, 13:11 #25
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Give SaveAs a second parameter of 0, see if that works. I haven't see anyone use saveas without the second parameter.
- Nathan
Bookmarks