Trouble Saving Excel ony my computer using c# - c#

I have a app that should create a excel spreadsheet and save it. The app works runs without any problems on other machines, including my bosses computer(he has the same ver of office and VS as me). I don't know if this is relevant but I have office 2013 and VS 2012.
Edit I'm trying to save as a .xls (exel 97').
SNIPIT
public static bool XLSaveAs(ref Excel._Workbook oWBTemplate, string FileName)
{
System.IO.FileInfo x = new System.IO.FileInfo(FileName);
if (x.Exists)
{
try
{
x.Delete();
}
catch
{
MyControls.MsgFunctions.WarningMsg("Make sure " + FileName + " is not open.");
return false;
}
}
try
{
if(FileName.Contains(".xlsx"))
oWBTemplate.SaveAs(FileName);
else
//Error occurs here
oWBTemplate.SaveAs(FileName, Excel.XlFileFormat.xlExcel8);
}
catch
{
MyControls.MsgFunctions.WarningMsg("Unable to save " + FileName);
return false;
}
return true;
}
If I take out the try catch statement this is the error I get.
ERROR
COMException as unhandled
Exception from HRESULT: 0x800A03EC

I found a solution that worked for me.
I had incorrect permissions to edit the folder that excel was writing too.
Adding full control to the folder resolved the issue.
Also the command oWBTemplate.SaveAsCopy() could help if you have a similar issue.

Related

C# File.AppendText(path) Access to path denied

I know this is a common problem with a lot of related topics on here. But none of them seem to work for me.
I have code that works on a production system that I've copied across to my local home computer:
private static void WriteToLog(string logText, string logPath)
{
try
{
using (StreamWriter outputFile = File.AppendText(logPath))
{
outputFile.WriteLine(DateTime.Now.ToString() + "|| " + Regex.Replace(logText, #"\t|\n|\r", ""));
}
}
catch (IOException ex)
{
//what do?
throw ex;
}
}
The line using (StreamWriter outputFile = File.AppendText(logPath)) throws the classic exception:
System.UnauthorizedAccessException: 'Access to the path
'C:\Users\Jaso\Documents\DataChecker_Logs\schema_a-academic_attainment.txt'
is denied.'
At runtime the path variable contains "C:\\Users\\Jaso\\Documents\\DataChecker_Logs\\schema_a-academic_attainment.txt"
The Security of the folder in question looks like this:
When I find the user the process is run under using WindowsIdentity.GetCurrent().Name;, the value returned is "DESKTOP-LMMBET3\\Jaso" which according to the folder's settings (above screenshot) is a principal with full control!!
Windows 10 machine.
GRRR!!!
Check the permission of the file itself not the folder
if you don't have the permission to access the file this error will be thrown
System.UnauthorizedAccessException

Program using LinqToExcel works on my computer but after publishing and installation on another one it does not

Published and installed program using LinqToExcel throws an "Exception has been thrown by the target of an invocation" exception. Inner exeption is not displayed but on my computer if I run exe file separately w/o another source files there is Could not load file or assembly linqtoexcel. but it is only on my PC when I tried to run it separately. But I beleave it is similar?
Both PCs are Win10, Access DB Engine 64bits or 64bits Offices are installed.
public void ImportNewData(String file)
{
ExcelConnector excel = new ExcelConnector(file);
foreach(var result in excel.ReadNewData())
{
this.loopsList.Add((Loop) result);
}
}
public IEnumerable ReadNewData() {
try
{
var query = from a in this.ExcelConnection.Worksheet < Loop > ("Data")
select a;
return query;
}
catch (Exception exeption)
{
MessageBox.Show(exeption.Message + "\n" + exeption.InnerException.Message + "\n" + exeption.InnerException.Source, "Warning");
return null;
}
}
I expect it will run on any Windows machine :)
I unchecked this check box and now it works also with 64bit Office.

The given path's is not supported C#

I have an strange problem I have an app that scan a directory and gets a list of files. it processes each file by reading it and doing some stuff. it works fine in the development computer but when I deploy it to the client it gives me the error. Here is the code
public void ProcessIMFiles()
{
DirectoryInfo di = new DirectoryInfo(Globals.ITMDIR);
FileInfo[] Files = di.GetFiles("*.txt");
foreach(FileInfo file in Files)
{
try
{
processThisIMFile(file.FullName);
movefile(file.FullName);
}
catch (Exception ex)
{
MessageBox.Show("error : " + ex.Message);
}
}
}
The error happens in the call to processThisIMFile(file.FullName) see below.
Globals.ITMDIR is a valid path.
private void processThisIMFile(string FileName)
{
string[] Fields = null;
setconnection();
DataTable dt = null;
try
{
string[] Lines = System.IO.File.ReadAllLines(FileName);
foreach (string line in Lines)
{
Fields = line.Split(Globals.delimiter);
if (Fields.Length == 7)
{
//stuff happens here
}
}//Try
catch (Exception e)
{
if (Interactive)
{
MessageBox.Show("Error in the Path: ->" + FileName);
writeToLog(true, "error opening file " + FileName);
}
}
}//end of processThisItemFile
the error happens in the "string[] Lines = System.IO.File.ReadAllLines(FileName)"
line. FileName comes from the di.GetFiles("*.txt"); when I show the actual path it looks ok to me. I have tried with UNC paths and with drive letters path as in C:\tmp\filename.txt or \\server\tmp\filename.txt both fail in the deplopyment machine with "The given path's is not supported" but it works fine in the development machine.
What is going on?
I'm wondering if this could be related to file.fullname somehow altering the file path string and giving an unacceptable result. Can you troubleshoot by using processThisIMFile(Path.GetFullPath(file))? Also, use messagebox.show(file.FullName) prior to processthisimfile to confirm that the result is as expected.

SPSS function spssOpenWrite returned error code SPSS_FILE_OERROR

Been using the opensource library from http://spss.codeplex.com/ to create spss files for quit sometime, but now the following error pops up on the creation of a SPSS file.
SPSS function spssOpenWrite returned error code SPSS_FILE_OERROR
public static void createSpss(Enquete eq)
{
if (File.Exists(eq.titel + ".sav"))
{
File.Delete(eq.titel + ".sav");
}
SpssDataDocument doc = SpssDataDocument.Create(eq.titel + ".sav");
createMetaData(doc, eq);
if (doc.Variables.Count != 0)
{
createData(doc, eq);
}
doc.Close();
}
Anyone got any ideas what could be the problem?
You don't have permission to write/update for this location

Save Access file in .net

How can I save an Access report in .net to the user's pc? I've tried using Access.SaveAsAXL, Docmd.OutputTo, and few other ways, but to no avail. The following is my latest attempt.
string dbname = "D:\\filename.mdb";
Microsoft.Office.Interop.Access.Application oAccess = new Microsoft.Office.Interop.Access.Application();
oAccess.OpenCurrentDatabase(dbname, true);
oAccess.Visible = true;
var acFormatXLS = "Microsoft Excel (*.xls)";
string id = "tblJobs.JobID=" + (Convert.ToInt32(GetMaxJobID())).ToString();
try
{
oAccess.Run("BuildXAxisTimeLine", Convert.ToInt32(GetMaxJobID()));
oAccess.SaveAsAXL(Microsoft.Office.Interop.Access.AcObjectType.acReport, "rptChartData", "D:\\report.xls");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//oAccess.CloseCurrentDatabase();
}
The error i receive is:
hresult e_fail has been returned from a call to a com component
any help would be greatly appreciated. Thank you.
The followig code solved my problem. place this in the try section as shown above instead of oAccess.SaveAsAxl line of code and right under the .Run line of code.
oAccess.DoCmd.OpenReport("rptChartData", Microsoft.Office.Interop.Access.AcView.acViewPreview, null,id);
oAccess.DoCmd.OutputTo(Microsoft.Office.Interop.Access.AcOutputObjectType.acOutputReport, "rptChartData", acFormatXLS, "D:\\reportname.pdf",false, null, null);

Categories

Resources