user2576487
user2576487

Reputation: 13

How can I set a string value to the date value in a ContentControl DatePicker in MS-Word using VBA

I have an MS-Word document template that has an existing DatePicker Contentcontrol QuickPart imported from a SharePoint list.

I want to either:

I can then use that data to calculate and place future dates in my document based on the date read from the DatePicker value.

I have researched numerous sites using keywords date picker, content control, VBA, Word. No examples come close to what I am trying to do.

Sub AddDates()
'
' AddDates Macro
    Dim PODate As Date
    Dim strDate As String
    Set doc = ActiveDocument
    Set ccs = doc.SelectContentControlsByTag("wqgr")
    Set strDate = ccs.ContentControl.Range.Text
    Set PODate = CDate(strDate)
    Selection.GoTo What:=wdGoToBookmark, Name:="Date1"
    Selection.TypeText Text:=Format(PODate + 7, "mm/dd/yyyy")
End Sub

Compile error: Object required, highlighting the "strDate = " in the code.

Upvotes: 1

Views: 1326

Answers (1)

Rich Michaels
Rich Michaels

Reputation: 1713

Your routine has a number of issues. The variables doc and ccs are not declared. You are also trying to "Set" strDate but strDate is a string variable and "Set" statements are only used for object variables. The same is true for PODate, you do not use a Set command to load a Date.

Below is revised code for you to review:

Sub AddDates()
'
' AddDates Macro
    Dim PODate As Date
    Dim strDate As String
    Dim doc As Word.Document
    Dim ccs As Word.contentControl

    Set doc = ActiveDocument
    Set ccs = doc.SelectContentControlsByTag("wqgr").Item(1)
    strDate = ccs.Range.Text
    PODate = CDate(strDate)
    Selection.GoTo What:=wdGoToBookmark, Name:="Date1"
    Selection.TypeText Text:=Format(PODate + 7, "mm/dd/yyyy")
End Sub

Upvotes: 1

Related Questions