Permission Denied while running vb script - scripting

I have written a vb script to open a file with different filetype other than the original filetype(eg : from .doc filetype to .txt filetype)
below is my code
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run("notepad" & "C:\OpenWith_Shell32_Doc.doc")
the above code was working fine but one day i edited the 'notepad' to 'wordpad' it is giving the below error..but i undo the changes then also it is giving the same error...
Microsoft VBScript runtime error '800a0046'
Permission denied

missing space between "notepad" and "C:\OpenWith_Shell32_Doc.doc" should work with notepad and wordpad (if you put a space in)

Related

Move files between folders in the server with vba

I'm trying to move a pdf file from a folder to another both located in the server (a Teams sharepoint).
I've used the code below (which is working perfectly on local) but i got the following error :
PS : I tried to access the "SourceFileName1" when debugging by copy paste in my browser and the file is here
Dim FSO As Object
Dim SourceFileName As String, DestinFileName As String
Set FSO = CreateObject("Scripting.Filesystemobject")
SourceFileName1 = "https://comxx.sharepoint.com/.../folder1/myfile.pdf"
DestinFileName1 = "https://comxx.sharepoint.com/.../folder2/myfile.pdf"
FSO.MoveFile Source:=SourceFileName1, Destination:=DestinFileName1
I've tried also with a path like this "\\comxx.sharepoint.com....\myfile.pdf" i got another error (below). I thought it was a permission issue but in my code i'm exporting this file (myfile) into the folder1 without problem.
Do you have any idea about how to handle with this issue ?
Thanks

Deleting a file immediately after closing it - 'Permission Denied' error

I want to save an email as a Word file through a macro, and then replace this with a converted PDF file via Word. The conversion part is working fine - the issue is when I try to delete the original .doc file.
Dim objWrd As Object
Dim objWrdDoc As Object
Dim wrdCurrentPrinter As String
Set objWrd = CreateObject("Word.Application")
Set objWrdDoc = objWrd.Documents.Open(filePath & fileName)
''Print' file as PDF - current printer is stored so it can be reverted back afterwards
wrdCurrentPrinter = objWrd.ActivePrinter
objWrd.ActivePrinter = "Microsoft Print To PDF"
'File name is specified here to avoid Save As prompt. PrintToFile property is 'True'
objWrd.PrintOut , , , filePath & Replace(fileName, ".doc", ".pdf"), , , , , , , True
objWrd.ActivePrinter = wrdCurrentPrinter
objWrd.Quit
Set objWrd = Nothing
Kill filePath & fileName
At Kill filePath & fileName I get error 70 - Permission denied.
I am able to delete the file manually without any problems, and if I add a breakpoint and step through the 'Kill' line, it works when there is even a slight delay between Word closing and the Kill command. Therefore I suspect that the code is being processed so quickly so that the file is still open at the time of running the command.
I have a feeling I may need to go down the route of creating a delay, which I have been having trouble with in Outlook. Is this the likely solution or is there something else that I have missed?
I have been able to get this to work consistently by simply closing the Word document before quitting Word altogether:
objWrdDoc.Close
objWrd.Quit
Set objWrd = Nothing
The error has not appeared since adding this line and testing with various emails.

How do I specify the file path in vba code in powerpoint 2011 for mac?

I want to access an image file in my VBA code for PowerPoint 2011 for Mac.
I have used both "/" and ":" as separator but it didn't work.
It works only when I save both .pptm file and the image file in the same folder and specify the relative path for the image file as below:
Dim strPath As String
strPath = "image.png"
But if I try saving the file at a different location than the .pptm file and provide the full path for the StrPath as:
strPath = "Users:Me:Desktop:image.png"
it throws a runtime error saying "the specified file wasn't found".
I have kept in mind the case sensitivity in case of Mac but nothing seems to be working for me.
Can anyone please help me in finding the possible workaround so that I can save the .pptm file and the image file at different locations on Mac?
Try:
Presentations.Open "Macintosh HD:Users:Me:Desktop:filename.ext"
Substitute appropriate strings for the name of the HD, your user name and the file.

Open a text file with vb.net , and if it exists delete that file

I am writing code for a windows application using vb.net. I want to open a text file under c:\. If the file already exists I want to delete that file.
my code
-------
Dim file As String = "C:\test.txt"
If System.IO.File.Exists(file) Then
file.Remove(file)
Else
System.Diagnostics.Process.Start(file)
End If
I am getting the following error when I try to open that file.
error
-----
The system cannot find the file specified
Apart from Konrad's point about trying to execute a file that you have just checked does not exist:
1) It's not a good idea to name your variable file as it could get confused with System.IO.File.
2) It's File.Delete, not file.Remove - you're calling the String.Remove method because file is a string. You should use Option Strict On because it would have caught that error for you.
3) On Windows Vista and later, you may not have write/delete access to C:.
Assuming you have write access to the directory C:\temp then this works:
Dim fyle As String = "C:\temp\test.txt"
If System.IO.File.Exists(fyle) Then
IO.File.Delete(fyle)
End If
IO.File.Create(fyle)
System.Diagnostics.Process.Start(fyle)

Permission Denied Error During Update of a Local MSOffice Add-In from Network

I am attempting to write a procedure in PowerPoint 2003 that will allow automatic updating of an installed add-in. The general process is as follows:
Uninstall the add-in
For Each objAddIn In Application.AddIns
If UCase(objAddIn.Name) = UCase(AddInName) Then
With objAddIn
.Registered = msoFalse
.AutoLoad = msoFalse
.Loaded = msoFalse
End With
End If
Next
Delete the file from the local Add-Ins directory
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(FileName) Then
Set objFSO = Nothing
Kill FileName
End If
Copy over the file from the network location
Install the updated add-In
Upon reaching step 2, any attempt at deleting the file post-uninstall using either the FileSystemObject or a straight Kill inevitably generates Run-time error '70': Permission denied. If I hit Debug and then play, it runs through as if there was never a problem.
Side note: I realize I can use FSO to overwrite the local file, but that gives me the same run-time error.
I'm guessing the problem has to do with some aspect of the file being in use, but I can't figure out how to "release" the old add-in so that the underlying file can be deleted.
Does anyone have insight that can help?
You need to remove it from the Addins Collection before it can get physically deleted. Put this, right after your End With:
Application.AddIns.Remove objAddIn.Name
The KB Article that refers to a VBA function you can use to CHECK for a locked file is here.
http://support.microsoft.com/kb/209189
It contains a simple function you can add to your VBA to check that a file is not locked and uses the same code sample that Otaku refers to in his answer.
What I would do is...
Replace the Kill Filename line in your code
If FileLocked(Filename) = True Then
SetAttr Filename, vbNormal
Kill Filename
End If
*Where FileLocked is a custom function referred to in KB209189
**If this doesn't work, reply back, we could also look at replacing the Kill statement above with a lower level Win32 function that does the same thing (but perhaps more throughly). :D