Link to home
Start Free TrialLog in
Avatar of tonygardner1
tonygardner1Flag for United States of America

asked on

Excel Macro for Creating a PDF "on the fly" from a Word Doc

Hello Experts.

I would like to start off 2013 with an interesting idea for a macro. Let me explain...

I have an Excel spreadsheet that contains hundreds of song titles and their keys. Another column has been setup with a hyperlink formula that opens PATH:Song Title:"*":Key:".pdf". This way, if PDFs have been created in more than one key, you would just change the key, and be able to open the PDF for the song in that key.

To help with the process of creating the PDFs with the "*KEY" suffix, I was able to create the following VB script:
Sub SaveAsPDF()
  If ActiveDocument.MailMerge.State = wdNormalDocument Then
    MsgBox "Not a Mail Merge Document!"
    Exit Sub
  End If
  CurrentKey = ActiveDocument.MailMerge.DataSource.DataFields("Root").Value
  If CurrentKey = "" Then
    MsgBox "Song Key Not Defined!"
    Exit Sub
  End If
  If Len(ActiveDocument.Path) = 0 Then
    MsgBox "File has no name!"
    Exit Sub
  End If
  DocType = Right(ActiveDocument.Name, 4)
  Select Case DocType
    Case "docx"
      ExtSize = 5
    Case ".doc"
      ExtSize = 4
  End Select
  DocName = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - ExtSize)
  NewDocPath = DocPath & DocName & " *" & CurrentKey & ".pdf"
  ActiveDocument.SaveAs fileName:=NewDocPath, FileFormat:=wdFormatPDF
End Sub

Open in new window

So I was wondering if it would be possible to take this baby to the next level, and have an expanded version of the above macro be created in Excel so when the user clicks the hyperlink, it would do one of two things: 1) if the PDF already exists, open it; OR 2) if the PDF doesn't exist, create it, then open it.

Well, there it is. Obviously, if you need further details or clarification, don't hesitate to ask!

Best Wishes to Everyone for a Happy and Prosperous 2013!

Tony G.
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tonygardner1


Thank you Chris. Unfortunately, I don't think I'm "connecting the dots" just yet, especially because I've never ventured into the Excel side of the VB scripting world.

If possible, could you provide more detailed instructions on how to call the script when clicking on a cell? I'm also a little unclear as to the role my existing script would play.

Sorry for being so dense, but I am encouraged as I now feel that there is a good chance this can actually be done.

Tony G.
The redsponse was how to adapt the script you supplied to make the requested change ... therefore it is called exactly as before.

The only application specifics are as commented in thta the shellexecute declaration has to be at the top of the module although I am using 2010 so notice now an error:

Line 24 (in 'my' listing) should be:

doc.SaveAs FileName:=FP, fileformat:=FF

My apologies, Chris. I see where we've had a disconnect in our communication. I failed to mention that the VBscript I posted above was created to run from MS Word while the document was already open.

What I was hoping to learn was how to have our function run from Excel when the user clicks on a cell in Column C (perhaps a button or other trigger). The called Subroutine would then need to identify the Song Title in Column A of that row, Song Key in Column B of that row, then either open the corresponding PDF, or if it doesn't exist, open the document in Word, create the PDF, close Word, then open the PDF. See the attached Excel doc for a "boiled down" view.

As I type this, I realize that it could be a significant programming task. If so, I will certainly understand and seek out a developer to take on that task with appropriate compensation.

Kind Regards,

Tony G.
Whilst that changes my understanding ...

What is needed is to try and build the requirement.  For example to run from excel there isnoactivedocuent so we need to create the word instance and go froma new instance therefore how can the word document be found?

As always, good thinking Chris. For starters, all the Word docs for the songs are always named after the song title, and kept in a single directory on a fixed volume. Therefore, I use the =HYPERLINK() function to create an "intelligent" link to open the Word doc based on the data in the other columns. For example, assuming that the Song Title is in cell $A$2, and the Song Key is in cell $B$2, I have the following formula in cell $D$2 to open the Word doc:
=HYPERLINK("SONGCHARTS:WordDocs:"&$A2&".DOCX","Click Me")

Likewise, I have a similar formula which opens the PDF in cell $C$2 (assuming it's already present):
=HYPERLINK("SONGCHARTS:PDF:"&$A2&" *"&$B$2&".PDF","Click Me")

I hope that helps. If not, just let me know and I'll try to clarify further.

Tony G.
Okay but a bit more ....

In the sample file there is no column d etc just the button in c so how is the folder path added to the column a and b values?

Of course. Do keep in mind that I'm working on a Mac, so "SONGCHARTS:PDF:" actually refers to a volume and directory. On Windows, it would be something like "S:\PDF\" (where the S: drive is assigned to the external drive containing all the documents).

I referenced Column D earlier to provide a straight-forward illustration as to how the Word doc could be retrieved from within Excel. To help clarify, I have attached an updated version, and will continue to do so for all future posts.

Per EE, I am closing this Question, and will open another with a more specific objective.