Word error when processing multiple documents - vba

Right now we have a program that opens all of the word files in a folder and adds some information to the footer. After switching from Windows XP and Word 2003 to Windows 7 and Word 2007 we have started getting the following error:
Run-time error '5096':
EOALPHABETICARABICARABICABJADARABICALPHABAHTTEXTCAPSCA
(c:\Users...\Content.MSO\8BE508C6.docx)
It occurs at random in any of the files (you can be 10 files in or 100). I thought that it might be a problem with the files being on a network drive so I modified the program to copy each file locally first, add the footer to that copy, and then copy it back out to the network. However, that didn't seem to work. I'm looking for any suggestions on how to fix the problem or possibly rewrite the solution. However, I need to keep the solution in VBA since this app is part of a bigger suite which in not ready for a .NET re-write.

As it turns out I was able to workaround this problem by copying the word files locally, running the main code on the local copies, then copying them back out to the network. It didn't seem to be working at first because the user's machine on which I tested it did not have the most recent version of the program.

Adding this note for those still looking for answers to this error...
I have a macro that creates several docs from one that is open based on sections in the main document.
I was getting the Run-time error '5096' and was an able to find any solutions that worked.
Upon further examination while debugging the code I discovered that while pulling data out of the main doc and building a file name that there was a carriage return after the file name and before the extension. After adding a replace statement to remove carriage returns I am able to process documents all day without an error.

I have found that the error is caused by the total number of characters in the file name is too long. This usually occurs when manipulating file names and the full path is set in the variable. I will manually chop pieces out of real long names to force it to work.
I suppose if I were smart, I'd move into the directory and just deal with the file names without the directory prepended to the name.

Related

How to Create a Program Which Searches for Values from a .txt or any Text Document in Specific Folders

I am relatively new to programming and want to create a program which can solve a problem that I frequently have.
So here's the background to my short story: I was on a website which hosted many files (We're talking about around 500-1000 small files). I was then like," Oh sweet! I want to have all these things in my hard drive so I know that I have access to them... but am probably not going to use them either way". I proceeded to download all 500-1000 files on that site, but encountered a problem when I looked at the properties of my destination file. Let's say that out of 500 on the site, my computer only had 499 files. Just my luck. I wanted to know what was that one pesky file that slipped right by me and download that file specifically. What I didn't want to do was to delete all the files and then try my luck once more in downloading all the files from the website. On the site, there was no indication of what all files I downloaded, so I was completely in the blue. I could go in Ctrl+C each item, then Ctrl+V into the file manager search bar, but that would be tedious to repeat that 500 times.
Now, what I want to do: I wanted to go ahead and take all of the file names from the website (The file name that I downloaded and the file name that was in my drive are the same), put them all in a simple .txt document or something (The website has multiple unwanted text alongside the text I need, such as:
. If this is not possible to extract the text from the site like this, then I am ok with manually entering the names via copy paste). Then I want the computer to take these values in the document and then search for it in a specific folder path (Note: the actual files are in subfolders within the root folder I want to choose, so the program has to be able to search within multiple folders of the root). Then I want the computer to know if the value in the document, is present as a file. If the file doesn't exist, then I want that value/those values in the document to be displayed as the output. I want this cycle to repeat until all the values have been gone through. The output should list the values that were not present.
Conclusion: You probably now get at what I am trying to do, if you don't, tell me what I need to elaborate on. I really don't care how this program is made (what language or software), I just want something that works... but myself don't know how to create.
Thanks for reading and any response is appreciated!
Dhanwanth P :)
Here's a solution in Python in case you would like to explore...
Similar to what you described, all files from the website are listed in an Excel file 'website_files.xlsx'
And all files are saved in a folder 'downloaded_wav'. The script will work regardless the files are saved in the root directory or sub-folders.
Then I run below Python script to look for the missing file:
import pandas as pd
import os
path_folder = 'C:\\Users\\Admin\\Downloads\\downloaded_wav'
downloaded_files = []
d,m = 0,0
for path_name, subfolders, files in os.walk(path_folder): #include all subfolders
for file in files:
d+=1
downloaded_files.append(file)
df = pd.read_excel('website_files.xlsx')
for file in df.values:
if file not in downloaded_files:
print('MISSING', file)
m+=1
print(len(df), 'files on website')
print(d, 'files downloaded')
print(m, 'missing file(s) found')
Output:
MISSING ['OLIVER_snare_disco_mixready_hybrid.wav']
3 files on website
2 files downloaded
1 missing file(s) found
No worries; I found a solution by myself using Excel (God, it's powerful!).
Basically, I copied and pasted my values from the website, then used a filter to show the values only with .wav. Then I used a Power Query from the folder to get me a list of all names of files in a folder. Finally, I went ahead and compared the two using a formula:
=IF(COUNTIF(B:B,D,"OK","MISSING")
If you need more elaboration, I'd be happy to help, just reply to this. There might be an easier way, but I personally liked the straight-forwardness of this. You only need Microsoft excel!
EDIT:
For me, I used these two videos which go over the power query and countif function:
How to Get the List of File Names in a Folder in Excel (without VBA): https://www.youtube.com/watch?v=OSCPVBWOqwc
How to Compare Two Excel Sheets (and find the differences): https://www.youtube.com/watch?v=8Ou_wfzcKKk
In my case, I made my sheet look like this:

Why Google Colab thinks my file does not exist despite it being mounted from my Drive?

I made sure every single thing is correct with the related file names and their content addresses and so on yet I always get stuck upon executing the !sudo line command which should open and use a given file, it returns this error :
"Error: Cannot read file '/content/-p': No such file or directory"
As I have already said, the file does exist and is located in my Google Drive, I even copy pasted its specific path to make sure I put it right and yet the issue is still there, why? how can I solve it?
Thanks in advance for any help.
Two scenarios in my experience:
(1) It's the first time running the lines of code after mounting your google drive and for no apparent reason it runs for tens of minutes, produces not output, raises an error (e.g. "File does not exist"), and the session crashes; you repeat the exact same steps, then it works.
(2) There are thousands (or more) files within the folder that contains the file you're trying to read or write; when a google drive folder contains many files (i.e. thousands or more) it may crash for that reason.

Office server automation failed while calling "CopyPicture" on some computers

We have wrote an application with C++/Qt which reads some data from multiple excel files and then generate a word report. We have used benefits of Office automation server to read/find and copy data from excel files and write them into a single word file.
The application works fine without any significant problem in my office computers. Yesterday I've decided to copy the app into my laptop which has same OS and office version. After running the app, it seems that somethings wrong my OS/Office. None of the data are copied into word file. After debugging I've figured out that the app actually can read/write office files but failed to call CopyPicture method of automation!!!
I wanted to copy the range as picture, so I have used "CopyPicture" method. but it seems that the vba fails to call copyPicture on my laptop.
I'll be very appreciated for any hints. Thank you
I've found the problem and it's with excel docs which are used to copy data from. In these files, there are some embeddedCharts. after removing these charts the code works fine!!!! (very odd). Anyway, I've tried to make excel file visible in order to see if the operation goes well or not. After running (with embeddedCharts) the code worked fine (more odd!!)
excel->dynamicCall("Visible", true );
I don't know the reason but with making excel file visible, the error with copyPicture is gone!

VB.net: Is there a way to get the Printed File Path from Printform?

im working on my first big program. I will try to explain short how the important part of the program works, and then try to explain what my problem is.
My program is used by few people, they drive around the Europe and repair our machines. After the work, they start my program, and write a report. Until now the program was generating at the end 3 Files. (PDF file generated by printform, text file which contained the same information's again and the last file that was an Excel file, that one contained Data that was written inside the Datagridview.
These workers, used Email to send all 3 files separated. As you imagine, sometimes that can end bad, cause after work they are tired and sometimes they send the wrong files. So I made a upgrade, which gives the user a possibility to send the files directly from the Program, being sure everything is fine. In background I created a directory where 2 of 3 files always get saved. The problem is, while using printform, there opens a window where the user can select the path. And here start the troubles, some of the workers select different a different path, but then my program wont find the files again(its very important that are 3 files are together). I searched for something that would look like
dim printformpath as string = printform.getpath
is there something that works that way? I was searching but I didn't found anything helpful.
Thank you for understanding & help
Thanks, I added the path to the printFileName property and changed printform settings from Print to preview to print to file :)
Have a nice day

Can't copy to network path using Filesystem.filecopy or FileSystem.CopyFile

I'm trying to copy a file from a local computer to a remote share but I get an exception saying "Can't find the specified file".
My.Computer.FileSystem.CopyFile("C:\filename.jpg", "\\focserver2\consultoria\teste\filename.jpg")
The remote shared folder has full control permissions on "Everyone".
What am I doing wrong? Or it's not possible to copy to network paths using FileSystem.CopyFile ?
Thanks.
João
The issue does not appear to be the destination but the source. I assume that the sample you showed above is NOT the real code and in the real code, you combine a couple values together to define the source. To help prevent issues, get in the habit of using the Path.Combine() method when concatenating strings for a file path. It's a life-saver.
The next most important thing is to learn how to debug your code by setting break-points and seeing what the values of your combined strings are before posting to websites. This is a good one to get you started. http://weblogs.asp.net/scottgu/debugging-tips-with-visual-studio-2010
Most likely the problem is that you are trying to access the root folder of the C:\ drive. This folder is generally locked down by the operating system, and even simple file operations don't work easily there.
Try copying the file from a subfolder e.g.
My.Computer.FileSystem.CopyFile("C:\Temp\filename.jpg", "\\focserver2\consultoria\teste\filename.jpg")