locked
VBA API to check if current selection lis inside a ContentControl panel? RRS feed

  • Question

  • Hi,

    Is there any VBA call to check if the current selection lies inside a ContentControl item? Like, the one we have for Office 2013 : ActiveDocument.ActiveWindow.Selection.Range.Information (wdInContentControl).

    Thanks in advance,

    Parul


    Parul Gupta

    Wednesday, March 20, 2013 2:29 PM

Answers

  • Hi Parul

    Here's a function I've used. The way it's set up, it returns the content control if the selection is in one, otherwise it returns Nothing. But as you can see, I also set it up so that it can return a boolean True/False if you'd rather use that.

    Function IsSelectionInCC(sel As word.Selection) As word.ContentControl
        Dim rng As word.Range
        Dim doc As word.Document
        Dim nrCC As Long
        Dim cc As word.ContentControl
        Dim InCC As Boolean
        
        InCC = False
        Set rng = sel.Range
        Set doc = rng.Parent
        rng.Start = rng.Document.content.Start
        nrCC = rng.Contentcontrols.Count
        If nrCC > 0 Then
            If sel.InRange(doc.Contentcontrols(nrCC).Range) Then
                InCC = True 'Debug.Print ("Sel in cc")
                Set cc = doc.Contentcontrols(nrCC)
            Else
                sel.MoveEnd wdCharacter, 1
                If Len(sel) = 0 Then
                    'Debug.Print ("Sel at end of cc")
                    InCC = True
                    Set cc = doc.Contentcontrols(nrCC)
                End If
            End If
        End If
        Set IsSelectionInCC = cc
    End Function


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Ninja8 Thursday, March 21, 2013 1:53 PM
    Thursday, March 21, 2013 7:11 AM

All replies

  • Hi Parul

    Here's a function I've used. The way it's set up, it returns the content control if the selection is in one, otherwise it returns Nothing. But as you can see, I also set it up so that it can return a boolean True/False if you'd rather use that.

    Function IsSelectionInCC(sel As word.Selection) As word.ContentControl
        Dim rng As word.Range
        Dim doc As word.Document
        Dim nrCC As Long
        Dim cc As word.ContentControl
        Dim InCC As Boolean
        
        InCC = False
        Set rng = sel.Range
        Set doc = rng.Parent
        rng.Start = rng.Document.content.Start
        nrCC = rng.Contentcontrols.Count
        If nrCC > 0 Then
            If sel.InRange(doc.Contentcontrols(nrCC).Range) Then
                InCC = True 'Debug.Print ("Sel in cc")
                Set cc = doc.Contentcontrols(nrCC)
            Else
                sel.MoveEnd wdCharacter, 1
                If Len(sel) = 0 Then
                    'Debug.Print ("Sel at end of cc")
                    InCC = True
                    Set cc = doc.Contentcontrols(nrCC)
                End If
            End If
        End If
        Set IsSelectionInCC = cc
    End Function


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Ninja8 Thursday, March 21, 2013 1:53 PM
    Thursday, March 21, 2013 7:11 AM
  • Thanks Cindy. I had written a similar code but this one is much optimized.

    Parul Gupta

    Thursday, March 21, 2013 1:51 PM
  • hi, Cindy,

    "Len(sel)" is a VBA build-in function,  can you tell me the  equivalent c# code?

    is "(sel.Range.Text.Length == 0)"?

    thanks


    • Edited by sendreams Thursday, March 6, 2014 3:08 AM
    Thursday, March 6, 2014 3:05 AM
  • Hi sendreams

    Yes, the Length property is the correct .NET Framework equivalent :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, March 6, 2014 6:25 AM
  • thanks Cindy

    i trans your vba code to C# below, perhelps for anyone use

    Word.Range rng;
    Word.Document doc;
    int nrCC;
    Word.ContentControl cc = null;
    bool inCC = false;
    
    rng = sel.Range;
    doc = (Word.Document)rng.Parent;
    rng.Start = rng.Document.Content.Start;
    nrCC = rng.ContentControls.Count;
    if (nrCC > 0) {
    	if (sel.InRange(doc.ContentControls[nrCC].Range)){
    		inCC = true;
    		cc = doc.ContentControls[nrCC];
    	} else {
    		sel.MoveEnd(Word.WdUnits.wdCharacter, 1);
    		if (sel.Range.Text == null || sel.Range.Text.Length == 0) {
    			inCC = true;
    			cc = doc.ContentControls[nrCC];
    		}
    	}
    }
    return cc;

    Thursday, March 6, 2014 10:40 AM