Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-02-2012, 06:25 AM
rmw85 rmw85 is offline Making a Command Button that Prints, Saves and Emails Windows XP Making a Command Button that Prints, Saves and Emails Office 2010 32bit
Novice
Making a Command Button that Prints, Saves and Emails
 
Join Date: Apr 2012
Posts: 10
rmw85 is on a distinguished road
Default Making a Command Button that Prints, Saves and Emails

I'm extremly new to Visual Basic, all i know is what i've been picking up online in the past few month. I'm working on a template in word that Prints the document, Saves it then emails it to just one email. I had the code to a point where it worked perfectly on my machine aslong as you didnt click No in any of the message boxes. When you put the document on the network drives the Command Button no longer works even when enabling Macros and such. So then when brainstorming it will probably be easier to put it on our webpage for people to access then the network drive as that can be confusing to some users. The problem i ran across there is that when you open from the website it created opens the document as Read-Only and the code no longer Saves or Emails as i wanted it to but does print fine. I found a code that i've tried that makes it into a temp document and saves but loses formatting and doesnt email as the document thats open stays the same.
If you can point me in the right direction or have suggestions please let me know. Been working on this for abit now and would like some input.

EDIT: I should also add maybe the important part. The reason i created the temp file was because the first line of the document is what i want it saved as and the Subject line of the email and when opened as read-only it just keeps that name and doesnt allow it to change. When the template was opened Word by default wants to save the document as whatever is in the first line of the document which is EXACTLY what i'm looking for.


Thanks.

Code:
Private Sub CommandButton1_Click()
    'Save, Print and email documents
    Dim noSession As Object, noDatabase As Object, noDocument As Object
    Dim obAttachment As Object, EmbedObject As Object
    Dim stSubject As Variant, stAttachment As String
    Dim vaRecipient As Variant
    
    'Print Document
    Shapes(1).Visible = msoFalse
    ActiveDocument.PrintOut Background:=False
    Shapes(1).Visible = msoTrue
    
    'Check if the active Document is saved or not
    Const EMBED_ATTACHMENT As Long = 1454
    Const stTitle As String = "Active Document status"
    Const stMsg As String = "The active Document must first be saved " & vbCrLf _
    & "before it can be sent as an attachment."
    
    'If the active Document has not been saved at all.
    If Len(ActiveDocument.Path) = 0 Then
        MsgBox stMsg, vbInformation, stTitle
    End If
    
    'Ask then create folder if not created
     Dim rspCreate
    If Dir("E:\Documents and Settings\" & Environ("username") & "\Desktop\EMS\", vbDirectory) = "" Then
       rspCreate = MsgBox("Directory doesn't exist, do you wish to create it?", vbYesNo)
      If rspCreate = vbYes Then
            MkDir "E:\Documents and Settings\" & Environ("username") & "\Desktop\EMS\"
        End If
    End If
    
    'If the changes in the active Document have been saved or not.
    If ActiveDocument.Saved = False Then
        ChangeFileOpenDirectory "E:\Documents and Settings\" & Environ("username") & "\Desktop\EMS\"
        ActiveDocument.Save
    End If
    
     'Email address where file is sent to.
    vaRecipient = "myemail@mycompany.com"
     'Get the message from the user.
    stAttachment = ActiveDocument.FullName
     'Instantiate the Lotus Notes COM's Objects.
    Set noSession = CreateObject("Notes.NotesSession")
    Set noDatabase = noSession.GETDATABASE("", "")
     'If Lotus Notes is not open then open the mail-part of it.
    If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
     'Create the e-mail and the attachment.
    Set noDocument = noDatabase.CreateDocument
    Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
    Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
    
     'Add values to the created e-mail main properties.
    With noDocument
        .Form = "Memo"
        .SendTo = vaRecipient
        .Subject = ActiveDocument.Name
        .Body = "Attached in this email is the EMS forum"
        .SaveMessageOnSend = True
    End With
    
     'Send the e-mail instantly.
    With noDocument
        .PostedDate = Now()
        .Send 0, vaRecipient
    End With
   
    'End message to users saying process was sucessful
    MsgBox "Document was emailed and printed sucessfully"
    'Release objects from the memory.
    Set EmbedObject = Nothing
    Set obAttachment = Nothing
    Set noDocument = Nothing
    Set noDatabase = Nothing
    Set noSession = Nothing

End Sub
The code i was using for the temp file is
Code:
Private Sub CommandButton1_Click()
    Dim oSection As Section
    Dim r As Range
    Dim TempDoc As Document
    Dim FirstPara As String
     
    For Each oSection In ActiveDocument.Sections
        Set r = oSection.Range
        r.End = r.End - 1
        Set TempDoc = Documents.Add
        With TempDoc
            .Range = r
            FirstPara = r.Paragraphs(1).Range.Text
            FirstPara = Left(FirstPara, Len(FirstPara) - 1)
            ChangeFileOpenDirectory "E:\Documents and Settings\" & Environ("username") & "\Desktop\EMS\"
            .SaveAs FileName:=FirstPara & ".docx"
            .Close
        End With
        Set r = Nothing
        Set TempDoc = Nothing
    Next
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a Command Button that Prints, Saves and Emails Command Button doesnt work on network rmw85 Word VBA 1 04-25-2012 01:02 PM
Making a Command Button that Prints, Saves and Emails VBA Print Command Prints Document Twice HorizonSC Word 2 11-15-2011 03:26 AM
Check Boxes and Command Buttons Micky P Word VBA 0 10-27-2011 01:06 AM
Making a Command Button that Prints, Saves and Emails Command Button cksm4 Word VBA 7 02-27-2011 08:47 PM
Making a Command Button that Prints, Saves and Emails Making a button to open an Access Dbase aubreylc Outlook 2 04-07-2010 12:53 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:14 PM.


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