pat.bat
Goto Top

VB.Net - Word Dokument wird nicht abgespeichert

Hallo zusammen,

folgende Methode öffnet zwar das Template und fügt den String in die Textmarke, aber das Dokument wird nicht abgespeichert.

    Private Sub btnZahllisten_Click(sender As Object, e As EventArgs) Handles btnZahllisten.Click

        Dim word As New Microsoft.Office.Interop.Word.Application
        Dim doc As New Microsoft.Office.Interop.Word.Document
        Dim format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument

        Try
            Dim csvLines() As String = System.IO.File.ReadAllLines(".\ZE.txt", System.Text.Encoding.Default)  
            For Each csvline In csvLines
                Dim objStreamWriter As StreamWriter
                Dim FileName As String = csvline.Replace(Chr(34), "")  
                objStreamWriter = New StreamWriter(".\Zahllisten\" & FileName & ".txt")  
                Dim lines() As String = System.IO.File.ReadAllLines(".\Zahlliste.txt", System.Text.Encoding.Default)  
                For Each line In lines
                    If line.Contains(csvline) Then
                        objStreamWriter.WriteLine(line)
                    End If
                Next
                objStreamWriter.Close()



                word = CreateObject("Word.Application")  

                doc = word.Documents.Open(Application.StartupPath & "\ZahllistenTemplate.dotx")  
                doc.Activate()

                If doc.Range.Bookmarks.Exists("Einrichtungsname") Then  
                    doc.Bookmarks.Item("Einrichtungsname").Range.Text = csvline  
                Else
                    MessageBox.Show("Textmarke nicht vorhanden!")  
                End If

                doc.SaveAs2(".\" & FileName & ".doc", format)  
                doc.Close(Nothing, Nothing, Nothing)
                doc = Nothing
                word.Application.Quit()

            Next



        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            word.Application.Quit()
        End Try
    End Sub

Auch kommt eine Messagebox hoch wo er mich fragt ob ich das Template im schreibgeschützten Modus öffnen möchte, da das Dokument bereits von mir verwendet wird.

Allerdings verstehe ich nicht, warum das .doc nicht abgespeichert wird. Eine Fehlermeldung kommt nicht.

Content-Key: 483031

Url: https://administrator.de/contentid/483031

Printed on: April 18, 2024 at 22:04 o'clock

Member: emeriks
Solution emeriks Aug 08, 2019 at 13:10:53 (UTC)
Goto Top
Hi,
wenn man von einer Vorlage ein neues Dokument erstellen will, dann darf man meines Wissens das Template nicht mit Open öffnen sondern muss mit Add ein neues Dokument erstellen.

Also bei Dir in Zeile 25
doc = word.Documents.Add(Application.StartupPath & "\ZahllistenTemplate.dotx")  

E.
Member: Pat.bat
Pat.bat Aug 09, 2019 at 07:38:40 (UTC)
Goto Top
Hallo,

das hat mit der Messagebox geholfen, Danke.

Leider speichert er nur noch kein Dokument ab.

doc.SaveAs2(Application.StartupPath & FileName & ".rtf", format)  
Member: emeriks
Solution emeriks Aug 09, 2019 updated at 08:02:10 (UTC)
Goto Top
Ich vermute, "Filename" enthält keinen "\". Deshalb muss es wohl sein:
doc.SaveAs2(Application.StartupPath & "\" & FileName & ".rtf", format)  
Member: Pat.bat
Pat.bat Aug 09, 2019 at 08:21:39 (UTC)
Goto Top
puh danke. Das ist ja wie das vergessene Semikolon und man sucht ewig nach dem Fehler... ich brauche Urlaub face-smile