When I try to open an Excel document I have this error
System.Runtime.InteropServices.COMException (0x800706BE): The remote
procedure call failed.
I built an application for my company, it works fine on every computer but on my boss computer it stopped working when he the app has to open an Excel document.
I have tried to change the Permissions in Component Services but it didn't fix it.
I am working on Windows 7.
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
Excel.Range xlRange;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlSheet = null;
Excel.Workbook xlWorkbook = null;
xlWorkbook = excelApp.Workbooks.Open(filePath, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing);
Related
I want to create an Excel file, at specific location and specific name in c#.
Can we use File.Create("Filename.xlsx") for this?
This creates the file, but when we try to open it; it says problem in extension.
I have tried another way, using excelApp. But that way, it creates Excel file default at "~Documents\Sheet1.xlsx". So i can't specify location and filename in that case.
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(1);
The question is similar to - Specify Path and filename while creating excel
Since i am unable to comment, i have to ask again.
I have tried the answer provided in link.
My code:
Excel.Application excelApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(missing);
//....
Fill excelWorkbook after creating worksheets
....//
excelWorkBook.Save();
Now, as per suggestion, instead of Save, i have used SaveAs.
excelWorkBook.SaveAs(#"E:\\MyExcelBook\\abcd.xlsx",
Excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
It gives me error:
*The file could not be accessed. Try one of the following:\n\n• Make sure the specified folder exists. \n• Make sure the folder that contains the file is not read-only.\n• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or \n• Make sure the file/path name doesn't contain more than 218 characters.
Please tell me where am i doing wrong?
Just because you save an "empty" file with an extension of .xlsx doesn't mean excel is able to read such a file. You can use OpenXML, ClosedXML (my preferred), interop (yucky) and I'm sure many other methods of creating an excel document
File.Create(...) is not much different than right clicking in file explorer, choosing new text document, and renaming it from "New Text Document.txt" to "New Text Document.xlsx". Yeah that extension is excel, but excel can't read an empty file that states it's an excel file.
You can use any of the libraries available to you to create excel files:
ClosedXML
OpenXML
Interop
etc. Most "Save" methods or "SaveAs" methods within these libraries would (probably) create the file in the working directory by default, but you can specify a directory to create them in as well.
Just as the answer in the linked question shows:
workbook.SaveAs(#"c:\documents\book1.xlsx",
Excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
I am trying to insert an existing sheet from Excel into my current worksheet. But, instead to inserting into my current worksheet, it creates new worksheet. What am I missing in my code?
I am using this:
Workbook wkActive = Globals.ThisAddIn.Application.ActiveWorkbook;
objBook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
wkActive.Sheets.Add(objBook.Sheets[1], Type.Missing, Type.Missing, Type.Missing);
wkActive.Save();
wkActive.Close();
From an MVP:
I believe the Worksheet.Copy method is what you want. Assuming that you are using VSTO, the following code demonstrates one way to copy a worksheet from a non-VSTO workbook into the workbook in your VSTO customization. This particular example copies the first worksheet in a non-VSTO workbook named "MyWorkbook.xls" into a VSTO workbook, after "Sheet3".
Excel.Workbook workbook = Globals.ThisWorkbook.Application.Workbooks.Open(#"C:\MyWorkbook.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet worksheet = workbook.Sheets[1] as Excel.Worksheet;
worksheet.Copy(missing, Globals.Sheet3.InnerObject);
The code first opens a non-VSTO workbook named "MyWorkbook.xls". The code then copies the first worksheet of this workbook into the VSTO workbook by calling the Copy method of the non-VSTO worksheet, and passing in the VSTO worksheet that you want to copy the non-VSTO worksheet after.
The only somewhat tricky VSTO-specific part of this code is the fact that it passes in the InnerObject property instead of Globals.Sheet3 into the Copy method. This is because Copy expects a Microsoft.Office.Interop.Excel.Worksheet, and the VSTO worksheet (that is, Globals.Sheet3) is a Microsoft.Office.Tools.Excel.Worksheet. The InnerObject property exposes the Microsoft.Office.Interop.Excel.Worksheet that underlies the Microsoft.Office.Tools.Excel.Worksheet.
I need to automate the adding templates (.dot) in existing word document.
I can open the existing document like below
Dim ObjWord As New Microsoft.Office.Interop.Word.Application
Dim wrdoc As Microsoft.Office.Interop.Word.Document
Dim missing = Type.Missing
wrdoc = ObjWord.Documents.Open("D:\T_F\TandF_Sample.doc", missing, True, missing, missing, missing, _
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
After that i need to attach the template file ("D:\Temp.dot"). is it possible to do this?
anyone plz help me with sample code.
wrdoc.AttachedTemplate = "d:\temp.dot"
I have this code to open a WorkBook on SharePoint, it works perfectly when im on local server but when i access remotely fails to that sharepoint site EX:
(Local = Success) (MachineA to SharePoint = Fail)
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = SPContext.Current.Web)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks;
wb.Application.Visible = false;
string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
//The issue happens here
var file = wb.Open(fileToOpen.ToString(), Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
}
}
}
My environment is:
SharePoint 2010
Windows Server 2008
Interop 14
Any ideas ? i tried adding the Network Service Account to the Excel Application in dcomcnfg but anyway i use RunWithElevatedPrivilage that uses the TEST\administrator account.
Using VSTO, I've created a custom tab in the Ribbon designer and added some groups and button controls there. When user clicks one of the buttons, I'd like to connect to a SharePoint site and open a word document from it in Word (an instance is already open). I'm able to connect to the SharePoint site already and have the URLs to the documents I want to open.
But how can I actually load these documents into Word? I'm already in the code-behind in Word, so how can I target the Word instance I'm in and open a file there?
Thanks in advance.
You would have to use the Word API to open a document. See this link for a reference. You may have to update it based on the API version you use.
private void button1_Click(object sender, System.EventArgs e)
{
// Use the open file dialog to choose a word document
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
// set the file name from the open file dialog
object fileName = openFileDialog1.FileName;
object readOnly = false;
object isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
// Make word visible, so you can see what's happening
WordApp.Visible = true;
// Open the document that was chosen by the dialog
Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
// Activate the document so it shows up in front
aDoc.Activate();
// Add the copyright text and a line break
WordApp.Selection.TypeText("Copyright C# Corner");
WordApp.Selection.TypeParagraph();
}
}