Solved

How do I use a template saved as a resource in Visual Studio 2013?

Posted on 2016-09-15
13
107 Views
Last Modified: 2016-09-20
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:\Users\kawilso1\Documents\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.Resources.WSTF489)

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.
0
Comment
Question by:Karen Wilson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41799845
HI,

I think you have to extract it on disk to open it subsequently

or maybe by referencing it
Dim oDot As Word.Document
oDot = My.Resources.WSTF489

Open in new window


Regards
0
 

Author Comment

by:Karen Wilson
ID: 41799877
That does not work.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41799885
Sorry cannot help further
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41806436
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)

Open in new window

0
 
LVL 34

Expert Comment

by:ste5an
ID: 41806479
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..
0
 

Author Comment

by:Karen Wilson
ID: 41806491
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.
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41806500
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.

[..]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?
0
 

Author Comment

by:Karen Wilson
ID: 41806509
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?
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41806566
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.
0
 

Author Comment

by:Karen Wilson
ID: 41806586
- 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.
0
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 41806596
No, don't create such directories. That's what %ProgramData% is for. Use %ProgramData%\YourApplicationName\.
Press Win-R, enter %ProgramData% and press OK. For normal users this is a read only location for such data.

The rest is indeed okay. Cause it's simpler to just deploy a template, when necessary, then an entire application.
0
 

Author Closing Comment

by:Karen Wilson
ID: 41806605
Thanks.  I will try it out and see what happens.  Have a good evening!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question