Hello everyone,
I am new to excel VAB tyring to learn. i have tried to perform a small action of finding and replacing the data in Word 2013 using the excel VBA, and it runs successfully except for the date and number format.
The date i have in the Excel is December 12 2019, but when it is being copied and found and replaceed in Word it is pasted as date# (Number) 45636, and the number that i have in excel is 1080 and it is pasted as 1080.3333333.
I am not able to format the date and numbers as per the excel format, and at the end. I tried to save the word and convert it to PDF, but have no idea on how to write the code. I am using the code as below
Public Sub WordFindAndReplace()
Dim ws As Worksheet, msWord As Object
Set ws = ActiveSheet
Set msWord = CreateObject("Word.Application")
With msWord
.Visible = True
.Documents.Open "C:\Users\user\Desktop\Vishnu Project\Offer Letter original.doc"
.Activate
With .ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "FullName"
.Replacement.Text = ws.Range("A2").Value2
.Forward = True
.Wrap = 1 'wdFindContinue (WdFindWrap Enumeration)
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2 'wdReplaceAll (WdReplace Enumeration)
.Text = "Offer date" 'Unable to format this date field
.Replacement.Text = ws.Range("C2").Value2
.Forward = True
.Wrap = 1 'wdFindContinue (WdFindWrap Enumeration)
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2 'wdReplaceAll (WdReplace Enumeration)
.Text = "BasicM"
.Replacement.Text = ws.Range("J2").Value2
.Forward = True
.Wrap = 1 'wdFindContinue (WdFindWrap Enumeration)
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2 'wdReplaceAll (WdReplace Enumeration)
.Text = "BasicA" 'Unable to format the number without decimals
.Replacement.Text = ws.Range("K2").Value2
.Forward = True
.Wrap = 1 'wdFindContinue (WdFindWrap Enumeration)
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2 'wdReplaceAll (WdReplace Enumeration)
End With
.Quit SaveChanges:=True
End With
End Sub
Could some one please help me in formating the Date (as December 12, 2019) , Nunber (With no decimal Value) and converting the word to PDF.
Thanks in advacne for the help.
Kind Regards,
Vishnu T
Bookmarks