none
Problems when sorting lists of words in MS-Word files via VB.NET RRS feed

  • Question

  • Hello, Group Members:

    I am learning VB.NET with Visual Studio 2008 Express (Windows XP Pro, SP 3, Word 2k3). So far I have managed to convert some of my VBA bits and can now manipulate texts in MS-Word's files from my VS 2K8 Express. Below you will find a VB.NET sub that sorts words in an rtf file. The sorting of words within a table is fine, but when if I want to sort plain text the sub is stopped by a message from MS-Word that the header and data separator are not defined. When I click OK the sub is resumed, the words get sorted, and MS-Word session terminated. I have isolated the offending lines:

       oWord.ActiveDocument.Content.SortDescending()

       oWord.ActiveDocument.Content.Sort(ExcludeHeader:=False)

       oWord.ActiveDocument.Content.Sort(Separator:=False)

    but I have been stuck for two days. I do not know what is wrong with these lines. Thank you for any hints and/or suggestions,

    Mariusz

    PS.

    The project with this code is referenced to the Microsoft Word 11.0 Object Library and I would appreciate if someone took the time and tried to replicate this situation. The code itself is save to run and should not throw any exeptions.

     

    Imports Word = Microsoft.Office.Interop.Word

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim oWord As New Word.Application

      Dim oDoc As Word.Document

      Try

       oDoc = oWord.Documents.Open("C:\Documents and Settings\Mariusz\Moje dokumenty\Test.rtf", Word.WdOpenFormat.wdOpenFormatAuto)

       oDoc.Activate()

       oWord.Visible = True

       Dim myStoryRange As Word.Range

       For Each myStoryRange In oDoc.StoryRanges

        With myStoryRange.Find

         .ClearFormatting()

         .Replacement.ClearFormatting()

         .Text = "[ ]{2;}"

         .Replacement.Text = " "

         .Forward = True

         .Wrap = Word.WdFindWrap.wdFindContinue

         .MatchWildcards = True

         .Execute(Replace:=Word.WdReplace.wdReplaceAll)

        End With

        With myStoryRange.Find

         .ClearFormatting()

         .Replacement.ClearFormatting()

         .Text = " "

         .Replacement.Text = "^p"

         .Forward = True

         .Wrap = Word.WdFindWrap.wdFindContinue

         .MatchWildcards = False

         .Execute(Replace:=Word.WdReplace.wdReplaceAll)

        End With

        With myStoryRange.Find

         .ClearFormatting()

         .Replacement.ClearFormatting()

         .Text = "[^13]{2;}"

         .Replacement.Text = ""

         .Forward = True

         .Wrap = Word.WdFindWrap.wdFindContinue

         .MatchWildcards = True

         .Execute(Replace:=Word.WdReplace.wdReplaceAll)

        End With

       Next myStoryRange

     

    'sorting of words within a table is OK.

       oWord.ActiveDocument.Content.ConvertToTable()

       oWord.ActiveDocument.Tables(1).SortDescending()

       oWord.ActiveDocument.Tables(1).Sort(ExcludeHeader:=False)

     

    'at this point the sorting of words in a file is paused by a message: the header and data separator are not defined.

     

       oWord.ActiveDocument.Content.SortDescending()

       oWord.ActiveDocument.Content.Sort(ExcludeHeader:=False)

       oWord.ActiveDocument.Content.Sort(Separator:=False)

     

       MessageBox.Show("Check the result.")

     

    'and the words get sorted.

     

      Catch ex As Exception : MessageBox.Show("File access error.")

      Finally

       oWord.ActiveDocument.Close(SaveChanges:=False)

       oWord.Quit()

       'oDoc = Nothing

       'oWord = Nothing

      End Try

    End Sub

    End Class

     

     


    Mariusz
    • Moved by Mike Feng Tuesday, April 26, 2011 2:50 AM VSTO (From:Visual Basic Express Edition)
    Sunday, April 24, 2011 10:11 AM

Answers

  • Hi Mariusz,

    I'm using VS2010 so I can't be certain that there wasn't a change, but from what I understand both the ExcludeHeader and Separator properties are optional. The default for ExcludeHeader is False so the only value needed would be True if you wanted the first Paragraph ignored in the sorting, which I would doubt you do.

    As for the Separator property is is not a True/False value. It designates field separators between what is to be sorted. For example comma, if you had a comma delimited file.

    So I think the only thing you need to replace those two lines is:

    oWord.ActiveDocument.Content.Sort

    Hope this helps


    Regards, Rich
    • Marked as answer by mariuszJa Tuesday, April 26, 2011 1:53 PM
    Tuesday, April 26, 2011 3:31 AM

All replies

  • Hello Oakwinds,

     

    Welcome to MSDN forum.

     

    For Office related questions, you could also try the VSTO queue:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/threads

    and Word for Developers

    http://social.msdn.microsoft.com/Forums/en-US/worddev/threads

     

    And I have moved the thread to the VSTO queue.

     

    Thank you for your understanding and support.

     

    Best Regards,
    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 26, 2011 2:50 AM
  • Hi Mariusz,

    I'm using VS2010 so I can't be certain that there wasn't a change, but from what I understand both the ExcludeHeader and Separator properties are optional. The default for ExcludeHeader is False so the only value needed would be True if you wanted the first Paragraph ignored in the sorting, which I would doubt you do.

    As for the Separator property is is not a True/False value. It designates field separators between what is to be sorted. For example comma, if you had a comma delimited file.

    So I think the only thing you need to replace those two lines is:

    oWord.ActiveDocument.Content.Sort

    Hope this helps


    Regards, Rich
    • Marked as answer by mariuszJa Tuesday, April 26, 2011 1:53 PM
    Tuesday, April 26, 2011 3:31 AM
  • Hi Rich,

    Thank you for answer.

    Meanwhile, I have discovered that the code i used for sorting words within a table should not be used for words in plain text and that the code for plain text should read:

    oWord.ActiveDocument.Content.Sort(ExcludeHeader:=False, FieldNumber:=1, SortOrder:=1, CaseSensitive:=True)

    and the words are sorted from Z to A without the messege from MS-Word.

    But when I modify the code for the ascending order:

    oWord.ActiveDocument.Content.Sort(ExcludeHeader:=False, FieldNumber:=1, SortOrder:=2, CaseSensitive:=True)

    I get the „the file access error” message from the VB NET sub.

    But then again, when I use your line the words are sorted from A-Z

    oWord.ActiveDocument.Content.Sort

    Thank you for your time,

    Mariusz

     


    Mariusz
    Tuesday, April 26, 2011 1:56 PM