Skip to main content
Known Participant
January 15, 2022
Question

SaveAs method not working with Excel hyperlink

  • January 15, 2022
  • 1 reply
  • 3128 views

Am hoping someone can help with a problem I've not been able to solve.  I have a PDF Form with a script that uses the SaveAs method.  If I open the PDF from File Explorer or from Adobe Acrobat DC, the SaveAs script works fine.  However, if the PDF is opened from Excel, using the VBA HyperLink statement (ActiveWorkbook.FollowHyperlink "c:\path\filename.pdf"), the SaveAs script won't work.  The hyperlink itself works reliably.  I've tried adding trusted folders in Adobe Acrobat DC (where the PDF resides, where the Excel workbook resides, and similar) but cannot get it to work.  Any suggestions?

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
January 16, 2022

Can you post your code? I'm guessing it uses a relative file-path and when you open the file via the hyperlink maybe it opens it as a temporary file, which might interfere with that.

Known Participant
January 16, 2022

Here's my code (below).  To clarify, I use this on various laptops so that staff can open an empty template, complete the form, and then have the script name the file and place it in the correct folder on their laptop.  I use persistent global variables to make it work.  However, if they open a previously entered file for edit purposes (whose file name this script previously assigned), the script needs to detect that and save it back with the same file name and location.  It's been working reliably, except (thus the reason for this post) when it's opened from an Excel workbook that lists the PDF's from the consolidated folder.  As I'm sure you can tell from the script, this all operates in a Windows environment, and which is why I convert the environment independent path to a Windows path, i.e., so staff can see and confirm paths.  Thanks for any help!

 

var WinGPath = global.NoteStorPath;
var CAPath = this.path;
var WinCPath = "";
var cDateFormat = "mmm d, yyyy";
var cDate01 = this.getField("Text2").value;
var oDate01 = util.scand(cDateFormat, cDate01);
var cDate01 = util.printd(cDateFormat, oDate01);
var GFName = this.getField("Text1").value.trim() + " (Note) " + cDate01 + ".pdf";
var aPathComps = CAPath.split("/");
var CFName = aPathComps[aPathComps.length-1];
if (aPathComps[1].length == 1)
{
WinCPath = aPathComps[1] + ":\\";
}
else
{
WinCPath = "\\" + "\\" + aPathComps[1] + "\\";
}
for (var i = 2; i < aPathComps.length-1; i++) {WinCPath = WinCPath + aPathComps[i] + "\\"; };

if ((this.getField("Text1").value == "") || (this.getField("Text2").value == ""))
{
app.alert("Both Client Name & Date are required--File not Saved!", 3);
}
else
{
if (WinGPath == "")
{
app.alert("Standard Folder missing--File not Saved!", 3);
}
else
{
if (CFName.toLowerCase() == GFName.toLowerCase())
{
try
{
this.saveAs(WinCPath + CFName);
app.alert("File Saved with original name/location." + "\r\n" + "(" + WinCPath + CFName + ")", 3);
}
catch (err)
{app.alert("Save cancelled by User!" + "\r\n" + "(Note, if not user cancelled, perform Menu-based Save As and then restart Acrobat.)", 3); }
}
else
{
var FExist = true;
try {app.openDoc(WinGPath + GFName); FExist = true; }
catch (err) {FExist = false;}
if (FExist)
{
app.alert("Dup file risk; file not saved; confirm file name & location; perform menu-based SaveAs if appropriate.", 3);
this.fileToClose = app.openDoc(WinGPath + GFName);
this.fileToClose.closeDoc(true);
}
else
{
try
{
this.saveAs(WinGPath + GFName);
app.alert("New file saved in standard location." + "\r\n" + "(" + WinGPath + GFName + ").", 3);
}
catch (err)
{app.alert("Save cancelled by User!" + "\r\n" + "(Note, if not user cancelled, perform Menu-based Save As and then restart Acrobat.)", 3); }
}
}
}
}

try67
Community Expert
Community Expert
January 16, 2022

I recommend you add an alert that shows the current file path, just to make sure it's what you expect it to be.