Photoshop script - suppressWarnings, disable error dialogs - scripting

I have a script.jsx for photoshop, that exports some stuff from many PSD files. Sometimes (1 in 20 files), upon opening the psd file, the following dialog shows up:
var fileToOpen = new File(...);
open(fileToOpen);
I'm running this script on hundreds of files, and I need it to somehow ignore those dialogs. "Keep layers" would be ok, but in general, anything that will prevent the dialog will help.
I've found in a manual the option suppressWarnings, but it is only available to PdfOpenOptions or PhotoshopSaveOptions - there is no such thing as PsdOpenOptions or PhotoshopLoadOptions, neither does simple object {suppressWarnings: true} work. I have even tried adding displayDialogs = DialogModes.NO but that doesn't help either.
Is there a way to prevent this dialog? (ie. stop it from blocking the script's execution)

try try catch construction. If this does not help, then set the user interaction (though this may be cumbersome):
https://forums.adobe.com/thread/289239?tstart=0
Example:
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
var fileToOpen = new File(...);
open(fileToOpen);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

Related

How to disable autocomplete in vba?

I'm creating the program for exporting several excel sheets to pdf with watermarks and form fields etc. Everything works smooth right now but the final pdf is quite large. So I was thinking about the best way to make it smaller and I found out that the best result is by simply opening the pdf in Adobe Acrobat and then print it with "Adobe PDF" printer. This way I reduce the file size to 1/6 of the original size.
So I'm trying to do this via the VBA code and it looks like it's prety straight forward code using the JS.
sPath = "some path"
sPathFinal = "some other path"
Dim AcroApp As AcroAVDoc: Set AcroApp = CreateObject("AcroExch.AVDoc")
Dim Document As AcroPDDoc
Dim JSO, pp
AcroApp.Open sPath, ""
Set Document = AcroApp.GetPDDoc()
Set JSO = Document.GetJSObject
Set pp = JSO.getPrintParams
pp.printerName = "Adobe PDF"
pp.Filename = sPathFinal
JSO.Print (pp)
The problem is in the very last line as it should be
JSO.print(pp) - "print" with lowercase "p"
But everytime I step away from the lane, it gets autocorrected to uppercase "P". I tried to turn everything off in Tools -> Options -> Editor -> Code settings as well as on other places in options tab but had no luck so far.
Is there a way to prevent this autocorrect?
(Also I'm not native english speaking so there is quite big chance that it is called differently :)
Short answare is no you can't, because VBA editor auto-correct the case in your code.
This is because VB is case-sensitive (despite it doesn't look like it is), and the editor tries to prevent typo by changing the case of your variables.
If you want to preserve the case and avoid auto-correct, the simplest solution is to use another editor (like Notepad) and compile your code from the command-line.
Hope this help.

PowerPoint 2013 macro keeps file locked open after close command

I have a PowerPoint VBA function that opens presentations, copies slides into the active presentation, then closes the source presentation. It worked fine in 2010, but fails in 2013 (all on Windows 7) if it tries to open the same presentation more than once. It appears to me that after the presentation.close command is issued, the window is closed, but the file remains locked open until the VBA code exits. So if the code attempts to open that file again it returns the error:
"Method 'Open' of object 'Presentations' failed"
Here's a simplified form of the function I'm running that behaves the same way. I've had a colleague test this again in PowerPoint 2010 and it runs fine. I've also had a colleague test it under his 2013 to make sure it's not something with my particular installation.
Sub testopen()
Dim ppFile As Presentation
Dim i As Integer
Const fpath = "C:\test.pptx"
For i = 1 To 2
Set ppFile = Application.Presentations.Open(fpath)
ppFile.Close
Set ppFile = Nothing
Next i
End Sub
The file test.pptx is just a blank presentation. In debug mode I can see the file opens and closes on the first loop, then on the second loop the open command fails and I can see in Windows explorer that the hidden temporary file still exists, indicating the file is still open, until I exit the VBA code. I also verified that the file is held open by adding in a function to check the file open status.
I've spent probably an hour googling this and cannot find any other descriptions of this problem. I'm sure I can implement a workaround but it's driving me crazy that I can't find any other reports of seemingly such a simple issue. Any suggestions are greatly appreciated! Thanks.
The Best way that I have achieved this is to simply create a VBS file and in the VBS file I call out the desired VBA code. It's little more hassle than to write the VBA code, but it's the solution that worked for me.
For example in the VBS file:
Dim args, objPP
Set args = WScript.Arguments
Set objPP = CreateObject("Powerpoint.Application")
objPP.Open "C:\path\to\file.ppx"
objPP.Visible = True
objPP.Run "The_Macro"
objPP.Save
objPP.Close(0)
objPP.Quit
Or better yet, have the entire code within the VBS file and have it copy the desired slides.
Hope this helps you achieve your result.
Setting the file as Read Only resolved the issue. The open command is now:
Set ppFile = Application.Presentations.Open(fpath, msoTrue)
Also, saving the file before closing it resolved the issue. For that, add:
ppFile.Save
Interestingly, I had already tried setting the Saved property to True (ppFile.Saved = msoTrue), which does NOT work. Thanks to Michael for his suggestion on the VBS script. That does work and I had never run an external VBS script so I learned something new. In this case, I'd prefer to stick with a VBA solution.

Is there a way to close an open PDF file in VB.net programatically

In my VB.net application I am opening a PDF file using
System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf").
Is it possible to Close this file Programatically in some event.
Yes, there is one way, though it is not a very elegant solution.
When you start the PDF process, you capture the process-id in some global variable:
Dim id As Integer 'Global variable
id = System.Diagnostics.Process.Start("C:\Temp\myfile.pdf").Id
Then, when you need to kill the process, just do:
System.Diagnostics.Process.GetProcessById(id).Kill()
(make sure that there is a process with this id that is actually running!)
You may also use the Process.HasExited property to see if the PDF has been closed, and may process your code based on that.
I don't think that it's possible to close a specific PDF file because they are not an independent process, they are sub processes in the task manager.
you can kill the adobe acrobat reader process it self.
Dim AcrobateInstance() As Process = Process.GetProcessesByName("AcroRd32")
If AcrobateInstance.Length > 0 Then
For value As Integer = 0 To AcrobateInstance.Length - 1
BillInstance(value).Kill()
Next
End If
This is in C# but may come in handy...
var myPDFEvent = System.Diagnostics.Process.Start(#"C:\Temp\myfile.pdf");
myPDFEvent.Exited += new EventHandler(myPDFEvent_Exited);
myPDFEvent.EnableRaisingEvents = true;
void myPDFEvent_Exited(object sender, EventArgs e)
{
System.IO.File.Delete(#"C:\Temp\myfile.pdf);
}
This might work:
Process1.Start()
Process1.WaitForExit()
If Process1.HasExited Then
System.IO.File.Delete(Your File Path)
End If
Make sure to add the Process Object onto the form from the toolbox and configure the startinfo section.
If you are having permission problems. Use the AppData folder. It has the necessary permissions that programs need to run

Opening a file using impersonation

I have been searching the web looking for a way to open a WORD file from a secure network folder by impersonating a user who has access. The closest I've come to finding the answer was this from 2 years ago:
Impersonating in .net (C#) & opening a file via Process.start
Here is the code that I am using. When I set the arguments = LocalFile_Test, everything works perfectly because the user is accessing the local c:\ that is has access to. But when I set arguments = RemoteFile_Test, Word opens up a blank document which is the same effect as if I put garbage in the arguments. So it appears that it cannot find the file even though when I login with the user/domain/password that I specify in the properties below, I can find that exact file name and it is not empty. Does anything jump out at you right away? I appreciate your time.
Dim LocalFile_Test As String = "C:\New.docx"
Dim RemoteFile_Test As String = "\\Server1\Apps\File\New.docx"
Dim MyStartInfo As New System.Diagnostics.ProcessStartInfo
MyStartInfo.FileName = "C:\Program Files\Microsoft Office\Office12\WINWORD.exe "
MyStartInfo.Arguments = LocalFile_Test
MyStartInfo.LoadUserProfile = True
MyStartInfo.UseShellExecute = False
MyStartInfo.UserName = "specialuser"
MyStartInfo.Domain = "mydomainname"
MyStartInfo.Password = New System.Security.SecureString()
MyStartInfo.Password.AppendChar("p"c)
MyStartInfo.Password.AppendChar("a"c)
MyStartInfo.Password.AppendChar("s"c)
MyStartInfo.Password.AppendChar("s"c)
Process.Start(MyStartInfo)
My understanding is that you are trying to get a password protected file from a server, and when you do process start, it just opens up a blank word doc. I think the error is how you are trying to get the file, I think you have to map the actual physical path of the file on the server, like
System.Web.HttpContext.Current.Server.MapPath("\\Server1\Apps\File\New.docx")
From there, I am fairly certain, you need to create network credentials for the user like
System.Net.NetworkCredential=New NetworkCredential(userName:=, password:=)
Finally, once that is done, you can either write the file, or transmit the file like so...
System.Web.HttpContext.Current.Response.TransmitFile(file name)
System.Web.HttpContext.Current.Response.WriteFile(file name)
Then,once you get the file, you can try to open it with process start.
Hope that helps, let me know if what I said doesn't work.

Sparkline Printing to PDF

I have an issue where when I export to PDF via VBA my sparkline graphs are not printed. I've browsed your site, and a few others, trying to come up with a solution. Unfortunately I can't get it to work.
I'm the only one that uses the application, so the process is completely visible. I've tried to do all of the following before the export line in an effort to get the sparklines to 'refresh':
application.screenupdating = false then application.screenupdating = true
application.visible = true (based on forum here, even though it was never hidden)
select the cell where sparkline is located
select entire sheet where sparkline(s) are located
select.copy the cell where sparkline is located
application.wait to see if it would refresh
application.calculate to see if it would refresh
I really can't think of anything else to try. The spreadsheet is designed to create a report for a single entity, print the report, and then move on to create the next report for a different entity (pulls data from Access, creates over 200 10 page reports).
Any help is appreciated.
Thanks - Kris.
I had the same issue and I tried all of your listed ideas as well as DoEvents, but after testing the code line for line I found the offending code was:
.Axes.Vertical.MinScaleType = xlSparkScaleGroup
For some reason the xlSparkScaleGroup interferred with the update of the sparklines on the page and when I tried to update-print-update-print--- the sheet the sparklines were missing. My solution was to simply remove this code and then manually set the scales. Something like this:
.Axes.Vertical.MinScaleType = xlSparkScaleCustom
.Axes.Vertical.CustomMinScaleValue = Application.WorksheetFunction.Min(Range(SLAddress))
.Axes.Vertical.MaxScaleType = xlSparkScaleCustom
.Axes.Vertical.CustomMaxScaleValue = Application.WorksheetFunction.Max(Range(SLAddress))
where SLAddress was the address of the sparkline data I was using. I hope this helps solve your issue and maybe Microsoft will actually fix this issue.
Had to use a temp file to make it happen. Basically saved the current file as a temp file using 'savecopyas', then open the temp file (which allowed it to refresh the sparklines) do the print, close the temp file, and then start the process over again.
Hope they fix this at some point.
Kris.