Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2014, 12:40 PM
DefinedDesign DefinedDesign is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 32bit Save As PDF Macro/VBA with Predetermined File Name Office 2013
Novice
Save As PDF Macro/VBA with Predetermined File Name
 
Join Date: Oct 2014
Posts: 5
DefinedDesign is on a distinguished road
Default Save As PDF Macro/VBA with Predetermined File Name

Hello

I am adding a control button to a form template in Word 2013 and I need to write a macro/vba that will save the form as a PDF with a predetermined name, along with the name from the formfield, and date.



I would basically like the predetermined part of the file name to read:

NURS_353_Clinical_Evaluation_

Then I want to add the student's name from the formfield in the document:

NURS_353_Clinical_Evaluation_Jane_Doe_

Then add the date the file is being saved to give a complete file name of:

NURS_353_Clinical_Evaluation_Jane_Doe_10.28.2014

I need the user of the form to be able to save this PDF file anywhere on their computer or network without losing the file name.

I found a couple Macro's on the web, and tried to use them but they don't meet my needs. I confess I'm a complete newbie to all this macro/vba stuff.

Thanks in advanced for your advice!
Reply With Quote
  #2  
Old 10-28-2014, 04:04 PM
macropod's Avatar
macropod macropod is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 64bit Save As PDF Macro/VBA with Predetermined File Name 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

You could use a macro like:
Code:
Sub Demo()
Dim StrPth As String
StrPth = "C:\Users\" & Environ("Username") & "\"
With ActiveDocument
  .SaveAs2 FileName:=StrPth & "\Documents\NURS_353_Clinical_Evaluation_" & _
    Replace(.FormFields("Client").Result, " ", "_") & "_" & _
    Format(Date, "MM.DD.YYYY") & ".PDF", _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
End With
End Sub
where the formfield's internal bookmark name is 'Client'. The above would save the PDF to the user's 'Documents' folder.

You mention the desire to be able to save the file "anywhere on their computer or network", a process that would either require the use of a folder browser or working through the SaveAs dialogue. The latter carries with it the risk that the user would change the filename. The following version implements a browser:
Code:
Sub Demo()
Dim StrPth As String
StrPth = GetFolder & "\"
If StrPth = "\" Then Exit Sub
With ActiveDocument
  .SaveAs2 FileName:=StrPth & "\Documents\NURS_353_Clinical_Evaluation_" & _
    Replace(.FormFields("Client").Result, " ", "_") & "_" & _
    Format(Date, "MM.DD.YYYY") & ".PDF", _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
End With
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-29-2014, 06:19 AM
DefinedDesign DefinedDesign is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 32bit Save As PDF Macro/VBA with Predetermined File Name Office 2013
Novice
Save As PDF Macro/VBA with Predetermined File Name
 
Join Date: Oct 2014
Posts: 5
DefinedDesign is on a distinguished road
Default

Thank you Paul

I am very grateful for your help! I inserted your code but I'm getting 'Compile Error: Expected End Sub'. The first line of text was highlighted yellow in the error.

Code:
Private Sub CommandButton1_Click()
Sub Demo()
Dim StrPth As String
StrPth = GetFolder & "\"
If StrPth = "\" Then Exit Sub
With ActiveDocument
  .SaveAs2 FileName:=StrPth & "\Documents\NURS_353_Clinical_Evaluation_" & _
    Replace(.FormFields("Client").Result, " ", "_") & "_" & _
    Format(Date, "MM.DD.YYYY") & ".PDF", _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
End With
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by macropod; 10-29-2014 at 01:23 PM. Reason: Replicated oversized screenshot with actual code
Reply With Quote
  #4  
Old 10-29-2014, 06:33 AM
Cosmo Cosmo is offline Save As PDF Macro/VBA with Predetermined File Name Windows Vista Save As PDF Macro/VBA with Predetermined File Name Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Remove the second line, 'Sub Demo()'.
Reply With Quote
  #5  
Old 10-29-2014, 06:35 AM
DefinedDesign DefinedDesign is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 32bit Save As PDF Macro/VBA with Predetermined File Name Office 2013
Novice
Save As PDF Macro/VBA with Predetermined File Name
 
Join Date: Oct 2014
Posts: 5
DefinedDesign is on a distinguished road
Default

Thanks Cosmo - I removed but I still get the same error?
Reply With Quote
  #6  
Old 10-29-2014, 06:47 AM
DefinedDesign DefinedDesign is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 32bit Save As PDF Macro/VBA with Predetermined File Name Office 2013
Novice
Save As PDF Macro/VBA with Predetermined File Name
 
Join Date: Oct 2014
Posts: 5
DefinedDesign is on a distinguished road
Default

Here is my document, not sure if this will help determine the error?
Attached Files
File Type: dotm Nursing 353 Clinical Evaluation of Behaviors.dotm (37.7 KB, 17 views)
Reply With Quote
  #7  
Old 10-29-2014, 07:22 AM
Cosmo Cosmo is offline Save As PDF Macro/VBA with Predetermined File Name Windows Vista Save As PDF Macro/VBA with Predetermined File Name Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

Quote:
Originally Posted by DefinedDesign View Post
Here is my document, not sure if this will help determine the error?
You didn't delete the line I specified in the file you provided. When it is deleted, the error goes away.
Reply With Quote
  #8  
Old 10-29-2014, 08:09 AM
fumei fumei is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 64bit Save As PDF Macro/VBA with Predetermined File Name Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Private Sub CommandButton1_Click()
Sub Demo
.......
..........
.....
End Sub


TWO starts - Sub
ONE termination - End Sub

Therefore "Expected End Sub"

As Cosmo mentions, remove one start (the Sub Demo).
Reply With Quote
  #9  
Old 10-29-2014, 08:12 AM
DefinedDesign DefinedDesign is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 32bit Save As PDF Macro/VBA with Predetermined File Name Office 2013
Novice
Save As PDF Macro/VBA with Predetermined File Name
 
Join Date: Oct 2014
Posts: 5
DefinedDesign is on a distinguished road
Default

Thanks to all of you. Now I am getting 'Run-time error '91': Object variable or With block variable not set'.
Code:
Private Sub CommandButton1_Click()
Dim StrPth As String
StrPth = GetFolder & "\"
If StrPth = "\" Then Exit Sub
With ActiveDocument
  .SaveAs2 FileName:=StrPth & "\Documents\NURS_353_Clinical_Evaluation_" & _
    Replace(.FormFields("Client").Result, " ", "_") & "_" & _
    Format(Date, "MM.DD.YYYY") & ".PDF", _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
End With
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by macropod; 10-29-2014 at 01:31 PM. Reason: Replicated oversized screenshot with actual code
Reply With Quote
  #10  
Old 10-29-2014, 01:48 PM
macropod's Avatar
macropod macropod is offline Save As PDF Macro/VBA with Predetermined File Name Windows 7 64bit Save As PDF Macro/VBA with Predetermined File Name 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

Three things, none of which has anything to do with your error, which I cannot replicate:

1. An error in my code. Change:
.SaveAs2 FileName:=StrPth & "\Documents\NURS_353_Clinical_Evaluation_" & _
to:
.SaveAs2 FileName:=StrPth & "NURS_353_Clinical_Evaluation_" & _

2. As previously advised, the code works on the premise that your document has a formfield whose internal bookmark name is 'Client'. Your document has no such formfield. Not only that, your student name is contained in a content control in a table cell where, once the document has forms protection applied, you can't even update it.

3. Content controls and formfields are not designed to work together in the same document. You should use one or the other. You currently have one content control for the semester, another for the student name, another for the year, yet another for the clinical date and, finally, another for the Time & date stamp. All except your 'clinical date' content control have formfield equivalents. With formfields, your 'clinical date' could be typed into a formfield using date validation.

Fixing the run-time error may require nothing more than exiting and re-starting Word.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Save As PDF Macro/VBA with Predetermined File Name Choose file name Save As macro gbaker Excel Programming 28 07-22-2014 03:54 PM
Save As PDF Macro/VBA with Predetermined File Name Save As Macro that changes the file name also rosscortb Word VBA 5 05-19-2014 08:40 AM
save a file with a predetermined date in VBA lingering Word VBA 1 03-23-2014 07:47 PM
Save As PDF Macro/VBA with Predetermined File Name Word Macro: Save file as text with current file name jabberwocky12 Word VBA 2 10-22-2010 12:23 PM
Macro will not save to normal.dot file when exiting bobbraun Word 1 09-28-2010 06:26 AM

Other Forums: Access Forums

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