Results 1 to 5 of 5

Thread: Word automation numbering problem C#

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Word automation numbering problem C#

    I've written a program in C# (Visual Studio 2013) that generates educational worksheets to print out from Microsoft Word. I have a problem that I looked all of the internet to answer but it still has me stumped. Any help would be greatly appreciated.

    I generate a multiple choice worksheet that I cannot format. I get all the information and can hard code it to generate a formatted document, but whenever a question or possible answer is longer than 1 line of text the format really looks bad. I was hoping to automate bulleting/numbering but I do not know how to bullet/number in word automation. I've created a macro in Word but cannot translate it to C#. Help please. An example of how I want it to look is below:

    1) The question...
    a) option 1
    b) option 2
    Thanks

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Word automation numbering problem C#

    If you are able to select the range, the you should be able to apply the bullet formatting using;

    Code:
    Selection.Range.ListFormat.ApplyBulletDefault
    If that doesn't work please post the macro you have (that does work).


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Re: Word automation numbering problem C#

    Thanks for the reply. I figure I have to do something with the range that I'll have to dig deeper into. However this problem is still giving me fits. Below is the macro I recorded in Word 2013 but I'm having trouble converting the code to C#. Any help would be greatly appreciated.

    Code:
    Sub formatting()
    '
    ' formatting Macro
    '
    '
        Selection.TypeText Text:="This is a line of text."
        Selection.TypeParagraph
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1."
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleArabic
            .NumberPosition = InchesToPoints(0.25)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(0.5)
            .TabPosition = wdUndefined
            .ResetOnHigher = 0
            .StartAt = 1
            With .Font
                .Bold = wdUndefined
                .Italic = wdUndefined
                .StrikeThrough = wdUndefined
                .Subscript = wdUndefined
                .Superscript = wdUndefined
                .Shadow = wdUndefined
                .Outline = wdUndefined
                .Emboss = wdUndefined
                .Engrave = wdUndefined
                .AllCaps = wdUndefined
                .Hidden = wdUndefined
                .Underline = wdUndefined
                .Color = wdUndefined
                .Size = wdUndefined
                .Animation = wdUndefined
                .DoubleStrikeThrough = wdUndefined
                .Name = ""
            End With
            .LinkedStyle = ""
        End With
        ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
        Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
            ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
            False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
            wdWord10ListBehavior
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1)"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLowercaseLetter
            .NumberPosition = InchesToPoints(0.25)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(0.5)
            .TabPosition = wdUndefined
            .ResetOnHigher = 0
            .StartAt = 1
            With .Font
                .Bold = wdUndefined
                .Italic = wdUndefined
                .StrikeThrough = wdUndefined
                .Subscript = wdUndefined
                .Superscript = wdUndefined
                .Shadow = wdUndefined
                .Outline = wdUndefined
                .Emboss = wdUndefined
                .Engrave = wdUndefined
                .AllCaps = wdUndefined
                .Hidden = wdUndefined
                .Underline = wdUndefined
                .Color = wdUndefined
                .Size = wdUndefined
                .Animation = wdUndefined
                .DoubleStrikeThrough = wdUndefined
                .Name = ""
            End With
            .LinkedStyle = ""
        End With
        ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
        Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
            ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
            True, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
            wdWord10ListBehavior
        Selection.TypeText Text:="This is option q"
        Selection.TypeParagraph
        Selection.TypeText Text:="this is option 2"
        Selection.TypeParagraph
        Selection.TypeText Text:="this is option3 "
        Selection.TypeParagraph
        Selection.TypeText Text:="this is option r"
        Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
        Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
    End Sub

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Word automation numbering problem C#

    This will get you further down the track;

    Code:
                object missing = System.Reflection.Missing.Value;
    
                Microsoft.Office.Interop.Word.Application App = new Microsoft.Office.Interop.Word.Application();
                Document doc = App.Documents.Open(@"C:\Test.docx");
                App.Visible = true;
                Microsoft.Office.Interop.Word.Range rng = doc.Range(0, 0);
                Microsoft.Office.Interop.Word.Selection currentSelection = App.Selection;
    
                currentSelection.TypeText("Inserting this text.");
                currentSelection.TypeParagraph(); ;
    
                object n = 1;
                ListTemplate template =
                App.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);
                ListLevel level = template.ListLevels[1];
    
                level.NumberFormat = "%1.";
                level.TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
                level.NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
                level.NumberPosition = App.InchesToPoints(0.25f);
                level.Alignment = WdListLevelAlignment.wdListLevelAlignLeft;
                level.TextPosition = App.InchesToPoints(0.5f);
                level.TabPosition = (float)WdConstants.wdUndefined;
                level.ResetOnHigher = 0;
                level.StartAt = 1;
    
                level.Font.Bold = (int)WdConstants.wdUndefined;
                level.Font.Italic = (int)WdConstants.wdUndefined;
                level.Font.StrikeThrough = (int)WdConstants.wdUndefined;
                level.Font.Subscript = (int)WdConstants.wdUndefined;
                level.Font.Superscript = (int)WdConstants.wdUndefined;
                level.Font.Shadow = (int)WdConstants.wdUndefined;
                level.Font.Outline = (int)WdConstants.wdUndefined;
                level.Font.Emboss = (int)WdConstants.wdUndefined;
                level.Font.Engrave = (int)WdConstants.wdUndefined;
                level.Font.AllCaps = (int)WdConstants.wdUndefined;
                level.Font.Hidden = (int)WdConstants.wdUndefined;
                level.Font.Underline = WdUnderline.wdUnderlineNone;
                level.Font.Color = WdColor.wdColorAutomatic;
                level.Font.Size = (int)WdConstants.wdUndefined;
                level.Font.Animation = WdAnimation.wdAnimationNone;
                level.Font.DoubleStrikeThrough = (int)WdConstants.wdUndefined;
    
                level.LinkedStyle = "";
    
                template.Name = "";
                object bContinuePrevList = false;
                object applyTo = WdListApplyTo.wdListApplyToWholeList;
                object defBehavior = WdDefaultListBehavior.wdWord10ListBehavior;
                App.Selection.Range.ListFormat.ApplyListTemplateWithLevel(template, ref bContinuePrevList, ref applyTo, ref defBehavior, ref missing);
    
            }


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    Massachusetts
    Posts
    48

    Re: Word automation numbering problem C#

    Bulldog Thanks so much for the help. To get the formatting I wanted I had move the initial setting of the range out of my questions for loop and at the end of the for loop add the following code
    Code:
    oWord.Selection.Range.ListFormat.RemoveNumbers(ref missing);
    currentSelection.TypeParagraph();
    to get the numbering for each individual question set correctly, but because of your help it works!
    Thanks you saved me many headaches.
    Rcam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width