Results 1 to 4 of 4

Thread: Word VBA - problem with extracting date from datepicker in userform with multipage

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    5

    Word VBA - problem with extracting date from datepicker in userform with multipage

    I have a problem when extracting the date from a datepicker in userform with multipage.
    When I open the userform and I select a date on the first page of my multipage and click ok, the date value is transferred to the bookmark in my word document;
    When I do the same with the datepicker on the second page, it transfers the date to my second bookmark in my word document; however, it will only fill up the bookmark with the date of the multipage that was last shown/visible when clicking 'ok', the date of the other datepicker is shown in the word-document as "0:00:00". (no error is given)
    I made a test document so that no other code is involved and get the same problem.

    Anyone any idea on what I should do to have both dates copied into my word document?


    This is the code I use:

    Private Sub CommandButton1_Click()
    Dim bmDate As Range
    Set bmDate = ActiveDocument.Bookmarks("date").Range
    Dim bmDate2 As Range
    Set bmDate2 = ActiveDocument.Bookmarks("date2").Range
    With bmDate
    .Text = Me.MultiPage1.Pages(0).DTPicker1.Value
    .Italic = False
    .Bold = False
    .Font.ColorIndex = wdBlack
    End With
    ActiveDocument.Bookmarks.Add Name:="date", Range:=bmDate

    With bmDate2
    .Text = Me.MultiPage1.Pages(1).DTPicker2.Value
    .Italic = False
    .Bold = False
    .Font.ColorIndex = wdBlack
    End With
    ActiveDocument.Bookmarks.Add Name:="date2", Range:=bmDate2
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word VBA - problem with extracting date from datepicker in userform with multipag

    I made a test document so that no other code is involved and get the same problem.
    post this doc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Word VBA - problem with extracting date from datepicker in userform with multipag

    Hi 4G,

    I can confirm your issue with retrieving the value of DTPicker that is not on the active page. This is a strange one indeed. I ended up writing a function to set the MP's page first before retrieving the DTPicker's value.

    Code:
    Public Function GetMPBoundDTPickerValue(picker As MSComCtl2.DTPicker) As Date
       Dim cntrl As msforms.Control
       Set cntrl = picker         ' need to cast picker as control to expose Parent property
       Dim page As msforms.page
       Set page = cntrl.Parent
       
       Dim MP As msforms.MultiPage
       Set MP = page.Parent
       
       Dim currentPage As Integer
       currentPage = MP.Value
       If page.Index <> currentPage Then
          MP.Value = page.Index
          GetMPBoundDTPickerValue = picker.Value
          MP.Value = currentPage
       Else
          GetMPBoundDTPickerValue = picker.Value
       End If
    End Function
    Usage:
    Code:
    With bmDate
        .Text = GetMPBoundDTPickerValue(Me.MultiPage1.Pages(0).DTPicker1)
    Hopefully this will solve your issue.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    5

    Thumbs up Re: Word VBA - problem with extracting date from datepicker in userform with multipag

    Quote Originally Posted by TnTinMN View Post
    Hi 4G,

    I can confirm your issue with retrieving the value of DTPicker that is not on the active page. This is a strange one indeed. I ended up writing a function to set the MP's page first before retrieving the DTPicker's value.

    Code:
    Public Function GetMPBoundDTPickerValue(picker As MSComCtl2.DTPicker) As Date
       Dim cntrl As msforms.Control
       Set cntrl = picker         ' need to cast picker as control to expose Parent property
       Dim page As msforms.page
       Set page = cntrl.Parent
       
       Dim MP As msforms.MultiPage
       Set MP = page.Parent
       
       Dim currentPage As Integer
       currentPage = MP.Value
       If page.Index <> currentPage Then
          MP.Value = page.Index
          GetMPBoundDTPickerValue = picker.Value
          MP.Value = currentPage
       Else
          GetMPBoundDTPickerValue = picker.Value
       End If
    End Function
    Usage:
    Code:
    With bmDate
        .Text = GetMPBoundDTPickerValue(Me.MultiPage1.Pages(0).DTPicker1)
    Hopefully this will solve your issue.
    Thanks! This solved it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width