C
Camper
Aloha,
I want to enter a date as m/d/yyyy format in a template file in Word. The
original date was entered as a text string dd mmmm yyyyy in a Word file, not
in Field Code or Table. From the Discussion Groups I was able to assemble the
following code (below). To summarize, the macro starts in Word where date is
searched for and copied; pasted in Excel, formatted and copied; and then
pasted in the Word template. The Dim statements defined, and the Reference
was set to Excel prior to running the macro. But an error occurs at the
"ActiveCell.Paste" line in Excel: Error 91: Objective variable or With block
variable not set.
Any help will be much appreciated. If someone knows of a easier way, I am
all ears. Please let me know if you need additional information.
--
Mahalo for your help,
Camper
____________________
Sub FormatDate()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String
' Set workbook.
WorkbookToWorkOn = "C:\Documents and Settings\CampR\My
Documents\dateconversion.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set xlApp = New Excel.Application
End If
On Error GoTo Err_Handler
Set xlBook = xlApp.Workbooks.Open(FileName:=WorkbookToWorkOn)
' Find and copy date record in document 1.
Windows("DATA SET SUMMARY HA001KAPA1993SEP1").Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Start Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.Copy
' Paste and format date in Excel.
xlBook.Sheets(1).Range("A1").Select
ActiveCell.Paste
xlSelection.NumberFormat = "m/d/yyyy"
Selection.Copy
' Paste formatted date into template.
Windows("SMMS- template2").Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Beginning Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.PasteExcelTable False, True, False
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Beginning Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.PasteExcelTable False, True, False
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory
If ExcelWasNotRunning Then
xlApp.Quit
End If
' Release object reference.
Windows("dateconversion.xls").Activate
Set xlRng = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description,
vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
xlApp.Quit
End If
End Sub
I want to enter a date as m/d/yyyy format in a template file in Word. The
original date was entered as a text string dd mmmm yyyyy in a Word file, not
in Field Code or Table. From the Discussion Groups I was able to assemble the
following code (below). To summarize, the macro starts in Word where date is
searched for and copied; pasted in Excel, formatted and copied; and then
pasted in the Word template. The Dim statements defined, and the Reference
was set to Excel prior to running the macro. But an error occurs at the
"ActiveCell.Paste" line in Excel: Error 91: Objective variable or With block
variable not set.
Any help will be much appreciated. If someone knows of a easier way, I am
all ears. Please let me know if you need additional information.
--
Mahalo for your help,
Camper
____________________
Sub FormatDate()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String
' Set workbook.
WorkbookToWorkOn = "C:\Documents and Settings\CampR\My
Documents\dateconversion.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set xlApp = New Excel.Application
End If
On Error GoTo Err_Handler
Set xlBook = xlApp.Workbooks.Open(FileName:=WorkbookToWorkOn)
' Find and copy date record in document 1.
Windows("DATA SET SUMMARY HA001KAPA1993SEP1").Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Start Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.Copy
' Paste and format date in Excel.
xlBook.Sheets(1).Range("A1").Select
ActiveCell.Paste
xlSelection.NumberFormat = "m/d/yyyy"
Selection.Copy
' Paste formatted date into template.
Windows("SMMS- template2").Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Beginning Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.PasteExcelTable False, True, False
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Beginning Date:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.PasteExcelTable False, True, False
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory
If ExcelWasNotRunning Then
xlApp.Quit
End If
' Release object reference.
Windows("dateconversion.xls").Activate
Set xlRng = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub
Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description,
vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
xlApp.Quit
End If
End Sub