none
How to delete/remove the hidden text in word document

    Question

  • Hi,

    I am inserting a hidden field to Word Document using below C# code:

    Word.Range wmRange = null;
    docId = "Date: X ID:Y  ProductName: Z"
    object offset = (object)_wordDoc.Content.Start;
    wmRange = _wordDoc.Range(ref offset, ref offset);
    wmRange.InsertBefore(docId + "\n");
    wmRange.Font.Hidden = -1;
    wmRange.Font.Size = 6.0f;

    Now i want to delete the inserted hidden text. Put let me know how i can do this.

    Regards,


    Chetan Rajakumar

    Tuesday, December 11, 2012 9:44 AM

Answers

  • Hi Chetan

    Usually, we'd probably work with the Range.Find object, setting Find.Font.Hidden to true and Find.Replacement.Text = "" (empty string).

    You can test this in the UI and, if it works for you, record a VBA macro to get the basic syntax to work from.

    Ctrl+H will display the Replace dialog box. Click on the "More" button at the bottom left. Click on Format, choose Font, activate "Hidden", OK. Now click Replaced All.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 11, 2012 4:13 PM
  • Hi Cindy,

    The below code worked fine for me, :)

    object missing = System.Reflection.Missing.Value;
                object replaceAll = Word.WdReplace.wdReplaceAll;

                _wordApp.Selection.Find.ClearFormatting();
                _wordApp.Selection.Find.Font.Hidden = 1;

                _wordApp.Selection.Find.Replacement.ClearFormatting();
                _wordApp.Selection.Find.Text = "";
                _wordApp.Selection.Find.Replacement.Text = "";
                _wordApp.Selection.Find.Forward = true;
                _wordApp.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                _wordApp.Selection.Find.Format = true;
                _wordApp.Selection.Find.MatchCase = false;
                _wordApp.Selection.Find.MatchWholeWord = false;
                _wordApp.Selection.Find.MatchWildcards = false;
                _wordApp.Selection.Find.MatchSoundsLike = false;
                _wordApp.Selection.Find.MatchAllWordForms = false;

                _wordApp.Selection.Find.Execute(
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref replaceAll, ref missing, ref missing, ref missing, ref missing);

    Thanks alot.

    Regards,


    Chetan Rajakumar

    Wednesday, December 12, 2012 8:55 AM

All replies

  • Hi Chetan

    Usually, we'd probably work with the Range.Find object, setting Find.Font.Hidden to true and Find.Replacement.Text = "" (empty string).

    You can test this in the UI and, if it works for you, record a VBA macro to get the basic syntax to work from.

    Ctrl+H will display the Replace dialog box. Click on the "More" button at the bottom left. Click on Format, choose Font, activate "Hidden", OK. Now click Replaced All.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 11, 2012 4:13 PM
  • Thanks Cindy for the reply.

    I did as explained and i was able to remove the hidden text and i have recorded the macro, please find below. Now i have to do this from code side(C# code). Can you please let me know how this should be written in C# code,

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Font.Hidden = True
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

    Thanks in advance.

    Regards,


    Chetan Rajakumar

    Wednesday, December 12, 2012 3:49 AM
  • Hi Cindy,

    I tried below 2 code but it did not work :(  ,

    1. Word.Range wmRange = null;
        object offset = (object)_wordDoc.Content.Start;
         wmRange = _wordDoc.Range(ref offset, ref offset);
         wmRange.Find.Font.Hidden = 1;
         wmRange.Find.Replacement.Text = "";

    2.  Word.Selection sel = _wordApp.Selection;
                sel.Find.ClearFormatting();
                sel.Find.Font.Hidden = 1;
                sel.Find.Replacement.ClearFormatting();
                sel.Find.Text="";
                sel.Find.Replacement.Text = "";
                sel.Find.Forward = true;
                sel.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                sel.Find.Format = true;
                sel.Find.MatchCase = false;
                sel.Find.MatchWholeWord = false;
                sel.Find.MatchWildcards = false;
                sel.Find.MatchSoundsLike = false;
                sel.Find.MatchAllWordForms = false;

    Please let me know where i am going wrong.Thanks in advance.

    Regards,


    Chetan Rajakumar

    Wednesday, December 12, 2012 5:59 AM
  • Hi Chetan

    It always helps if you tell us HOW something doesn't work...

    However, in Word the numerical equivalent of True is usually -1, not 1. In addition, you must tell Find to perform the Find/Replace action: wmRange.Find.Execute(//parameters here);

    If you change these things do you get a better result?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 7:48 AM
  • Hi Cindy,

    The below code worked fine for me, :)

    object missing = System.Reflection.Missing.Value;
                object replaceAll = Word.WdReplace.wdReplaceAll;

                _wordApp.Selection.Find.ClearFormatting();
                _wordApp.Selection.Find.Font.Hidden = 1;

                _wordApp.Selection.Find.Replacement.ClearFormatting();
                _wordApp.Selection.Find.Text = "";
                _wordApp.Selection.Find.Replacement.Text = "";
                _wordApp.Selection.Find.Forward = true;
                _wordApp.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                _wordApp.Selection.Find.Format = true;
                _wordApp.Selection.Find.MatchCase = false;
                _wordApp.Selection.Find.MatchWholeWord = false;
                _wordApp.Selection.Find.MatchWildcards = false;
                _wordApp.Selection.Find.MatchSoundsLike = false;
                _wordApp.Selection.Find.MatchAllWordForms = false;

                _wordApp.Selection.Find.Execute(
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref replaceAll, ref missing, ref missing, ref missing, ref missing);

    Thanks alot.

    Regards,


    Chetan Rajakumar

    Wednesday, December 12, 2012 8:55 AM