Blanchetdb
Board Regular
- Joined
- Jul 31, 2018
- Messages
- 164
- Office Version
- 365
- Platform
- Windows
I need to copy a date (the current date) from a cell in Excel to a document in Word. I am able to do so but the date appears in the following format 2023-01-18 and I need it to appear as January 18, 2023
the following does what I need it to do but once I protect the sheet (with cell A17 and B17 both unlocked) it no longer works .... I get a debug message with the line (.Range("B17").NumberFormat = "mmmm dd yyyy") highlighted in yellow.
Dim Cell As Range
With ActiveSheet
.Range ("A17").Copy .Range("B17")
.Range("B17").NumberFormat = "mmmm dd yyyy"
For Each Cell In Range("B17")
Cell.Value = Cell.Text
Next Cell
End With
If I unlock the sheet, the date appears within the Word document as needed using the following vba:
Dim wdApp As Word.Application
Dim theString As String
Dim theObject As Object
Set wdApp = New Word.Application
Set theObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
With wdApp
.Visible = True
.Activate
.Documents.Add "M:\HR\CFIA Transfer Inventory\Builder Templates\Indeterminate_Appointments_EN_Template.DOCX"
Sheet3.Activate
Dim Cell As Range
With ActiveSheet
.Range ("A17").Copy .Range("B17")
.Range("B17").NumberFormat = "mmmm dd yyyy"
For Each Cell In Range("B17")
Cell.Value = Cell.Text
Next Cell
End With
theString = Range("B17").Value 'cast to string
theObject.SetText theString
theObject.PutInClipboard 'copy from cell B2 to clipboard.
.Selection.Goto what:=-1, Name:="Date"
.Selection.Paste 'paste from the clipboard to the Word Doc.
Can someone please provide me with assistance in regarding the date moving over to the Word document in the proper format (January 18, 2023) while the Excel sheet is locked? ...
thank you.
the following does what I need it to do but once I protect the sheet (with cell A17 and B17 both unlocked) it no longer works .... I get a debug message with the line (.Range("B17").NumberFormat = "mmmm dd yyyy") highlighted in yellow.
Dim Cell As Range
With ActiveSheet
.Range ("A17").Copy .Range("B17")
.Range("B17").NumberFormat = "mmmm dd yyyy"
For Each Cell In Range("B17")
Cell.Value = Cell.Text
Next Cell
End With
If I unlock the sheet, the date appears within the Word document as needed using the following vba:
Dim wdApp As Word.Application
Dim theString As String
Dim theObject As Object
Set wdApp = New Word.Application
Set theObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
With wdApp
.Visible = True
.Activate
.Documents.Add "M:\HR\CFIA Transfer Inventory\Builder Templates\Indeterminate_Appointments_EN_Template.DOCX"
Sheet3.Activate
Dim Cell As Range
With ActiveSheet
.Range ("A17").Copy .Range("B17")
.Range("B17").NumberFormat = "mmmm dd yyyy"
For Each Cell In Range("B17")
Cell.Value = Cell.Text
Next Cell
End With
theString = Range("B17").Value 'cast to string
theObject.SetText theString
theObject.PutInClipboard 'copy from cell B2 to clipboard.
.Selection.Goto what:=-1, Name:="Date"
.Selection.Paste 'paste from the clipboard to the Word Doc.
Can someone please provide me with assistance in regarding the date moving over to the Word document in the proper format (January 18, 2023) while the Excel sheet is locked? ...
thank you.