Karen Wilson
asked on
How do I use a template saved as a resource in Visual Studio 2013?
Windows Forms Application - Visual Studio 2013 Using vb.net.
I have a template that I use to produce a form. I generate it by using this code:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
oWord = New Word.Application
oWord.Visible = True
oDoc = oWord.Documents.Add("C:\Us ers\kawils o1\Documen ts\WSTF489 .dotx")
Everything works fine. I now find myself having to deal with our IT department moving my file locations on a more frequent basis. I have decided to add my templates as a resource within my application in hope to resolve this situation.
I add my Word template to Resources as an existing file. Now I want to use it. I use this code:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
oWord = New Word.Application
oWord.Visible = True
oDoc = oWord.Documents.Add(My.Res ources.WST F489)
I get an error: Type mismatch. (Exception from HRESULT: 0X8002005 (DISP_E_TYPEMISMATCH))
I would appreciate any direction or instruction on how to solve this.
Thanks.
I have a template that I use to produce a form. I generate it by using this code:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
oWord = New Word.Application
oWord.Visible = True
oDoc = oWord.Documents.Add("C:\Us
Everything works fine. I now find myself having to deal with our IT department moving my file locations on a more frequent basis. I have decided to add my templates as a resource within my application in hope to resolve this situation.
I add my Word template to Resources as an existing file. Now I want to use it. I use this code:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
oWord = New Word.Application
oWord.Visible = True
oDoc = oWord.Documents.Add(My.Res
I get an error: Type mismatch. (Exception from HRESULT: 0X8002005 (DISP_E_TYPEMISMATCH))
I would appreciate any direction or instruction on how to solve this.
Thanks.
ASKER
That does not work.
Sorry cannot help further
This will work
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
oWord = New Word.Application
oWord.Visible = True
Dim bytes = My.Resources.Resource1.WSTF489
Dim path As String
path = "some path you have access to.dotx"
System.IO.File.WriteAllBytes(path, bytes)
oDoc = oWord.Documents.Add(path)
Everything works fine. I now find myself having to deal with our IT department moving my file locations on a more frequent basis.
Why does this matter? Just use relative paths and a proper folder structure in your deployed project.
Using the resource path has a code smell: You need to deploy a new version for ever template change..
ASKER
ste5an - I'd like to package this up and send it off to other groups within the agency that don't necessarily have access to the file location due to security reasons. I feel that if I could include the template in the resources, I could eliminate this problem. Does that make sense? And I guess I don't understand "proper folder structure in your deployed project." Can you expand on that as well? I think I'm doing that, but maybe I'm not.
First of all: "Security reasons" I know IT departments which will actively block such an approach of yours, cause it may be an attack vector.
Do you use any kind of installer? Or other: How do you deploy your application?
[..]access to the file location due to security reasons[..]How do they run your application?
Do you use any kind of installer? Or other: How do you deploy your application?
ASKER
I deploy it on our network. If I were to package it and send it on, they would use their network. But then I'd have to send all the templates and they would have to create like folders etc. on their network.
I'm also curious as to how this works in general. If I have a word template in resources, how does one access it to work?
I'm also curious as to how this works in general. If I have a word template in resources, how does one access it to work?
Normally you use an installer to pack all dependencies into one setup.msi.
Depending on the installer tool, you have different options. The main two:
- a normal user based installation. Then on each users machine this must be run. Any data files are copied by the installer to %ProgramData%. This is a known location for invariant application data. E.g. your template. The user has no write access to this location.
- a network based installation. This is a two step operation. First the network files are copied to the network location. Then on each machine the installer is run to install necessary .Net or other code dependencies. During this run, the installer writes the share name, where the network files are located to the registry. Then you program uses this entry to locate its data files.
Depending on the installer tool, you have different options. The main two:
- a normal user based installation. Then on each users machine this must be run. Any data files are copied by the installer to %ProgramData%. This is a known location for invariant application data. E.g. your template. The user has no write access to this location.
- a network based installation. This is a two step operation. First the network files are copied to the network location. Then on each machine the installer is run to install necessary .Net or other code dependencies. During this run, the installer writes the share name, where the network files are located to the registry. Then you program uses this entry to locate its data files.
ASKER
- a normal user based installation - I would code, "Upon installation, create a read only directory (C:\MyAppTemps) and then copy my templates that are in resources, to that folder." All my code inside the application would then be pointed to the C:\MyAppTemps folder? Then if I made a change to the template, I'd do a remove and replace in that directory.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I will try it out and see what happens. Have a good evening!
I think you have to extract it on disk to open it subsequently
or maybe by referencing it
Open in new window
Regards