![]() |
|
|
|
#1
|
|||
|
|||
|
Help!
Ok, so i'm working on a large project and am trying to figure out if what I want to do is even possible... First step: Is it possible to run a macro in Word that takes the current date, whatever it is when the document is opened, and add 7 days to that date? The date format would be mmmm d, yyyy. I have two points within my 23 page document where i need this to happen and it works when I run the macro but it always adds the date to wherever my cursor is as well, I need to turn that off as well. Now, i found out it's possible to run a macro that prompts the user to click "ok" or whatever i want at the opening of the document but I can't figure out how to tie that to the document to run the macro that I found. Here are the macro's that I'm currently using: To open a message: Private Sub Document_Open() 'run.macro MsgBox "Run Macro?", vbOKOnly, "Message" End Sub To add the date: Sub DATEPLUS7() Selection.TypeText Text:=Format(Date + 7, "mmmm d, yyyy") Dim myStoryRange As Range For Each myStoryRange In ActiveDocument.StoryRanges With myStoryRange.Find .Text = "[DATEPLUS7]" .Replacement.Text = Format(Date + 7, "mmmm d, yyyy") .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next myStoryRange End Sub |
|
#2
|
|||
|
|||
|
Simply remove from Sub DATEPLUS7
Quote:
Quote:
|
|
#3
|
||||
|
||||
|
To do it properly, bookmark the first instance of your date, cross-reference that bookmark for each of the other instances, and use something like the following in the 'This Document' code module of the document or its template:
Code:
Private Sub Document_Open()
If MsgBox("Run Macro?", vbYesNo, "Message") <> vbYes Then Exit Sub
Application.ScreenUpdating = False
Dim BmkRng As Range: Const BmkNm As String = "MyDate"
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = Format(Date + 7, "mmmm d, yyyy")
.Bookmarks.Add BmkNm, BmkRng
End If
.Fields.Update
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
Thank you both for your responses! I have been very frustrated with getting this to work (having no experience with macros) so this has been truly helpful!
What I ended up writing to get this to work was the following: Sub AutoOpen() If MsgBox("Run Macro?", vbYesNo, "Message") = vbYes Then Call DATEPLUS7 End Sub Sub DATEPLUS7() Application.ScreenUpdating = False Dim BmkRng As Range: Const BmkNm As String = "MyDate" With ActiveDocument If .Bookmarks.Exists(BmkNm) Then Set BmkRng = .Bookmarks(BmkNm).Range BmkRng.Text = Format(Date + 7, "mmmm d, yyyy") .Bookmarks.Add BmkNm, BmkRng End If .Fields.Update End With Application.ScreenUpdating = True End Sub I'm sure I'll have more questions in the future
|
|
| Tags |
| current date, macro, vba |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
start a macro with command Line startup switches
|
Fesazu | Word | 3 | 02-08-2018 12:15 AM |
| Spell check macro within macro button field doesn't work in one document | samuelle | Word VBA | 0 | 07-20-2016 02:27 AM |
StartUP location
|
ptmuldoon | Word | 2 | 05-12-2015 12:19 PM |
| Save As Macro using first line of document as document name | redzan | Word VBA | 1 | 01-31-2015 09:24 PM |
Merge doc on startup
|
arkay | Mail Merge | 1 | 03-20-2012 11:50 PM |