vba - Word (2010) Update date and keep formatting using Macro -
i have following vba code insert date in format of code @ bookmark locations. inserting date each time open file without deleting old boookmark text added delete text @ top of code deletes format , inserts text there way keep formatting?
sub autoopen() ' ' autoopen macro ' ' activedocument.bookmarks("mydate").range.delete activedocument.bookmarks("mydate1").range.delete activedocument.bookmarks("mydate2").range.delete activedocument.bookmarks("mydate3").range.delete activedocument.bookmarks("mydate4").range.delete activedocument.bookmarks("mydate5").range.delete activedocument.bookmarks("mydate6").range.delete activedocument.bookmarks("mydate").range .insertbefore format(date + 1, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate1").range .insertbefore format(date + 2, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate2").range .insertbefore format(date + 3, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate3").range .insertbefore format(date + 4, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate4").range .insertbefore format(date + 5, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate5").range .insertbefore format(date + 6, "dddd dd mmmm yyyy") end activedocument.bookmarks("mydate6").range .insertbefore format(date + 7, "dddd dd mmmm yyyy") end end sub
when 'formatting' mean date formatting, or bold/italic, etc?
in case, code doesn't run me; when delete range bookmark goes it. following runs me, , correctly formats dates each time run it:
dim temprng range set temprng = activedocument.bookmarks("mydate").range temprng.text = format(date + 1, "dddd dd mmmm yyyy") activedocument.bookmarks.add "mydate", temprng set temprng = activedocument.bookmarks("mydate1").range temprng.text = format(date + 2, "dddd dd mmmm yyyy") activedocument.bookmarks.add "mydate1", temprng set temprng = activedocument.bookmarks("mydate2").range temprng.text = format(date + 3, "dddd dd mmmm yyyy") activedocument.bookmarks.add "mydate2", temprng
you can continue rest of bookmarks or throw whole thing in loop.
Comments
Post a Comment