Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-18-2019, 12:25 PM
bluefate25 bluefate25 is offline Macro / VBA to run at document startup Windows 10 Macro / VBA to run at document startup Office 2010
Novice
Macro / VBA to run at document startup
 
Join Date: Sep 2019
Posts: 2
bluefate25 is on a distinguished road
Question Macro / VBA to run at document startup

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
Reply With Quote
  #2  
Old 09-22-2019, 04:27 AM
eduzs eduzs is offline Macro / VBA to run at document startup Windows 10 Macro / VBA to run at document startup Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Simply remove from Sub DATEPLUS7
Quote:
Selection.TypeText Text:=Format(Date + 7, "mmmm d, yyyy")
Quote:
Private Sub Document_Open()
If MsgBox("Run Macro?", vbYesNo, "Message") = vbYes Then Call DATEPLUS7
End Sub
Reply With Quote
  #3  
Old 09-22-2019, 04:57 AM
macropod's Avatar
macropod macropod is offline Macro / VBA to run at document startup Windows 7 64bit Macro / VBA to run at document startup Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 09-22-2019, 09:45 PM
bluefate25 bluefate25 is offline Macro / VBA to run at document startup Windows 10 Macro / VBA to run at document startup Office 2010
Novice
Macro / VBA to run at document startup
 
Join Date: Sep 2019
Posts: 2
bluefate25 is on a distinguished road
Default

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
Reply With Quote
Reply

Tags
current date, macro, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro / VBA to run at document startup 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
Macro / VBA to run at document startup 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
Macro / VBA to run at document startup Merge doc on startup arkay Mail Merge 1 03-20-2012 11:50 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:21 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft