|
-
Apr 15th, 2010, 12:10 PM
#1
Thread Starter
New Member
Stroning Excel, Word, and PDF docs in your program
I created a very simple program that organizes my various document and opens them when the button is clicked inside the program with the process.start method.
The problem is that when i take the executable file and move it to another machine or cd to run the program the process.start filepath is no longer valid.
Is there any way to store these files in my program so that they will be stored in my .exe file? I need to be able to Give this app to our sales teams. I dont know how to store these files within the program so that they will always be found.
Thanks in advance for the help.
-
Apr 15th, 2010, 01:06 PM
#2
Re: Stroning Excel, Word, and PDF docs in your program
Are these to be read only documents, or will they need to be modified and saved by the end user?
-
Apr 15th, 2010, 01:10 PM
#3
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
 Originally Posted by kleinma
Are these to be read only documents, or will they need to be modified and saved by the end user?
They will be Read Only.
-
Apr 15th, 2010, 01:12 PM
#4
Re: Stroning Excel, Word, and PDF docs in your program
you can add them as resource files to your exe which will embed them into the compiled application. They will be accessible as a byte array though, so when you would want to display one of these documents to the user, the easiest solution is that you write out a temp file with the contents of the byte array and then load up that file.
You can find the place to add these files as resources by going to project properties and then the resources tab.
-
Apr 15th, 2010, 01:31 PM
#5
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
Sorry im kind of new at this. I know how to use arrays, but how would i write a temp file with contents and load it up... all from the resource file?
-
Apr 15th, 2010, 01:55 PM
#6
Re: Stroning Excel, Word, and PDF docs in your program
ok, well one of the things to worry about is the fact that you are storing multiple types of files. When you add a file as a resource to a project, it strips the file type from it, and stores it as just an array of bytes. It doesn't care what type of file it is, but you do since you need to open a word document in word, or a PDF in adobe, etc...
So for the example, lets say I add a PDF to the project resources, and the PDF is called sample.pdf
When I add this to the resources, it is going to just call it 'sample'.
My suggestion would be to append the file extension to the name. You can't use periods, so use something like an underscore. So the resource name of 'sample' would be renamed by you to 'sample_pdf'. This will help when it comes time to figure out what type of file it is.
Then when you actually want to display the document via code, you just need to know what the original file name is that you want to show, and grab the resource file that matches, and write it out to a temp folder and launch it. Here is some sample code:
Code:
'THE DOCUMENT WE WANT TO LOAD
Dim myDocumentName As String = "sample.pdf"
'THE NAME OF THE RESOURCE THAT REPRESENTS THIS DOCUMENT (REPLACE _ WITH . TO MAKE VALID FILE NAME)
Dim myResourceName As String = myDocumentName.Replace(".", "_")
'TEMP FILE NAME
Dim myTempFileName As String = IO.Path.Combine(IO.Path.GetTempPath, myDocumentName)
'WRITE TEMP FILE TO DISK
My.Computer.FileSystem.WriteAllBytes(myTempFileName, CType(My.Resources.ResourceManager.GetObject(myResourceName), Byte()), False)
'LAUNCH TEMP FILE
If IO.File.Exists(myTempFileName) Then
Process.Start(myTempFileName)
End If
-
Apr 15th, 2010, 02:27 PM
#7
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
Thanks a million for the help. I think im almost there but i get an error
"Object reference not set to to an instance of an object."
It points to the part highlighted in red
'WRITE TEMP FILE TO DISK
My.Computer.FileSystem.WriteAllBytes(myTempFileName, CType(My.Resources.ResourceManager.GetObject(myResourceName), Byte()), False)
-
Apr 15th, 2010, 02:29 PM
#8
Re: Stroning Excel, Word, and PDF docs in your program
what is the exact name of the resource file you added to your exe?
-
Apr 15th, 2010, 02:48 PM
#9
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
Here is my Block of code
Code:
Private Sub CertificateButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CertificateButton1.Click
'document to load
Dim WBENCCretificate As String = "21WBENC Cretificate 2010.pdf"
Dim WBENCCertificate2010 As String = WBENCCretificate.Replace(".", "_")
Dim WBENCCertificateTemp As String = IO.Path.Combine(IO.Path.GetTempPath, WBENCCretificate)
My.Computer.FileSystem.WriteAllBytes(WBENCCertificateTemp, CType(My.Resources.ResourceManager.GetObject(WBENCCertificate2010), Byte()), False)
If IO.File.Exists(WBENCCertificateTemp) Then
Process.Start(WBENCCertificateTemp)
End If
End Sub
-
Apr 15th, 2010, 02:49 PM
#10
Re: Stroning Excel, Word, and PDF docs in your program
Did you actually add the "21WBENC Cretificate 2010.pdf" file to your project under the resources section though?
-
Apr 15th, 2010, 02:49 PM
#11
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
The exact name of the resource file is 21WBENC Certificate 2010.pdf under the properties and file name of my resource file
-
Apr 15th, 2010, 02:54 PM
#12
Re: Stroning Excel, Word, and PDF docs in your program
If the original file name was "21WBENC Certificate 2010.pdf" then adding it as a file resource would have named the resource as "21WBENC_Certificate_2010"
is that what you see in the resources section?
-
Apr 15th, 2010, 03:01 PM
#13
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
The name is "_21WBENC_Certificate_2010"
-
Apr 15th, 2010, 03:03 PM
#14
Re: Stroning Excel, Word, and PDF docs in your program
ah right because you also cant start a resource name with a number.
How do the users select which document they want to view?
-
Apr 15th, 2010, 03:10 PM
#15
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
they click on the appropriate button for the document they wish to view after they navigate to that part of the application.
Should i just rename the original files without numbers?
If that will fix it i can do that.
-
Apr 15th, 2010, 03:12 PM
#16
Re: Stroning Excel, Word, and PDF docs in your program
Yes, the easiest way to do it would be to rename your files to not start with numbers and to also not contain spaces. If you do that, it will work with my original example code. There are other ways that don't involve renaming, but they would likely be more complicated and require more code. If you can just rename the files from the start, that will make your life easier.
-
Apr 15th, 2010, 03:25 PM
#17
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
:-( It still doesn't work
name in resources is WBENCCertificat2010
Code:
Private Sub CertificateButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CertificateButton1.Click
'document to load
Dim WBENCCretificate As String = "WBENCCertificat2010.pdf"
Dim WBENCCertificate2010 As String = WBENCCretificate.Replace(".", "_")
Dim WBENCCertificateTemp As String = IO.Path.Combine(IO.Path.GetTempPath, WBENCCretificate)
My.Computer.FileSystem.WriteAllBytes(WBENCCertificateTemp, CType(My.Resources.ResourceManager.GetObject(WBENCCertificate2010), Byte()), False)
If IO.File.Exists(WBENCCertificateTemp) Then
Process.Start(WBENCCertificateTemp)
End If
End Sub
when the error comes up it the title says "NullRefrenceException was unhandled"
Under that it says "Object reference not set to to an instance of an object."
trouble shooting tips are:
"Use the "new" keyword to create and object instance."
"Check to determine if the object is null before calling the method."
Sorry for how much of a problem this is and thanks for the help.
-
Apr 15th, 2010, 03:31 PM
#18
Re: Stroning Excel, Word, and PDF docs in your program
you are almost there. Go back into resources and rename WBENCCertificat2010 to be WBENCCertificat2010_pdf (remember this is how you will be able to know what type of file it is so you can append the correct extension on it to write it out to a temp file. Then when you launch the file, the extension will tell windows what app to open it with, adobe, word, etc..)
so when you add the file to the resources, you will need to add _pdf or _doc, or whatever the correct extension is, to the resource name itself. Then it should work.
-
Apr 15th, 2010, 03:35 PM
#19
Thread Starter
New Member
Re: Stroning Excel, Word, and PDF docs in your program
Thank you so much!!!!!!!!!
I will be using the same method in many of my projects.
You helped me so much.
Thank you for taking the time and sticking with me :-)
-
Jul 26th, 2010, 02:18 PM
#20
New Member
Re: Stroning Excel, Word, and PDF docs in your program
 Originally Posted by kleinma
ok, well one of the things to worry about is the fact that you are storing multiple types of files. When you add a file as a resource to a project, it strips the file type from it, and stores it as just an array of bytes. It doesn't care what type of file it is, but you do since you need to open a word document in word, or a PDF in adobe, etc...
So for the example, lets say I add a PDF to the project resources, and the PDF is called sample.pdf
When I add this to the resources, it is going to just call it 'sample'.
My suggestion would be to append the file extension to the name. You can't use periods, so use something like an underscore. So the resource name of 'sample' would be renamed by you to 'sample_pdf'. This will help when it comes time to figure out what type of file it is.
Then when you actually want to display the document via code, you just need to know what the original file name is that you want to show, and grab the resource file that matches, and write it out to a temp folder and launch it. Here is some sample code:
Praise you!!!
Spent quite a bit of time on this for an excel file...finally got er going...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|