Word - Formatierung wird nicht übernommen

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von jan99.

    Word - Formatierung wird nicht übernommen

    Moin!

    ich baue gerade etwas an einem Tool das Worddateien bearbeitet. Wiederkehrend werden an bestimmten Bookmarks Texte einfügt und optional formatiert.

    Das habe schon an x Stellen in dem Programm funktioniert. Nur an einem Bookmark nicht. Im Debugger werden alle Passagen korrekt durchlaufen. Aber die farbliche Hervorhebung und Bold werden nicht erstellt.

    Das ich an der richtigen Stelle bin zeigt, dass der einzufügende Text platziert wird. Halt nur die Formatierung wird ignoriert!

    Hier zunächst mein Code:

    VB.NET-Quellcode

    1. ''' <summary>
    2. ''' Setzen eines Bookmarks
    3. ''' </summary>
    4. ''' <param name="BookmarkName"></param>
    5. ''' <param name="Text"></param>
    6. ''' <param name="CreateError">optional Schreiben einer Fehlermeldung (
    7. ' default=true) - false bei der Fehlermeldung selber</param>
    8. ''' <param name="FontBold">optional FETTE Schrift (default:= false =>
    9. ' keine Berücksichtigung) </param>
    10. ''' <param name="FontColor">optional Schriftfarbe (default:=
    11. ' automatische Farbe)</param>
    12. ''' <param name="FontSize">optional Schriftgröße (default:= -1 ...
    13. ' deaktiviert)</param>
    14. ''' <param name="WithErrorMessage">optional mit Fehlermeldung (default:=
    15. ' true)</param>
    16. ''' <remarks></remarks>
    17. Public Sub AddBookmark(BookmarkName As String, Text As String, _
    18. Optional CreateError As Boolean = True, _
    19. Optional FontBold As Boolean = False, _
    20. Optional FontColor As Word.WdColorIndex = _
    21. Word.WdColorIndex.wdAuto, _
    22. Optional FontSize As Integer = -1, _
    23. Optional WithErrorMessage As Boolean = True)
    24. If _oDoc.Bookmarks.Exists(BookmarkName) Then
    25. Try
    26. _oWord.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, _
    27. Name:=BookmarkName)
    28. If FontBold = True Then _oWord.Selection.Font.Bold = True
    29. _oWord.Selection.Font.ColorIndex = FontColor
    30. If FontSize > 0 Then
    31. _oWord.Selection.Font.Size = FontSize
    32. End If
    33. _oDoc.Bookmarks.Item(BookmarkName).Range.Text = Text
    34. Catch ex As Exception
    35. AddError4BookmarkTryCatch(BookmarkName, ex.ToString)
    36. End Try
    37. Else
    38. If BookmarkName.ToUpper = "ERROR" Then Exit Sub ' damit es keine
    39. ' Endlosscheife wird
    40. If WithErrorMessage = True Then
    41. AddError4BookmarkMissing(BookmarkName)
    42. End If 'WithErrorMessage
    43. End If 'Bookmarks.Exists
    44. End Sub


    Habe ich da vielleicht irgendetwas übersehen oder hat einer von Euch eine Idee woran es liegen kann??

    Oder gibt es vielleicht spezielle Eigenarten von Word, wo das nicht klappen könnte?

    Gruß Jan
    Das Problem liegt wohl daran, dass das Bookmark.Range.End-Property nicht neu gesetzt wird.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. ''' <summary>
    2. ''' Setzen eines Bookmarks
    3. ''' </summary>
    4. ''' <param name="BookmarkName"></param>
    5. ''' <param name="Text"></param>
    6. ''' <param name="CreateError">optional Schreiben einer Fehlermeldung (default=true) - false bei der Fehlermeldung selber</param>
    7. ''' <param name="FontBold">optional FETTE Schrift (default:= false => keine Berücksichtigung) </param>
    8. ''' <param name="FontColor">optional Schriftfarbe (default:= automatische Farbe)</param>
    9. ''' <param name="FontSize">optional Schriftgröße (default:= -1 ... deaktiviert)</param>
    10. ''' <param name="WithErrorMessage">optional mit Fehlermeldung (default:= true)</param>
    11. ''' <remarks></remarks>
    12. Public Sub AddBookmark(BookmarkName As String, Text As String,
    13. Optional CreateError As Boolean = True,
    14. Optional FontBold As Boolean = False,
    15. Optional FontColor As Word.WdColorIndex =
    16. Word.WdColorIndex.wdAuto,
    17. Optional FontSize As Integer = -1,
    18. Optional WithErrorMessage As Boolean = True)
    19. If _oDoc.Bookmarks.Exists(BookmarkName) Then
    20. Try
    21. Dim _oRange As Word.Range = _oDoc.Bookmarks.Item(BookmarkName).Range
    22. _oRange.Text = Text
    23. _oRange.End = _oRange.Start + Text.Length
    24. _oRange.Font.ColorIndex = FontColor
    25. If FontBold = True Then _oRange.Bold = 1
    26. If FontSize > 0 Then _oRange.Font.Size = FontSize
    27. Catch ex As Exception
    28. AddError4BookmarkTryCatch(BookmarkName, ex.ToString)
    29. End Try
    30. Else
    31. If BookmarkName.ToUpper = "ERROR" Then Exit Sub ' damit es keine Endlosscheife wird
    32. If WithErrorMessage = True Then
    33. AddError4BookmarkMissing(BookmarkName)
    34. End If 'WithErrorMessage
    35. End If 'Bookmarks.Exists
    36. End Sub
    Hallo Henry,

    danke für den Tipp. Werde ich die Tage ausprobieren wenn ich wieder. Im Office.

    Mich hAt nur gewundert das es bisher anderswo funktioniert hat und nur nicht bei dem Bookmark nicht.

    Hatte sowieso immer Verständnisproblem mit dem Code den ich auf Grundlage einer alten Implementierung erstellt habe.

    Jetzt wäre alles etwas klarer.

    Gruß Jan