none
Macro for duplicating date from datepicker in word 2007 RRS feed

  • Question

  • Hi there,

    I'm new to the field of macros. I would like to get the macro for duplicating a date, which is selected using date picker into same word file in a different paragraph.

    Appreciate your kind support. 

    Thanks in advance,
    Saturday, February 2, 2013 5:41 AM

Answers

  • Saturday, February 2, 2013 6:07 AM
  • The links I gave you show how you can use content control mapping to link your content controls, so that updating any of the ones mapped to a given XML node will update all that are mapped to that node, without the need for the document to have macros once the mapping has been done. The links include references to utilities to create the mapping.

    Since you seem to prefer a macro solution, try something based on the following:

    Suppose your DatePicker content control has the title 'Source' and you have a text content control with the title 'Target'. The following macro, placed in the document's 'ThisDocument' module will update the 'Target' content control any time the 'Source' content control is updated.

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim CCtrl As ContentControl
    If ContentControl.Title = "Source" Then
      For Each CCtrl In ActiveDocument.ContentControls
        If CCtrl.Title = "Target" Then
          With CCtrl
            .LockContents = False
            .Range.Text = ContentControl.Range.Text
            .LockContents = True
          End With
          Exit For
        End If
      Next
    End If
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, February 2, 2013 8:31 AM

All replies

  • Saturday, February 2, 2013 6:07 AM
  • Dear Paul,

    Thanks for your reply.

    I check those links, but i couldn't get the answer for my question. Wat i need to get is, if i hav selected one date using datepicker, need to get the same date in another paragraph. like we  used to do in macro for textform field.

    thanks in advance, 

    Saturday, February 2, 2013 7:43 AM
  • Hi Ifthi

    By "date picker" do you mean a Date type of content control?

    If yes, then the links Paul gave you are what you need. You need to map all the content controls that should display the same information to a Custom XML Part. The information in those links explains how that is done.

    If you aren't using a content control data picker, please specifiy exactly what this "date picker" is.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, February 2, 2013 8:30 AM
    Moderator
  • The links I gave you show how you can use content control mapping to link your content controls, so that updating any of the ones mapped to a given XML node will update all that are mapped to that node, without the need for the document to have macros once the mapping has been done. The links include references to utilities to create the mapping.

    Since you seem to prefer a macro solution, try something based on the following:

    Suppose your DatePicker content control has the title 'Source' and you have a text content control with the title 'Target'. The following macro, placed in the document's 'ThisDocument' module will update the 'Target' content control any time the 'Source' content control is updated.

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim CCtrl As ContentControl
    If ContentControl.Title = "Source" Then
      For Each CCtrl In ActiveDocument.ContentControls
        If CCtrl.Title = "Target" Then
          With CCtrl
            .LockContents = False
            .Range.Text = ContentControl.Range.Text
            .LockContents = True
          End With
          Exit For
        End If
      Next
    End If
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, February 2, 2013 8:31 AM
  • Dear Paul & Cindy

    Thanks for your kind support and giving time to look my question.

    I got the solution.. Now its working with both Macro and as a link too.

    Once again thanks for supporting,

    Regards,

    Saturday, February 2, 2013 8:45 AM
  • Saturday, February 2, 2013 10:51 AM
  • Paul,

    That will only work if "target" is in the main text story range.  Otherwise you have to loop through the storyranges similar to vba find and replace.  A more direct method (as you are only interested in a single control titled "target") that will work is:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim CCtrl As ContentControl
    If ContentControl.Title = "Source" Then
      Set CCtrl = ActiveDocument.SelectContentControlsByTitle("Target").Item(1)
      With CCtrl
        .LockContents = False
        .Range.Text = ContentControl.Range.Text
        .LockContents = True
      End With
    End If
    End Sub

    Still, mapping the CC is the way to go and I don't see why the OP didn't find the solution in the links you provided.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Saturday, February 2, 2013 3:17 PM
  • If the target was in a header/footer, I'd probably opt for a StyleRef field solution. No code required.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, February 3, 2013 4:42 AM