Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2014, 10:22 AM
madcar86 madcar86 is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2013
Novice
Save Filename based on Text Field
 
Join Date: Sep 2014
Posts: 4
madcar86 is on a distinguished road
Default Save Filename based on Text Field

I have a form template that I have put a command button on to submit via email. I would also like to add to the button to save as a pdf, and to save the filename based on one of the text content controls. Is this possible?
Reply With Quote
  #2  
Old 09-30-2014, 08:32 PM
macropod's Avatar
macropod macropod is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Yes, and it's quite simple, using code like:
Code:
With ActiveDocument
  .SaveAs2 FileName:="C:\Users\" & Environ("Username") & "\Documents\" & _
    .SelectContentControlsByTitle("MyControl")(1).Range.Text & ".PDF", _
    FileFormat:=wdFormatPDF
End With
where 'MyControl' is your content control's title.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-01-2014, 06:51 AM
madcar86 madcar86 is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2013
Novice
Save Filename based on Text Field
 
Join Date: Sep 2014
Posts: 4
madcar86 is on a distinguished road
Default

Thanks, it worked perfectly.

What would be the code to email the pdf to a specific address?

Also is there a way to have a message pop up and say that your email has been sent?

Sorry for all the questions, I am new to VBA.
Reply With Quote
  #4  
Old 10-01-2014, 11:07 PM
gmayor's Avatar
gmayor gmayor is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

To send the PDF by e-mail using the following code assumes the sender has Outlook.
I have left the code displaying the message requiring the user to send it.
You can add the command to send the message if you wish, after .Display (i.e. .Send) but retain .Display:

Code:
Sub Send_PDF_As_Attachment()
Dim oOutlookApp As Object
Dim oItem As Object
Dim olInsp As Object
Dim strFname As String
Dim wdDoc As Document
Dim oRng As Range

    With ActiveDocument
        strFname = .SaveAs2(Filename:="C:\Users\" & Environ("Username") & "\Documents\" & _
         .SelectContentControlsByTitle("MyControl")(1).Range.Text & ".PDF", _
         FileFormat:=wdFormatPDF)
    End With

    On Error Resume Next
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")

    'Outlook wasn't running, start it from code
    If Err <> 0 Then
        Set oOutlookApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(0)

    With oItem
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        oRng.Collapse 1
        oRng.Text = "This is the message body"
        .to = "someone@somewhere.com"
        .Subject = "This is the subject"
        .BodyFormat = 2
        .Attachments.Add strFname
        .Display
    End With

    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
lbl_Exit:
    Exit Sub
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
  #5  
Old 10-02-2014, 09:27 AM
madcar86 madcar86 is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2013
Novice
Save Filename based on Text Field
 
Join Date: Sep 2014
Posts: 4
madcar86 is on a distinguished road
Default

Thanks for the quick responds. I put the code in into VBA, but when I press the button nothing happens. If i put the "Private Sub CommandButton1_Click()" on, I get a compiling error: expected end sub. What am I missing?

I really appreciate your help.

Code:
Private Sub CommandButton1_Click()
Sub Send_PDF_As_Attachment()
Dim oOutlookApp As Object
Dim oItem As Object
Dim olInsp As Object
Dim strFname As String
Dim wdDoc As Document
Dim oRng As Range

    With ActiveDocument
        strFname = .SaveAs2(FileName:="C:\Users\" & Environ("Username") & "\Documents\" & _
         .SelectContentControlsByTitle("MyControl")(1).Range.Text & ".PDF", _
         FileFormat:=wdFormatPDF)
    End With

    On Error Resume Next
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")

    'Outlook wasn't running, start it from code
    If Err <> 0 Then
        Set oOutlookApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(0)

    With oItem
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        oRng.Collapse 1
        oRng.Text = "This is the message body"
        .to = "someone@somewhere.com"
        .Subject = "This is the subject"
        .BodyFormat = 2
        .Attachments.Add strFname
        .Display
    End With

    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
lbl_Exit:
    Exit Sub
End Sub
End Sub
Reply With Quote
  #6  
Old 10-02-2014, 09:45 PM
gmayor's Avatar
gmayor gmayor is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

You have embedded a sub within a sub

Private Sub CommandButton1_Click()
Sub Send_PDF_As_Attachment()

and similarly at the end you have

End Sub
End Sub

Remove one of the End Subs

and the line

Sub Send_PDF_As_Attachment()

and provided the code is associated with the button you should be getting somewhere.
__________________
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
  #7  
Old 10-03-2014, 07:23 AM
madcar86 madcar86 is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2013
Novice
Save Filename based on Text Field
 
Join Date: Sep 2014
Posts: 4
madcar86 is on a distinguished road
Default

I now get a error expected function or variable and it highlights saveas2. Could it be that it is an Active X button? I know this is getting annoying for you I appreciate the help.

Code:
Private Sub CommandButton1_Click()
    Dim oOutlookApp As Object
    Dim oItem As Object
    Dim olInsp As Object
    Dim strFname As String
    Dim wdDoc As Document
    Dim oRng As Range

        With ActiveDocument
            strFname = .SaveAs2(FileName:="C:\Users\" & Environ("Username") & "\Documents\" & _
             .SelectContentControlsByTitle("Title")(1).Range.Text & ".PDF", _
             FileFormat:=wdFormatPDF)
        End With
    
        On Error Resume Next
        'Get Outlook if it's running
        Set oOutlookApp = GetObject(, "Outlook.Application")
    
        'Outlook wasn't running, start it from code
        If Err <> 0 Then
            Set oOutlookApp = CreateObject("Outlook.Application")
        End If
        On Error GoTo 0
        'Create a new mailitem
        Set oItem = oOutlookApp.CreateItem(0)
    
        With oItem
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.Collapse 1
            oRng.Text = "This is the message body"
            .to = "someone@somewhere.com"
            .Subject = "This is the subject"
            .BodyFormat = 2
            .Attachments.Add strFname
            .Display
        End With
    
        'Clean up
        Set oItem = Nothing
        Set oOutlookApp = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
lbl_Exit:
        Exit Sub
End Sub
Reply With Quote
  #8  
Old 10-18-2014, 10:29 PM
macropod's Avatar
macropod macropod is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

What does the error message say?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 10-19-2014, 12:53 AM
gmayor's Avatar
gmayor gmayor is offline Save Filename based on Text Field Windows 7 64bit Save Filename based on Text Field Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 message says Compile Error - expected Function or Variable and highlights SaveAs2.

SaveAs2 incidentally is not available in Word 2007, but the OP says 2013 and in that version the following change doesn't produce the error.

Code:
With ActiveDocument
        strfname = "C:\Users\" & Environ("Username") & "\Documents\" & .SelectContentControlsByTitle("Title")(1).Range.Text & ".PDF"
        .SaveAs2 Filename:=strfname, FileFormat:=wdFormatPDF
End With
__________________
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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Save Filename based on Text Field How can I save a Word Document as a PDF file with a merged field filename? kp2009 Word VBA 5 08-27-2015 11:45 PM
Save Filename based on Text Field Auto update Filename field Oliver Beirne Word VBA 4 10-19-2012 03:33 AM
Macro to populate a text form field based on dropdown selection koloa Word 0 10-20-2011 11:52 AM
Save Filename based on Text Field Save Filename using Document Text Knawl Word 11 10-10-2011 03:00 AM
Save Filename based on Text Field Selecting merge field based on whether or not text is present amym Mail Merge 1 12-07-2010 05:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:37 AM.


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