none
Space in c# wordAddIn project RRS feed

  • Question

  • Hi guys,
    I devekop a c# wordAddIn project. I want to get space BEFORE generation bookmarks.I had developed a method that adds space after generating bookmarks :
     public static void CreateSimpleField (string itemName, string bookmarkName, Word.Application application)
        {
          object index = 1;
     
          if (application.Windows.get_Item(ref index).View.ShowBookmarks == false)
            application.Windows.get_Item(ref index).View.ShowBookmarks = true;
     
          Word.Selection   currentSelection = application.Selection;
          Word.Range     rng = currentSelection.Range;
          
          rng.Text = itemName;
          
     
          while (application.ActiveDocument.Bookmarks.Exists(bookmarkName))
          {bookmarkName += "_";}
    		  
          Object       range = rng;
          application.ActiveDocument.Bookmarks.Add (bookmarkName, ref range);
          // Ajouter un espace.
          int       indexRange = rng.End + 1;
          currentSelection.s.SetRange(indexRange, indexRange);
          currentSelection.TypeText(" ");
        }
    
    How to transform it to get space BEFORE bookmarks?
    Also, when I generate, it appears like this :
    [bookmarkName]
    
    How can I remove the accolade []????
    ty Smile | :) 
    Monday, May 9, 2011 12:42 PM

All replies

  • Hi Ty

    What you want to do is possible, I'm sure. I'm just not clear on exactly what you want to do:

    Do you want the space inside the bookmark: [ bookmark] or just before the bookmark?

    And would the space already exist in the document or would you need to be adding it?

    The [bracketss] around the bookmark are visual, only, and are a display setting in Word. Some people will have it on (as I do) and others will not (not on is the default).  I wouldn't be happy if you turned it off (and left it off) on my installation, so if you change it for certain tasks you should be sure to change it back.

    The setting "Show Bookmarks" is in Word's "Options", but without knowing the version of Word you're using, I can't give you detailed instructions on how to find it.

    In the object modell it's windowObject.View.ShowBookmarks = True (or False)


    Cindy Meister, VSTO/Word MVP
    Monday, May 9, 2011 12:50 PM
    Moderator
  • Hi Cindy,

    I use MS Word 2007.

    I want the space BEFORE [] like this :

    ESPACE[bookmarkName1]ESPACE[BooKmarkName2]

    for campany reason. I use 

     

     // Add a space.
      int  indexRange = rng.End + 1;
      currentSelection.s.SetRange(indexRange, indexRange);
      currentSelection.TypeText(" ");
    

    but it inserts space AFTER [].

    For the [] delete, I don't really understand what u mean$

    thank u

    ty


    Monday, May 9, 2011 12:58 PM
  • Hi Ty

    <<For the [] delete, I don't really understand what u mean$>>

    You cannot delete the [brackets]. If you do that, you delete the bookmark. You can only turn off the display of the [ ]. "Office button" (top left of Word, big round "pizza")/Word Options/Advanced/Show Document Content/Show Bookmarks checkbox. (I'm sorry, I don't know the commands in French!)

    <<I want the space BEFORE [] like this :

    ESPACE[bookmarkName1]ESPACE[BooKmarkName2]>>

    I'd start at this point:

      rng.Text = " " + itemName;

    Then I'd "shorten" the range by a single character:

      rng.MoveStart(Unit : Word.WdUnit.wdCharacter, Count : 1);

    And put the bookmark around this shortened range:

    application.ActiveDocument.Bookmarks.Add (bookmarkName, ref range);


    Cindy Meister, VSTO/Word MVP
    Monday, May 9, 2011 1:35 PM
    Moderator
  • Hi cindy,

    I did what u told me to display the brackets but nothing is done, still bookmarks (do I need a restart of computer to apply that?).

    For the code u gave me, it doesn't work. it show me an error.I use it like this :

    object index = 1;
    
          if (application.Windows.get_Item(ref index).View.ShowBookmarks == false)
            application.Windows.get_Item(ref index).View.ShowBookmarks = true;
    
          Word.Selection   currentSelection = application.Selection;
          Word.Range     rng = currentSelection.Range;
    			rng.Text = " " + itemName;
    			object unit = Word.WdUnits.wdWord;
    			object count = 1; 
    application.Selection.MoveStart(ref unit, ref count);
    			application.ActiveDocument.Bookmarks.Add (bookmarkName, ref range);
    

    ty

    Monday, May 9, 2011 2:03 PM
  • Hi ty

    What error message and which line of code is triggering it?

    You're using the wrong Enum value here:

       object unit = Word.WdUnits.wdWord;

    This should be Word.WdUnits.wdCharacter;

    <<I did what u told me to display the brackets but nothing is done, still bookmarks (do I need a restart of computer to apply that?).>>

    The code you show will display the bookmarks, not hide them. You should be setting ShowBookmarks = False to hide the bookmarks.

    If they aren't hiding, are you able to select and delete the [ ] characters in the document?


    Cindy Meister, VSTO/Word MVP
    Monday, May 9, 2011 2:55 PM
    Moderator
  • Hi Cindy,

    1) well the brackets are hidden finally. I changed like this :

     application.Windows.get_Item(ref index).View.ShowBookmarks = false;
    

    2) for the space before still not working. I want to have like this :

    ESPACE[bookmarkName1]ESPACE[bookmarkName2]......

    ty

    Monday, May 9, 2011 3:32 PM
  • Hi Ty

    <<2) for the space before still not working. I want to have like this :

    ESPACE[bookmarkName1]ESPACE[bookmarkName2]......>>

    And I asked you what the error message is and what line of code is triggering it. I also pointed out an error in your code...


    Cindy Meister, VSTO/Word MVP
    Monday, May 9, 2011 4:31 PM
    Moderator
  • Hi cindy,

    the error doesn't appear but the space is added in the begining of every bookmark like this :

    [ESPACE Bookmark1]

    me, I want the space on the left of every bracket  ESPACE[bookmark1]

    ty

    Tuesday, May 10, 2011 8:04 AM