Link to home
Start Free TrialLog in
Avatar of TelMaco
TelMaco

asked on

Save filename with timestamp

This needs to run as a schedualled task:

This code works while logged on:



Sub Original_Create_and_Send()

    On Error Resume Next
    Application.DisplayAlerts = False
   
    'opens Word template
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False 'hides MS word
    Set wrdDoc = wrdApp.Documents.Open("C:\Reports\Template.doc")

    'copy to Word
    Sheets("Final Report").Select
    Range("A1:G45").CopyPicture ' creates report as a bitmap
    wrdApp.Selection.Paste
    Application.CutCopyMode = False
   
    'Save word
    Dim sFileName As String, sPath As String
    sPath = "C:\Reports\Archive\" 'change path here if you want
    'adds a timestamp to the file name
    sFileName = "VoIP Status " & Format(Now(), "mm_dd_yyyy hh mm AMPM")
    wrdDoc.SaveAs (sPath & sFileName)

But it does NOT work while logged off

If I change this:
    sFileName = "VoIP Status " & Format(Now(), "mm_dd_yyyy hh mm AMPM")
To this:
    sFileName = "VoIP Status "

It DOES work while logged off

So why is the
& Format(Now(), "mm_dd_yyyy hh mm AMPM")
Part not working while logged off??

Thank you!!
ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TelMaco
TelMaco

ASKER

One note - It's a Word File, so  it's : & ".Doc"

Anyways,

Why does it not require the  "& ".Doc"" if I am logged on ?!?!?

In any case it works like a charm now.  Thank you thank you thank you!!

Stephen