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.
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);
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);
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 added a reference to Microsoft.Office.Interop.Word 12.0.0.0. I have Visual Studio 2008 and Microsoft Word 2010(Starter).
string filePath = #"C:\PP.docx";
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
// create object of missing value
object miss = System.Reflection.Missing.Value;
// create object of selected file path
object path = filePath;
// set file path mode
object readOnly = false;
// open document
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
// select whole data from active window document
docs.ActiveWindow.Selection.WholeStory();
// handover the data to cllipboard
docs.ActiveWindow.Selection.Copy();
// clipboard create reference of idataobject interface which transfer the data
System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
I get the error mentioned below:
Retrieving the COM class factory for component with CLSID
{000209FF-0000-0000-C000-000000000046} failed due to the following
error: 80040154. at Microsoft.Office.Interop.Word.Application word => new Microsoft.Office.Interop.Word.ApplicationClass();
Thanks,
Sachin K
The Office Starter 2010 edition is limited and does not support automation.
You need to get an appropriate full Office suite (e.g. the Professional edition).
For more information about the limitations see http://www.microsoft.com/oem/en/products/office/pages/office_2010_starter.aspx
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();
}
}