#1
|
|||
|
|||
Macro to auto-save pdf using text on page + current date
I have done a bit of research on this topic, but can't seem to find exactly what I am looking for. I would like to create a macro - preferably a keyboard shortcut over a button - that will allow me to automatically create a pdf copy of the document using a specific naming convention. This is the naming convention:
Boilerplate - Lorem Ipsum - Date Where "Boilerplate" is the same text every time, regardless of the document; "Lorem Ipsum" is text found on the page, highlighted in red in the attachment; and "Date" is the current date in the "yyyy-mm-dd" format. I'd prefer the pdf to save wherever the document is, but if that is a hassle to code, saving to the desktop would be fine. Finally, I was just introduced to Word's field code display feature (thanks macropod!) and am wondering if it's possible to use that (or another feature) to create "linked text"; in other words, text that changes based on designated reference text. For example, in the attachment, I've highlighted two instances of "Lorem ipsum". I'm looking for a way to change the first instance that instantly changes the other instance in the table. I assume this can be accomplished using the bracketed ( {{}} ) field code display, but I'm not sure. This is important to the pdf macro, because it draws on this text to create the filename. Let me know if you have any questions; thanks in advance! |
#2
|
||||
|
||||
There are numerous ways of replicating text. See: https://gregmaxey.com/word_tip_pages...ting_data.html
In this case, a mapped content control might work best, as a macro can then reference that for the save name. The code for the saving isn't especially complicated, but we'd need to know what data replication method you've implemented before the code could be written.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Great - let's go with content controls. I can follow what is needed up until the VBA code listed in the "Advanced Custom Mapping" section. Do I need to go that route, or can I just follow the simpler method explained above it?
|
#4
|
||||
|
||||
Rather than using the VBA code listed in the "Advanced Custom Mapping" section, you might want to read a little further on and go to the 'Content Control Tools' page and download the addin there.
A simpler alternative would be to use a ContentControlOnExit macro in the 'ThisDocument' code module of either the document or its template, coded along the lines of: Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) If CCtrl.Title <> "Parent" Then Exit Sub With CCtrl If .ShowingPlaceholderText = False Then ActiveDocument.SelectContentControlsByTitle("Child")(1).Range.Text = .Range.Text Else ActiveDocument.SelectContentControlsByTitle("Child")(1).Range.Text = "XXXX" End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Sorry, I'm not sure I follow what you mean by "Parent" and "Child". But I was able to accomplish what I wanted using one of the predefined document types ("Company").
Is this enough information to construct the pdf macro, or do I need to implement the VBA code you provided first? |
#6
|
||||
|
||||
As indicated in my previous reply, those are content control titles that you can add via Developer|Controls>Properties, after selecting the appropriate content control.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
I opened the property list. Each has a "(Name)" and "ThisDocument" attribute. The list is rather long, and I'm unsure if I need to be using to them.
I would assume the next step would be to create a macro for saving a pdf using the filename convention discussed in my original post. If we know I'm using the built-in "Company" control type for the "Lorem Ipsum" text in the filename, is there a way to grab that for the macro? |
#8
|
||||
|
||||
Quote:
Quote:
That's not the issue. The issue is the content replication. In any event, it's unwise to repurpose a built-in content control the way you propose.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Sorry if I'm not understanding - I am completely new to content controls - but what exactly is it that you need me to do at this point? I have my "Lorem ipsum" text replicating throughout the document as needed now. That solves the second part of my two-part issue in the original post, I think.
Now I just need to know how to write a pdf-save macro that saves the filename according to some boilerplate text at the front, my "Lorem ipsum" text currently being used with a content control, and the computer's internal date. Am I missing something? Am I skipping a step? Apologies for all the questions. |
#10
|
||||
|
||||
So what are you typing the text that's to become part of the filename into, and how is the replication being done?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Sorry for the delayed response. See attached image, viewed from Design Mode. I added those content controls around the "Lorem ipsum" text by going to Insert > Quick Parts > Document Property > Company. When I edit any instance of "Lorem ipsum" within these content control brackets, it changes all other instances on the page, which is exactly what I needed.
|
#12
|
||||
|
||||
In that case, you could use a macro like:
Code:
Sub SaveAsPDF() Dim StrName As String With ActiveDocument StrName = "\Boilerplate - " & .BuiltInDocumentProperties("Company") & " - " & Format(Date, "YYYY-MM-DD") .SaveAs FileName:=.Path & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Works perfect! Thank you again for your help.
|
Tags |
pdf export |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Auto save macro | brent chadwick | Word VBA | 10 | 10-25-2017 08:26 AM |
How to save the current page in a new file with all the page settings (header, footer | Jamal NUMAN | Word | 6 | 03-15-2012 03:27 PM |
How to call current PC date and/or current PC year | KIM SOLIS | Excel | 2 | 11-04-2011 06:09 PM |
Word Macro: Save file as text with current file name | jabberwocky12 | Word VBA | 2 | 10-22-2010 12:23 PM |
Auto insert current month's name and current year | Styler001 | Word | 4 | 01-25-2010 06:40 PM |