none
How to remove right margin and clear tab stops in word document? RRS feed

  • Question

  • I am new to word/office development and I am trying to develop an add-in for the user to do minor modifications to the word document that they open. 

    I have office 2013 and Visual Studio 2015 installed on my machine.

    I am going through the javascript api library but I have a hard time figuring out from past 8 hours, which objects/APIs help me in accomplishing these tasks. Can anyone help me start on the right foot? 

    my requirements are simple for the add-in

    1.        Accept all changes in document and stop Change Tracking
    2.        Remove all styles associated with “Deltaview”
    3.        Convert Autonumbers
    4.        Replace all non-breaking spaces with regular spaces (Replace: ^s with space)
    5.        Replace (2) spaces with (1) space (only run one time).
    6.        Clear all tab stops
    7.        Ensure that there is only one blank paragaph break between each item (text, table, etc.), remove extra breaks as necessary.
    8.        Remove right margin 

    Thanks,

    Lavanya

    Thursday, February 4, 2016 7:35 PM

Answers

  • Hi Lavanya,

    There are too many questions/issues in a thread. I'd suggest asking separate questions in new forum threads. Thus, forum readers may recognize questions and answers easily.

    For tracking changes of a document, as far as I know, there isn’t the JavaScript API to do it. You may build a VSTO add-in to achieve that. For example:

    The Document.AcceptAllRevisions method accepts all tracked changes in the specified document.

    The Document.TrackRevisions method determines if changes are tracked in the specified document.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by lchigurupati Monday, February 8, 2016 5:15 PM
    Friday, February 5, 2016 7:53 AM
    Moderator
  • You could do all of that with a macro like:

    Sub Document_CleanUp()
    Application.ScreenUpdating = False
    Dim i As Long, ArrStl
    ArrStl = Array("Style1", "Style2", "Style3", "Style4")
    With ActiveDocument
      .TrackRevisions = False
      .Revisions.AcceptAll
      For i = 0 To UBound(ArrStl)
        .Styles(ArrStl(i)).Delete
      Next
      With .Range
        .ListFormat.ConvertNumbersToText (wdNumberAllNumbers)
        .ParagraphFormat.TabStops.ClearAll
        .ParagraphFormat.RightIndent = 0
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          '.MatchWildcards = True
          '.Text = "^t"
          '.Replacement.Text = ""
          .Execute Replace:=wdReplaceAll
          .Text = "^s"
          .Replacement.Text = " "
          .Execute Replace:=wdReplaceAll
          .Text = "[ ]{2,}"
          .Execute Replace:=wdReplaceAll
          .Text = "[ ]^13"
          .Replacement.Text = "^p"
          .Execute Replace:=wdReplaceAll
          .Text = "[^13]{2,}"
          .Execute Replace:=wdReplaceAll
          '.Text = "[^13]"
          '.Replacement.Text = "^p^p"
          '.Execute Replace:=wdReplaceAll
        End With
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    Simply save the macro in a global template (i.e. saved to the Word startup folder) and it'll be available at all times. See: https://msdn.microsoft.com/en-us/library/office/gg597509%28v=office.14%29.aspx?

    Since I don't know what the "styles associated with Deltaview" are, I've simply defined an array with the names "Style1", "Style2", "Style3", "Style4" that you can change to the correct names. You can also expand/contract the array.

    There's some ambiguities about your specifications:
    1. You say you want to remove all tabstops, but that isn't the same as removing tabs. I've included code for both (the code for removing tabs is commented-out).
    2. You say want "only one blank paragaph break between each item". In a properly-formatted document the only blank spaces between paragraphs is that created via paragraph before/after space settings, preferably managed by Style definitions, rather than by inserting empty paragraphs. As coded, the macro assumes that is how they're managed, but there's a second set of commented-out code to insert empty paragraphs if that's what you really want.

    3. You say you want to "Remove right margin", but that's impossible. I assume you actually want to remove right indents, which the code does.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, February 5, 2016 9:37 PM typo
    • Marked as answer by lchigurupati Monday, February 8, 2016 5:15 PM
    Friday, February 5, 2016 10:45 AM

All replies

  • Hi Lavanya,

    There are too many questions/issues in a thread. I'd suggest asking separate questions in new forum threads. Thus, forum readers may recognize questions and answers easily.

    For tracking changes of a document, as far as I know, there isn’t the JavaScript API to do it. You may build a VSTO add-in to achieve that. For example:

    The Document.AcceptAllRevisions method accepts all tracked changes in the specified document.

    The Document.TrackRevisions method determines if changes are tracked in the specified document.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by lchigurupati Monday, February 8, 2016 5:15 PM
    Friday, February 5, 2016 7:53 AM
    Moderator
  • You could do all of that with a macro like:

    Sub Document_CleanUp()
    Application.ScreenUpdating = False
    Dim i As Long, ArrStl
    ArrStl = Array("Style1", "Style2", "Style3", "Style4")
    With ActiveDocument
      .TrackRevisions = False
      .Revisions.AcceptAll
      For i = 0 To UBound(ArrStl)
        .Styles(ArrStl(i)).Delete
      Next
      With .Range
        .ListFormat.ConvertNumbersToText (wdNumberAllNumbers)
        .ParagraphFormat.TabStops.ClearAll
        .ParagraphFormat.RightIndent = 0
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          '.MatchWildcards = True
          '.Text = "^t"
          '.Replacement.Text = ""
          .Execute Replace:=wdReplaceAll
          .Text = "^s"
          .Replacement.Text = " "
          .Execute Replace:=wdReplaceAll
          .Text = "[ ]{2,}"
          .Execute Replace:=wdReplaceAll
          .Text = "[ ]^13"
          .Replacement.Text = "^p"
          .Execute Replace:=wdReplaceAll
          .Text = "[^13]{2,}"
          .Execute Replace:=wdReplaceAll
          '.Text = "[^13]"
          '.Replacement.Text = "^p^p"
          '.Execute Replace:=wdReplaceAll
        End With
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    Simply save the macro in a global template (i.e. saved to the Word startup folder) and it'll be available at all times. See: https://msdn.microsoft.com/en-us/library/office/gg597509%28v=office.14%29.aspx?

    Since I don't know what the "styles associated with Deltaview" are, I've simply defined an array with the names "Style1", "Style2", "Style3", "Style4" that you can change to the correct names. You can also expand/contract the array.

    There's some ambiguities about your specifications:
    1. You say you want to remove all tabstops, but that isn't the same as removing tabs. I've included code for both (the code for removing tabs is commented-out).
    2. You say want "only one blank paragaph break between each item". In a properly-formatted document the only blank spaces between paragraphs is that created via paragraph before/after space settings, preferably managed by Style definitions, rather than by inserting empty paragraphs. As coded, the macro assumes that is how they're managed, but there's a second set of commented-out code to insert empty paragraphs if that's what you really want.

    3. You say you want to "Remove right margin", but that's impossible. I assume you actually want to remove right indents, which the code does.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Friday, February 5, 2016 9:37 PM typo
    • Marked as answer by lchigurupati Monday, February 8, 2016 5:15 PM
    Friday, February 5, 2016 10:45 AM
  • Thank you for pointing me in the right direction. That is a huge help. 

    Also thank you for the tips. I appreciate it very much

    Friday, February 5, 2016 9:23 PM
  • Hi lchigurupati,

    You could mark the reply as answer, which helps you to solve the question/issue.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 8, 2016 2:47 AM
    Moderator
  • I am programming it in C#, visual studio 2015 for Office 2013. 

    I have a hard time figuring out which C# class/object is relative to ActiveDocument or offers a method/property TrackRevisions. I am wondering if you could explain a little more for me? Thanks.

    Tuesday, February 9, 2016 6:46 PM
  • Thank you for answering my question in detail. It helped me great deal. I figured out how to establish it in my c# application.

    The only problem I have now is to find a style that has the text "deltaview" and then remove it. 

    I am trying to find a style object by searching its description and I was not able to find it. 

     //2. Remove all styles associated with “Deltaview”
                Word.Range rng = app.ActiveDocument.Range();
                List<string> deltaStyles = new List<string>();

                for(int i=0; i < app.ActiveDocument.Styles.Count; i++)
                {
                    if(app.ActiveDocument.Styles[i].Description.Contains("DeltaView"))
                        {
                        deltaStyles.Add(app.ActiveDocument.Styles[i].NameLocal);
                    }
                }

                for (int i=0; i<deltaStyles.Count; i++)
                {
                    MessageBox.Show(deltaStyles[i]);
                    app.ActiveDocument.Styles[deltaStyles[i]].Delete();
                }

    Please help.

    Thank you.
    Tuesday, February 9, 2016 10:31 PM
  • If anyone is looking for c# equivalent of the code. Here is what I have done. 

    Create a VSTO addin application and then add a user control with task pane. To that control I added buttons that handle my requirements.

    public partial class MyUserControl : UserControl
        {
           Word.Application app = Globals.ThisAddIn.Application;
            public MyUserControl()
            {
                InitializeComponent();
            }

            private void BtnStopChangeTracking_Click(object sender, EventArgs e)
            {
                // 1. Accept all changes in document and stop Change Tracking
                app.ActiveDocument.TrackRevisions = false;
                app.ActiveDocument.AcceptAllRevisions();
                // app.ActiveDocument.Paragraphs.SpaceAfter = 0;
            }

            private void BtnClearTabStops_Click(object sender, EventArgs e)
            {
                //6. Clear all tab stops
                Word.Range rng = app.ActiveDocument.Range();
                rng.ParagraphFormat.TabStops.ClearAll();
            }

            private void BtnConvertAutoNumbers_Click(object sender, EventArgs e)
            {
                //3. Convert Autonumbers
                Word.Range rng = app.ActiveDocument.Range();
                rng.ListFormat.ConvertNumbersToText(Word.WdNumberType.wdNumberAllNumbers);
            }

            private void BtnRemoveRightMargin_Click(object sender, EventArgs e)
            {
                //8. Remove right margin
                Word.Range rng = app.ActiveDocument.Range();
                rng.ParagraphFormat.RightIndent = 0;
            }

            private void BtnEnforceSingleParagraphBreak_Click(object sender, EventArgs e)
            {
                //7. Ensure that there is only one blank paragaph break between each item(text, table, etc.), remove extra breaks as necessary.
                Word.Range rng = app.ActiveDocument.Range();
                rng.Find.Text = "[ ]^13";
                rng.Find.Replacement.Text = "^p";
                rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);

                rng.Find.Text = "[^13]{2,}";
                rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);

                //rng.Find.Text = "[^13]";
                //rng.Find.Replacement.Text = "^p^p";
                //rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
            }

            private void BtnRemoveDoubleSpaces_Click(object sender, EventArgs e)
            {
                //5. Replace(2) spaces with (1) space(only run one time).
                Word.Range rng = app.ActiveDocument.Range();
                rng.Find.Text = "[ ]{2,}";
                rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
            }

            private void BtnRemoveNBRSpaces_Click(object sender, EventArgs e)
            {
                //4. Replace all non - breaking spaces with regular spaces (Replace: ^ s with space)
                Word.Range rng = app.ActiveDocument.Range();
                rng.Find.ClearFormatting();

                rng.Find.Forward = true;
                rng.Find.Wrap = Word.WdFindWrap.wdFindContinue;
                rng.Find.Format = false;

                //rng.Find.MatchWildcards = true;
                //rng.Find.Text = "^t";
                //rng.Find.Replacement.Text = " ";
                //rng.Find.Execute(Replace:Word.WdReplace.wdReplaceAll);

                rng.Find.Text = "^s";
                rng.Find.Replacement.Text = " ";
                rng.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
            }

    }

    }

    Tuesday, February 9, 2016 10:34 PM
  • I don't do C# but, if you're trying to delete Styles containing the string 'DeltaView', you might do it like this in VBA:

    Dim i As Long
    With ActiveDocument
      For i = .Styles.Count To 1 Step -1
        If InStr(.Styles(i).NameLocal, "DeltaView") > 0 Then .Styles(i).Delete
      Next
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, February 10, 2016 12:38 AM