I am using document.Active() method which is a part of Microsoft.office.interop.word namespace. I want to open the file i.e. I want to see the file opened in the Word application. I have set thetrackRevisions property true and rest of all the things.
I just want the file to open NOT IN SAVE-AS MODE. Just open so that when I open up a document from DB or from my PC drives I just want it to open.
Here is the code that I am executing:
Word.Document tempDoc = app.Documents.Open("E:\\xyz.docx");
// Activate the document so it shows up in front
tempDoc.Activate();
tempDoc.TrackRevisions = true;
foreach (Revision rev in tempDoc.Revisions)
{
string editedBy = rev.Author;
//string what = rev.Cells;
}
tempDoc.Close(ref Nothing, ref format, ref Nothing);
Any suggestions that come to your mind?
You just want word to open?
Have you tried
Process.Start('path to word plus filename');
Related
I am using Microsoft.Office.Interop.Word.Document in .net.
I am using below code to open the file.
Dim openedDoc As Microsoft.Office.Interop.Word.Document = ActivePRADocument.Application.Documents.Open(FileName:=sProvantisPrint, [ReadOnly]:=False)
It opens the file correctly but even if I dont change anything on the file it prompts to save the document when the file is closed.
In the event DocumentBeforeClose of Microsoft.Office.Interop.Word.Application it checks the Saved property of Word document as shown:
If Not closingDocument.WordDocument.Saved Then
' 120020: "Do you want to save file ‘{0}’?"
If AddInMessageBox.Show(120020, MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, closingDocument.WordDocument.Name) = DialogResult.Yes Then
closingDocument.WordDocument.Save()
Else
closingDocument.WordDocument.Saved = True
End If
End If
But even if i dont change anything on the document when i close the word this 'Saved' property is set to false. And it prompts to save the document.
Is there any way so that I can find out whether the document is actually changed or not? And prompt only when it is changed.
I have a question.
I have a winform with a richTextBox1, this textbox is readonly, but there is a "Edit"-Button. When you press the Edit-Button, Wordpad or maybe Microsoft Office will open, then you write a Text in the Office tool and after you close word/wordpad, the richTextBox1 will be filled with the Text from the Wordpad.
Is this possible? and if Yes, how ?
It is very simple:
private void btnEdit_Click(object sender, EventArgs e)
{
var myFileName = #"myRtb.rtf";
//Save your RichTextBox text to a file.
richTextBox1.SaveFile(myFileName);
string PathToApp = #"Microsoft Office Word 2007.lnk";
//Make a System.Diagnostics.Process object
Process runProg = new Process();
try
{
//With path to your MS Office application
runProg.StartInfo.FileName = PathToApp;
//Command line arguments to open file
runProg.StartInfo.Arguments = "/t" +" "+ myFileName;
runProg.StartInfo.CreateNoWindow = true;
//And start your application and also open file
runProg.Start();
}
catch (Exception ex)
{
}
}
A documentation to Microsoft Office products command line arguments:
https://support.office.com/en-us/article/Command-line-switches-for-Microsoft-Office-products-079164CD-4EF5-4178-B235-441737DEB3A6
I might have an answer.
Start up Word with a parameter for the desired file location. Then edit your file and save it. When your app detects that Word has closed or the file has been created, whichever, then you can load that word file into your textbox.
It's a little more long winded than that I'm sure, but that's the gist, totally possible.
I'd begin by looking into Aspose, a library for Microsoft products that exposes simple APIs to use.
Good luck!
I have a MS Word AddIn that launches a winform which allows the user to specify some additional metadata which is then saved as an xml file along with a copy of the document in a specified location.
This all works fine when run from a standalone Word document, however one of the areas this will be used is where a Word document is launched inside an application (EMIS WEB). It launches a copy of Word from the local machine which is fine as it allows the AddIn to be run.
When I try to save the document I get a Command Failed. error. The XML file saves no problem: xml.Save(path + docName + ".xml");.
The application prompts with it's own 'save' like dialog.
At first I thought it was the application removing focus from the document therefore this.Application.ActiveDocument.SaveAs failing because it wasn't the Active Document. So I tried getting the Document object when it was active and passing it through to the saveDoc method so that I could set it as the Active Document like so:
public void saveDoc(string doc, Word.Document wd)
{
string path = #"\\servername\folder\subfolder\";
object filename = path + doc + ".docx";
try
{
wd.Activate();
this.Application.ActiveDocument.SaveAs(ref filename);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
However this made no difference, the application's dialog still pops up, and regardless of whether I click OK or Cancel to the dialog it does not process the SaveAs command.
I have come to the conclusion that the application is intercepting the Save/SaveAs command and doing its own thing instead.
So is it possible to save a word document by bypassing the Save or SaveAs methods? Is there a way round this?
Figured it out, fortunately there was not a strict requirement to save it as a .doc or .docx so I have opted for .pdf. I have got round the application intercepting the Save commands by using the Document.ExportAsFixedFormat Method with wdExportFormatPDF to save it as a PDF.
So the final code looks like this, and it works a treat:
public void saveDoc(string doc)
{
string path = #"\\servername\folder\sub-folder\";
string filename = path + doc + ".pdf";
try
{
this.Application.ActiveDocument.ExportAsFixedFormat(filename, WdExportFormat.wdExportFormatPDF);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Based on Opening Word document within C# that has spaces in the path. I'd like to ask how to do the same but when the path is acquired from list box. I know that if I use #"path" it will work, but how do I do the same to listBox.SelectedItem.ToString() when opening
var _p = new Process();
_p.StartInfo.FileName = "Word.exe"
_p.StartInfo.Arguments = lbFiles.SelectedItem.ToString();
Let's say I want to open "C:\new word document.docx". Word gives an error can't find path "C:\new.doc" any ideas how to do it.
Try surrounding the file name in quotes as well:
_p.StartInfo.Arguments = string.Format("\"{0}\"", lbFiles.SelectedItem);
Also, Word.exe doesn't work on my system. You may need to change that to:
_p.StartInfo.FileName = "WinWord.exe"
I did a little research, and an unsure if I should be using
the Microsoft.Office.Interop.Excel
Just to clarify with the example: I'm taking a .txt, and doing stuff, then saving it as a .CSV (Comma Separated Values). I want to then open it (on button click or something...)
How can this be done?
Pair to your comment:
I want it to open in Excel.exe. It should be a separate window that launches after my program is done with its conversion
Simply start it using System.Diagnostics.Process class:
using System.Diagnostics.Process;//at top of your application
//
//At button click or after you finish editing
//
Process excel = new Process();
//if the excel was installed in the target machine and the default program to open csvs
//then you can simply just call process start and put the file path, like:
excel.Start(#"Your edited csv file path");
//otherwise:
excel.StartInfo.FileName = #"The excel application file path";
excel.StartInfo.Arguments = #"Your edited csv file path";
excel.Start();
You can start the excel process with the file path as command line parameter (excel.exe C:\myFile.csv). This will open it in excel.
Yup, Microsoft.Office.Interop.Excel is what you will need to open the CSV file in Excel.
Depends on which framework you are using (i.e. Silverlight or Windows Forms).
If I were you I'd be using OpenFileDialog to read the values from the comma seperated list into a string or a class. The sample below is for silverlight.
private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
{
// Create an instance of the open file dialog box.
var openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "CSV File (.csv)|*.csv|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
tbResults.Text = reader.ReadLine();
//the results of your CSV are now stored in tbResults.Text
//optionally you could parse the .CSV using string.Spit(',') into a string array
}
fileStream.Close();
}
}
Is this a WinForms, WPF, or ASP.NET application? If you use the Office Interop libraries you are dependent on Office being installed on that machine. If it's a web server, you don't want to run Excel that way.
Check out www.SpreadSheetGear.com. No affiliation, just very satisfied.