In Word Datei schreiben

  • Ich habe ein RichEdit und möchte den Inhalt in ein .doc Dokument schreiben. Ich habe bereits die Word Funktionen gefunden (http://www.autoitscript.com/autoit3/docs/l…0Management.htm) aber keine von denen ist dafür, in ein Dokument zu schreiben. Das hab ich bis jetzt an code:

    [autoit]

    $MyDocsFolder = "::{450D8FBA-AD25-11D0-98A8-0800361B1103}"
    $wohinexport = FileSaveDialog("Plan Speichern", $MyDocsFolder,"Microsoft Word Dokumente (*.doc)", 2)
    If StringInStr($wohinexport,".doc") Then
    $word = _Word_Create(false,true)
    $doc = _Word_DocAdd($word)
    _Word_DocSaveAs($doc,$wohinexport)
    $wasinfile = _GUICtrlRichEdit_StreamToFile($Edit1, $wohinexport)
    _Word_DocSave($doc)
    _Word_DocClose($doc)
    _Word_Quit($word)
    Else
    $wohinexport = $wohinexport & ".doc"
    $word = _Word_Create(false,true)
    $doc = _Word_DocAdd($word)
    _Word_DocSaveAs($doc,$wohinexport)
    $wasinfile = _GUICtrlRichEdit_StreamToFile($Edit1, $wohinexport) ;this is what I tried
    _Word_DocSave($doc)
    _Word_DocClose($doc)
    _Word_Quit($word)
    EndIf

    [/autoit]


    Was kann ich statt StreamToFile nutzen?

    Zusatzfrage: Wenn ich das ganze in eine .rtf datei streame und die datei öffnen möchte, sagt mir word, dass die dater von einem anderen Benuter gesprerrt ist (ich schätze mal das ist das Programm selber). Wenn ich dann mein Programm schließe oder einfach die schreibgeschützte Version öffne, ist das Dokument leer.
    Hier Der Code Dafür:

    [autoit]

    $MyDocsFolder = "::{450D8FBA-AD25-11D0-98A8-0800361B1103}"
    $wohinexport = FileSaveDialog("Plan Speichern", $MyDocsFolder,"Rich Text Files (*.rtf)", 2)
    If StringInStr($wohinexport,".rtf") Then
    _FileCreate($wohinexport)
    $wasinfile = _GUICtrlRichEdit_StreamToFile($Edit1, $wohinexport)
    Else
    $wohinexport = $wohinexport & ".rtf"
    _FileCreate($wohinexport)
    $wasinfile = _GUICtrlRichEdit_StreamToFile($Edit1, $wohinexport)
    EndIf

    [/autoit]
  • Ich setze mal voraus, dass Du die #include <Word.au3> einbindest.

    _DocSaveAs bedingt die Nennung des Dateinamen ohne Zusatz, wie .doc.
    In die "ELSE" Abfrage kommt Dein Script nicht, da der Filter nur ".doc" zulässt. Bitte prüfe das mal nach.

    Der Ablauf ist dann:
    1. mit _GUICtrlRichEdit_StreamToFile eine rtf Datei erzeugen (habe ich aus der Hilfedatei entnommen)
    2. mit _WordCreate diese Datei in Word laden (kann auch im Hintergrund erfolgen...siehe Hilfe)
    3. Inhalte sammeln mit _WordDocGetCollection
    4. Datei mit neuem Dateinamen - ohne Endung - dafür mit Angabe des Zielformats (0) speichern

    Zielformate im _WordDocSaveAs sind u.a.:
    0 = .doc
    3 = .txt
    6 = .rtf
    8 = .htm
    11 = .xml
    12 = .docx
    17 = .pdf

    Exemplarisch würdest Du die Speicherung in Word so erreichen:

    Spoiler anzeigen
    [autoit]


    #include <word.au3>
    _GUICtrlRichEdit_StreamToFile($Edit1, @ScriptDir & "\gcre.rtf") ;this is what I tried
    $oWord = _WordCreate(@ScriptDir & "\gcre.rtf")
    $oDoc = _WordDocGetCollection ($oWord, 0)
    $DocNeu = _WordDocSaveAs($oDoc,@ScriptDir & "\TestAlsDoc", 0) ; hier ist auch _WordDocSaveAs($oDoc,@ScriptDir & "\gcre",0) möglich
    _WordDocClose($oDoc)

    [/autoit]

    Damit wirst Du Dein Script sicher anpassen können.

    2 Mal editiert, zuletzt von qwert23 (22. Februar 2014 um 19:20)

  • Kann es sein, dass diese Funktionen veraltet sind? Denn mein AutoIT erkennt diese Funktionen nicht an (Ja, ich habe word.au3 importiert). Funktionen wie _WordCreate funktionieren bei mir aber mit Unterstrich, also _Word_Create. Nur bei der _WordDocGetCollection Fuktion ist dies nicht möglich. Zusätzlich haben die Funktionen andere funktionen. Hab ich irgendwas vllt falsch gemacht?


    Dazu kommt das, was ich in die zusatzfrage geschrieben habe. Die .rtf datei kann nur im schreibgeschützten modus geöffnet werden.

  • Dann bitte ich Dich um mehr Informationen, wie Welche Fehlermeldungen erscheinen?
    Ist die Word.au3 in Deinem Include-Verzeichnis?
    Deine Zusatzfrage kann ich so nicht beurteilen, da in dem kleinen Beispiel keine schreibgeschützte / gesperrte Datei erzeugt wird.

  • Also das hier erscheint wenn ich das Skript laufen lasse: http://gyazo.com/54c17e8a5135ac3bcc20351695392ce4
    Mit folgendem Srkipt

    [autoit]

    #include <word.au3>
    _GUICtrlRichEdit_StreamToFile($Edit1, $DIR_Data & "\tempplan.rtf")
    $word = _Word_Create(false)
    $oWord = _Word_DocOpen($word, $DIR_Data & "\tempplan.rtf") ;Damit der speichern dialog kommt
    $oDoc = _Word_DocGet($oWord, 0)
    $DocNeu = _Word_DocSave($oDoc)
    _Word_DocClose($oDoc) ;die Funktionen ohne den zweiten Unterstrich geht ja nicht

    [/autoit]

    Ich hab übrigens SciTE Version 3.3.7

  • Die Anweisung #include <word.au3> wird offensichtlich nicht erreicht.
    Die gesamten Funktionen mit Word kannst Du Dir doch in diesem Beispiel sparen, da Du selbige Datei als rtf öffnest und als rtf speicherst.
    Da ist erst einmal kein Unterschied.
    Ursprünglich wolltest Du doch die Datei als .doc speichern, stimmts?

    Dann gilt auch hier, SaveAs nur ohne Dateizusatz - in Deinem Falle ".rtf"

    Spoiler anzeigen

    Name...........: _Word_DocSaveAs
    ; Description ...: Saves the specified Word document with a new name or format.
    ; Syntax.........: _Word_DocSaveAs($oDoc, $sFileName[, $iFileFormat = 0[, $bReadOnlyRecommended = False[, $bAddToRecentFiles = True[, $sPassword = ""[, $sWritePassword = ""]]]]])
    ; Parameters ....: $oDoc - Word document object
    ; $sFileName - Optional: The full path name for the document (default = Current folder and current file name).
    ; If the document has never been saved, the default name is used (for example, Doc1.doc);
    ; $iFileFormat - Optional: The format in which the document is saved. Can be any WdSaveFormat constant
    ; (default = $wdFormatDocument)
    ; $bReadOnlyRecommended - Optional: True to have Word suggest read-only status whenever the document is opened (default = False)
    ; $bAddToRecentFiles - Optional: True to add the document to the list of recently used files (default = True)
    ; $sPassword - Optional: A password string for opening the document (default = "")
    ; $sWritePassword - Optional: A password string for saving changes to the document (default = "")
    ; Return values .: Success - Returns 1
    ; Failure - Returns 0 and sets @error
    ; |1 - $oDoc is not an object
    ; |2 - Error saving the specified document. @extended is set to the error returned by method Documents.SaveAs
    ; Author ........: water (based on the Word UDF written by Bob Anthony)
    ; Modified ......:
    ; Remarks .......: If a document with the specified file name already exists, the document is overwritten without the user being prompted first.
    ; Related .......:
    ; Link ..........:
    ; Example .......: Yes

    HIer: http://www.autoitscript.com/forum/topic/15…e-to-word-file/ fragst Du nach einer etwas anderen Aufgabenstellung. Was ist denn davon Deine Fragestellung?

    Einmal editiert, zuletzt von qwert23 (22. Februar 2014 um 21:08)

  • Du versuchst in Deinem Codeausschnitt in eine vorhanden .doc zu schreiben.
    $wasinfile = _GUICtrlRichEdit_StreamToFile($Edit1, $wohinexport)

    $wohinexport kann nur eine .doc sein, lt. Deinem Beispiel. Es ist m.E. ein rtf Format.
    Deshalb schaue Dir mein Beispiel an und Du wirst die Lösung haben.

  • qwert23,
    reinhardt1julian verwendet die 3.3.10.2 Version von AutoIt. Dort wurde die Word UDF durch die WordEX ersetzt.
    Daher heissen alle Funktionen nun _Word_* statt _Word*.