• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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
  DocPath = "SONGCHARTS:PDF:"
  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.
0
tonygardner1
Asked:
tonygardner1
  • 5
  • 4
1 Solution
 
Chris BottomleyCommented:
Personally I would suggest something like:

'At the top of a code module
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) _
    As Long


'Instead of your line  25   openorSave ActiveDocument, "c:\deleteme\fred.pdf", wdFormatPDF

'A new function

Function openorSave(doc As Document, FP As String, FF As Integer)
Dim fso As Object

    Set fso = CreateObject("scripting.filesystemobject")
    If fso.fileexists(FP) Then
        ShellExecute 0, "Open", FP, "", "", 0
    Else
        doc.SaveAs2 FileName:=FP, fileformat:=FF
        ShellExecute 0, "Open", FP, "", "", 0
    End If
    Set fso = Nothing

End Function

Open in new window


Chris
0
 
tonygardner1Author Commented:
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.

Cheers,
Tony G.
0
 
Chris BottomleyCommented:
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

Chris
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tonygardner1Author Commented:
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.
Workbook1.xlsm
0
 
Chris BottomleyCommented:
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?

Cheis
0
 
tonygardner1Author Commented:
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.

Cheers,
Tony G.
0
 
Chris BottomleyCommented:
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?

Chris
0
 
tonygardner1Author Commented:
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.

Best,
Tony
Workbook1.xlsm
0
 
tonygardner1Author Commented:
Per EE, I am closing this Question, and will open another with a more specific objective.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now