Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-08-2016, 02:12 AM
nparsons75 nparsons75 is offline Adding a save as macro button to a template Windows 7 64bit Adding a save as macro button to a template Office 2013
Novice
Adding a save as macro button to a template
 
Join Date: Jan 2016
Posts: 4
nparsons75 is on a distinguished road
Default Adding a save as macro button to a template

Hi, really hope someone can help with this.

I have a macro button that sends a document via email. I would also like the macro to save the file in a particular location with some details in the filename.

For example,



path: C:\reports\templates

filename: to include, MMR, Author, date, time,

example: MMR A.N.Other 20160108 09:10.doc

My current macro code is below.


Code:
Private Sub CommandButton1_Click()
Dim OL              As Object
Dim EmailItem       As Object
Dim Doc             As Document
 
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument

With EmailItem
    .Subject = "Monthly Management Report"
    .Body = "Hi Brian," & vbCrLf & _
    "Please find attached our Monthly Management Report" & vbCrLf & _
    "Regards,"
    .To = "n.parsons@lur.co.uk"
    .Importance = olImportanceNormal
    .Attachments.Add Doc.FullName
    .Send
End With
 
Application.ScreenUpdating = True
 
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub

Private Sub CommandButton2_Click()
ActiveDocument.PrintOut Copies:=1
End Sub
Appreciate any help.
Reply With Quote
  #2  
Old 01-08-2016, 03:17 AM
gmayor's Avatar
gmayor gmayor is offline Adding a save as macro button to a template Windows 10 Adding a save as macro button to a template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is easy enough to include the option to save (though you can't have a colon in the filename). Frankly I would use ribbon buttons for this in the document template rather than ActiveX buttons in the document (Menu buttons for Word 2003 and earlier).

Code:
Private Sub CommandButton1_Click()
Dim OL As Object
Dim EmailItem As Object
Dim olInsp As Object
Dim wdDoc As Document
Dim oRng As Range
Dim Doc As Document
Dim strFname As String
Const strPath As String = "C:\reports\templates\"
    Application.ScreenUpdating = False
    On Error Resume Next

    Set OL = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        Set OL = CreateObject("Outlook.Application")
    End If
    Set EmailItem = OL.CreateItem(0)
    Set Doc = ActiveDocument
    strFname = strpath & "MMR "
    strFname = strFname & Doc.BuiltInDocumentProperties("Author")
    strFname = strFname & Chr(32) & Format(Date, "yyyyddmm")
    strFname = strFname & Chr(32) & Format(Time, "HHMM") & ".doc"
    Doc.SaveAs strFname
    With EmailItem
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range(0, 0)
        .Subject = "Monthly Management Report"
        oRng.Text = "Hi Brian," & vbCrLf & _
                "Please find attached our Monthly Management Report" & vbCrLf & _
                "Regards,"
        .to = "n.parsons@lur.co.uk"
        .Importance = 1
        .Attachments.Add Doc.FullName
        .Display
        .Send
    End With

    Application.ScreenUpdating = True

    Set Doc = Nothing
    Set OL = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
    Set EmailItem = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-08-2016, 03:18 AM
nparsons75 nparsons75 is offline Adding a save as macro button to a template Windows 7 64bit Adding a save as macro button to a template Office 2013
Novice
Adding a save as macro button to a template
 
Join Date: Jan 2016
Posts: 4
nparsons75 is on a distinguished road
Default

Thank you, you may be right. How would I do that, ribbon buttons?
Reply With Quote
  #4  
Old 01-08-2016, 04:00 AM
nparsons75 nparsons75 is offline Adding a save as macro button to a template Windows 7 64bit Adding a save as macro button to a template Office 2013
Novice
Adding a save as macro button to a template
 
Join Date: Jan 2016
Posts: 4
nparsons75 is on a distinguished road
Default

Hi the code returns this error: .Attachments.Add Doc.FullName when i debug.

Any ideas?

Thanks again
Reply With Quote
  #5  
Old 01-08-2016, 05:48 AM
gmayor's Avatar
gmayor gmayor is offline Adding a save as macro button to a template Windows 10 Adding a save as macro button to a template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Does the folder C:\reports\templates\ exist? You will get an error if it doesn't.

The attached template includes a ribbon, a modified version of the code to reflect that, and includes some basic error correction. It also works!

You can edit the ribbon with the CustomUI editor - see http://gregmaxey.com/word_tip_pages/...bbon_main.html
Attached Files
File Type: dotm This is an example template.dotm (107.8 KB, 13 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 01-08-2016, 07:00 AM
nparsons75 nparsons75 is offline Adding a save as macro button to a template Windows 7 64bit Adding a save as macro button to a template Office 2013
Novice
Adding a save as macro button to a template
 
Join Date: Jan 2016
Posts: 4
nparsons75 is on a distinguished road
Default

Now that is really good, never realised you could do that?

I will have a read through it. I need to work out how to edit the button as the save location needs to be changed.

Thank you so much.
Reply With Quote
  #7  
Old 01-08-2016, 07:47 AM
gmayor's Avatar
gmayor gmayor is offline Adding a save as macro button to a template Windows 10 Adding a save as macro button to a template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The save location is in the code in the templates ModMain and in any case the default from your earlier post is created if not present.
Code:
Const strPath As String = "C:\reports\templates\"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
macro, save



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a save as macro button to a template Adding a Save button into a document WyRm Word 12 03-23-2013 05:10 AM
Word ask to save template whenever i save a derived document jorbjo Word 3 10-04-2012 10:52 AM
Adding a save as macro button to a template Add a "SAVE" Button to a form template? Dave L Word 9 03-21-2012 07:04 PM
Adding a save as macro button to a template Adding email button to Word form leilers Word 5 01-09-2012 03:21 PM
Adding an Email Button to a Word Document maddoktor Word 0 12-01-2011 01:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:13 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