OOP met word

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jeroen

Jeroen

01/10/2008 17:53:00
Quote Anchor link
Beste PHPers,

Heb een probleempje met het volgende script:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
ini_set("error_handling", 1);
error_reporting(E_ALL);

class PHPtoWORD
{
    // Defining filepath and extension
    var $FilePath = "C:/";
    var
$FileExt = "doc";
    
    // Making vars for filename and text
    var $FileName;
    var
$FileText;
    
    // Var for running the application (WORD)
    var $Application;
    
    function
__construct()
    {

        if(!$this->Application = new COM("word.application"))
        {

            echo "[001] An error occured when launching the <i>Word Application</i>, either the application couldn't get launched, or <i>Microsoft Office</i> was not found!";
        }

        else
        {
            $this->Application->Visible = 1;
            if(!empty($this->FileText) && !empty($this->FileName))
            {

                $this->AddTextToFile();
            }

            //else
            //{
            //    echo "[002] An error occured, the text for the document could not be found!";
            //}

        }
    }
    
    function
AddTextToFile()
    {

        $this->Application->Add();
        $this->Application->Selection->TypeText($this->filetext);
    }
    
    function
SaveFile($FilePath = "C:/", $FileExt = "doc")
    {

        if(!$this->Application->Documents[1]->SaveAs("C:/test.doc"))
        {

            echo "[003] An error occured, the file could not be saved!";
        }

        else
        {
            $this->ExitApplication();
        }
    }
    
    function
ExitApplication()
    {

        $this->Application->Quit();
        $this->Application = NULL;
    }
    
    function
SetFileName($filename)
    {

        if(!empty($filename))
        {

            $this->FileName = $filename;
        }

        else
        {
            echo "[004] An error occured, the file's name could not be changed!";
        }
    }
    
    function
SetFileExt($fileext)
    {

        if(!empty($fileext))
        {

            $fileext = strtolower($fileext);
            if($fileext == "doc" || $fileext == "docx")
            {

                $this->FileExt = $fileext;
            }

            else
            {
                echo "[005] An error occured, the file's extension is unknown, or not allowed!";
            }
        }

        else
        {
            echo "[006] An error occured, the file's extension could not be changed!";
        }
    }
    
    function
SetFileText($filetext)
    {

        if(!empty($filetext))
        {

            $this->FileText = $filetext;
        }

        else
        {
            echo "[007] An error occured, the text for the file could not be set!";
        }
    }
}


$PHP2WORD = new PHPtoWord();
$PHP2WORD->SetFileName('JUSTATEST');
$PHP2WORD->SetFileText('BLAHBLAHBLAHALVALHASDFL');
$PHP2WORD->SetFileExt('DOC');

$PHP2WORD->SaveFile("C:/", "doc");
?>

De foutmelding die ik krijg is de volgende:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> Het gevraagde lid van de collectie bestaat niet.' in C:\wamp\www\phpword\index.php:47 Stack trace: #0 C:\wamp\www\phpword\index.php(47): PHPtoWORD::SaveFile() #1 C:\wamp\www\phpword\index.php(113): PHPtoWORD->SaveFile('C:/', 'doc') #2 {main} thrown in C:\wamp\www\phpword\index.php on line 47

Wat zou betekenen dat het in deze regel fout zou moeten gaan:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?PHP    if(!$this->Application->Documents[1]->SaveAs("C:/test.doc"))
{

    echo "[003] An error occured, the file could not be saved!";
}

?>

Als jullie een oplossing weten zou ik dit graag horen!

Alvast bedankt

Jeroen
Gewijzigd op 01/01/1970 01:00:00 door Jeroen
 
PHP hulp

PHP hulp

20/04/2024 15:55:33
 
Douwe

Douwe

01/10/2008 18:16:00
Quote Anchor link
Je moet de Exceptions afvangen, met een try-catch blok, en dan de error weergeven, of iets zinnigs doen met de error.
 
Thijs X

Thijs X

01/10/2008 19:02:00
Quote Anchor link
De naam van je klasse lijkt meer op een naam voor een functie, wanneer je OOP gaat toepassen zijn de benaming van klassen methodes en attributen erg belangrijk.

Zie het als objecten, in dit geval maak je eigenlijk gewoon een nieuw word bestand aan. Dus een betere benaming voor de klasse zou bijvoorbeeld zijn: WordDocument.

Ook stamt het var gebeuren uit PHP4, beter is dus om de toegang van attributen aan te geven net zoals je bij de methoden ( functies binnen een klasse ) doet.

Bijvoorbeeld:
private $varnaam;
Gewijzigd op 01/01/1970 01:00:00 door Thijs X
 
Emmanuel Delay

Emmanuel Delay

01/10/2008 19:07:00
Quote Anchor link
Probeer jij een file op te slaan op locatie "C:/..."?
Dan is dat toch niet de bedoeling om online te zetten, op een server?

Zorg gewoon eens dat je die file op een submap opslaat, misschien helpt dat.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.