Under my project, i have created a directory called "Tempaltes" and have put there several excel files I want to use later in my code. When working with those files, I want to use indirect paths. Let's say I want to open a file: \Templates\tempalte.xlsx
. How should I do that?
Dim fs = CreateObject("Scripting.FileSystemObject")
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Open("/Templates/template.xlsx")
When doing that I get an error, that file /Templates/template.xlsx could not be found.
Of course everything works fine when I use direct paths to those files, but that makes the project less universal and causes potential issues when migrating to other platforms.
gadujr
Member
14 Points
21 Posts
Using indirect paths when working with files
Apr 29, 2012 02:02 PM|LINK
Hi,
Under my project, i have created a directory called "Tempaltes" and have put there several excel files I want to use later in my code. When working with those files, I want to use indirect paths. Let's say I want to open a file: \Templates\tempalte.xlsx . How should I do that?
Dim fs = CreateObject("Scripting.FileSystemObject")
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Open("/Templates/template.xlsx")
When doing that I get an error, that file /Templates/template.xlsx could not be found.
Of course everything works fine when I use direct paths to those files, but that makes the project less universal and causes potential issues when migrating to other platforms.
THanks
K.
Vipindas
Contributor
5514 Points
810 Posts
Re: Using indirect paths when working with files
Apr 29, 2012 02:14 PM|LINK
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("~/Templates/template.xlsx"))
Try this
gadujr
Member
14 Points
21 Posts
Re: Using indirect paths when working with files
Apr 29, 2012 02:17 PM|LINK
I owe you a beer :) That's what I was looking for. Thanks a million Vipindas!
Pravin8712
Member
8 Points
19 Posts
Re: Using indirect paths when working with files
Apr 29, 2012 07:28 PM|LINK
Hi,
use tild symbol at the start of your path like,
xlWorkBook = xlApp.Workbooks.Open(Server.MapPath("~/Templates/template.xlsx"))
Thanks
Pravin