troubleshooting Question

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

Avatar of tonygardner1
tonygardner1Flag for United States of America asked on
Visual Basic ClassicMicrosoft ExcelScripting LanguagesVB Script
9 Comments1 Solution554 ViewsLast Modified:
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

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.
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros