Reopen DOC file in write mode, with macro inside it - vba

I am using a macro template for particular DOC files only. It runs only for these files and, as I assume, it is treated as the DOC file itself.
All I want is to remove the file OR clear the contents of it, after the macro finishes. The initial file is read-only.
I have tried:
SavingAs in TEMP folder, change file attribute of initial file and then try to Kill it - no luck, it seems that initial file is still preloaded(?) and does not know that the read-only flag was removed. Error: Permision denied.
When I try to Close the initial document, I am closing also the macro itself...
So:
Is there any way to (from the macro inside the DOC file) :
Reopen the file in write-mode? (so I could clear the contents afterwards)
Remove the initial file? (after SaveAs so I actually will have a new file in write-mode and will be able to remove initial file)

Related

Vba do not open wbook

There isn't enough memory to complete this action" in Excel
How to fix it without opening file
İ want to turn calculation manual but without open it is impossible
In order to change the automatic calculation to manual without opening the .xlsx file, you can do this. Before doing this, be sure to make a copy of the file and try these manipulations on the copy.
Unzip the .xlsx as a .zip archive, open the file ...\xl\workbook.xml in a text editor, find a tag similar to the following <calcPr calcId="191029"/> and add to it calcMode="manual" so you get <calcPr calcId="191029" calcMode="manual"/>. Then save this file, package the entire directory, and change the extension to .xlsx.

How to copy a file without extension and at the end of the copy put it?

I am using teracopy to move some files. I would like to make a form in visual * .exe where you simply have two windows: one with the file's origin on the right, and another with the file's destination.
I need to copy the file without its extension, verify that the transfer occurred without issue, and then restore the original extension. For example:
Copy file.mov to // smb: Storage / Movies /
Remove the .mov extension
Copy the file to the storage location
Validate file size
If the file size at the destination matches the source, restore the .mov extension
If the file size doesn't match, delete the destination file and notify the user of the error.
The files I plan to copy are very large, and will take a good amount of time. I'm removing the extension so that users on the server will not accidentally try to access a file that isn't completely transferred yet - the idea is they see an "extensionless" file and know not to try and open it, whereas any files with extensions are certain to have copied over correctly.
I've tried using CMD and powershell, but I've only managed to copy the file or retry copying if the copy fails. I don't yet know how to remove the extension and restore it.
> ROBOCOPY /Mir <Source> <Target>
You can try this:
copy %UserProfile%\YourFile.* "Path to copy to"

Print button didn't save the PDF file at a shared path, and no error logged

I have an Excel macro that uses SAP for printing data in pdf format.
Steps:
it access the SAP transaction which provides a table with the necessary information
it press Print button (from the Menu Bar of SAP)
then Print window appears (from here it is selected Microsoft Print To PDF option and then it is pressed OK button)
Save As window appears (complete the path and filename: \S\BC....\outputName)
then Save button it is pressed
then no error happens
But if I access the path folder "\S\BC....", nothing happen, there is no file saved. BUT, if we look in "Date modified" property of the folder, it is modified in accordance with the last saving time.
Also, I mention that if I'm trying to write the pdf file on local folders, not network shared folders (example: a desktop folder: C:\Users\mariah\Test), macro works.
Also, I mention that user can Read&Write at the shared path \S\BC....\
Please help me to find the cause of this issue.
A VBA macro only does what you are allowed to do manually. Can you manually save the file in the shared path without the macro?
Is the common path really \S\BC...\ or \ \S\BC...\ ?
Regards,
ScriptMan
Solution proposed by the OP in this comment:
IT WORKS if I save the file on "C:\Users\UserName" and then cut it to shared path "S\BC...". So I've implemented code that saves the file in a path and then copy it to another path and then I deleted it from the first path (such that user never knows that I copy the file in other destination than desired destination).

Programmatical and application-based editing of an Excel file

Good evening,
I have the following problem to solve:
I want to add to an Excel file the contents of a bunch of user-generated .txt files. These files are generated throughout the day and sent over FTP to a folder, which is being constantly monitored by the program to see if there are new additions.
If the program finds new .txt files in it, it opens the Excel file which is to be edited, adds the info and then closes the Excel, saving the changes.
At the same time, users have to open these Excel files to check the new updated info and deal with it accordingly.
The program's execution is somewhat like this:
Infinite loop checking if the folder is empty or contains new .txt files.
If the folder is not empty (hence there is info to be added), it checks whether the Excel file is open or not.
If the Excel file is closed:
Opens it programmatically and adds the info to it.
Saves the Excel file and quits.
Backups the .txt to another folder in case there was some sort of error.
If the Excel file is open:
Keep checking until it is closed.
To check if the folder is empty or not I use: System.IO.Directory.EnumerateFileSystemEntries(path).Any()
To check if the Excel file is open I use:
System.IO.FileInfo(path) and a FileStream which, inside a Try-Catch clause, opens the FileInfo in the following mode: info.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
For the opening, editing and saving/closing of the Excel file I use Microsoft.Office.Interop.Excel.Application, [..].Workbook and [..].Worksheet.
The main problem comes when a user opens (through the normal Office Application) the Excel file in the middle of the addition process. I was hoping to find some sort of lockage of said file so the user cannot interrupt the editing process.
Any ideas on how to combine both types of opening/editing the Excel file?
Thank you so much in advanced.
TL;DR: How can I prevent, programmatically, a user from opening an Excel file while a program is editing that very Excel? Or at least, open another instance of it so the process is not interrupted?
PS: If any further code should be needed, I'll gladly post it :)
I think you need a better solution. It sounds like you are maxing out the capabilities of Excel. You would be better off, if possible, to use a database.

How to open a file from a stream in Notepad

To open a file I could do this like
Process.Start(fileName)
For this it is necessary that the file exists in the file system. Now I use a XmlDocument object and want to open the content in an external program like Notepad. I save the xml content in a stream but have no idea how to show it in another program.
Dim MyXmlDocument As New XmlDocument
'do something with the document
MyXmlDocument.Save(MyXmlStream)
If it isn't possible I have to save it first and open it in a second step. I only want to avoid the saving of the file in the file system.
I think it is necessary to save in the file system. However, you could create a temporary file and execute a delete command when you're finished with the file. This way it does not clutter the FileSystem. The AppData folder is a good place to start.