Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 04-27-2018, 03:45 AM
gmayor's Avatar
gmayor gmayor is offline Word document with VBA and Form Fields reset when select File,Print Windows 10 Word document with VBA and Form Fields reset when select File,Print Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code is simple enough - annotated below:

Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim oTable As Table
Dim EndDate As Date
Dim oCell As Range
Dim i As Long, j As Long
    If ContentControl.Tag = "Date" Then 'Process only the date content control
        If ContentControl.ShowingPlaceholderText = False Then 'Only process if it is not showing the placeholder text
            'Get the end date from the content control
            EndDate = CDate(ContentControl.Range.Text)
            'If it is not a Friday, get the next Friday
            EndDate = EndDate - Weekday(EndDate) + 6
            'Define which table to process (here table 2)
            Set oTable = ThisDocument.Tables(2)
            'Process the cells in the first row
            With oTable.Rows(1)
                'From cell 4 to cell 17
                For i = 4 To 17
                    'adjust the count to allow for the unwanted cells
                    j = 13 - i + 4
                    'Set a range to each cell in turn
                    Set oCell = .Cells(i).Range
                    'omit the end of cell character from the range
                    oCell.End = oCell.End - 1
                    'write the date in the cell
                    oCell.Text = Format(DateAdd("d", -j, EndDate), "dd/mm/yyyy")
                Next i
            End With
            'Process the cells in the third row
            With oTable.Rows(3)
                For i = 4 To 17
                    'eveything as row 1
                    j = 13 - i + 4
                    Set oCell = .Cells(i).Range
                    oCell.End = oCell.End - 1
                    'except the value is the first three letters of the day of the week
                    oCell.Text = Format(DateAdd("d", -j, EndDate), "ddd")
                Next i
            End With
        End If
    End If
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 04-27-2018 at 11:28 PM.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't reset form fields (Word 2013) and conditional formatting rogelinepaula Word VBA 24 01-07-2016 03:54 PM
Extract form fields to Word Document RonNCmale Word VBA 22 01-11-2014 05:06 AM
Word document with VBA and Form Fields reset when select File,Print Recovering a word file (Select the encoding that makes your document readable) Canni Word 2 08-29-2012 02:46 PM
Word document with VBA and Form Fields reset when select File,Print Preventing Form Fields to Reset PosseJohn Word VBA 4 07-15-2011 09:44 PM
Word document with VBA and Form Fields reset when select File,Print Word Macro That Checks a Check Box Form Field When File Print is Executed DKerne Word VBA 4 06-09-2011 11:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:03 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft