Create, insert text and save a Word doc in C# - c#

I've found loads of useful documentation around creating an instance of a word doc, inserting all manner of text and formatting but cannot find anywhere something to save a document that hasnt already been created and opened programmatically.
Essentially I want to create a docx file and fill it with text from a rich text box. Using code Ive found at How to Insert text in the end of the document I am able to achieve this if I first create a document. But despite suggestions of using _document.SaveAs() (which doesnt exist - version diff i guess) or .Save() supposedly prompting with a SaveAs dialogue if the file doesnt already exist, I always get a type mismatch error. So this is the working code if i pre-create the file to use:
OpenFileDialog SDO = new OpenFileDialog();
SDO.ShowDialog();
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
oWord.Documents.Open(SDO.FileName);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
Now one would assume that removing the lines for the OpenFileDialogue Documents.Open would go some way to saving a new file created in C#, however even with:
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
SaveFileDialog SD = new SaveFileDialog();
SD.Filter = "Word File |*.docx";
SD.Title = "Save File";
SD.ShowDialog();
oWord.Documents.Save(SD.FileName,WdNewDocumentType.wdNewXMLDocument);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
Other examples ive seen open the document so that you can save it yourself but i need it saving without any human intervention other than choosing a filename.
Any help appreciated, also the option of third party dlls like spire and gem are precluded so not an option :(
If anyone has a simple example of creating and saving a word doc that didnt exist before the program ran id be much obliged.

The Microsoft MSDN documentation has tons of useful guides and examples.
You are going to want to include:
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
Then declare your application:
Word.Application app = new Word.Application();
Declare your new document:
Word.Document doc = app.Documents.Add();
Add text to your documnet
There are two ways to save these documents:
Programmatically
Using a save file dialog box

This is the way I do it.
app = new Word.Application();
object oMissing = System.Reflection.Missing.Value;
Word._Document oDoc = app.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
.....
app.ActiveDocument.SaveAs2(fileName);
Where filename is my desired file name. When I was originally doing this, I discovered that there were a lot of undocumented (and therefore unsupported) functions. SaveAs2 is one of them! But it does work.

Related

In word 365 where can I find the template variables to place them on the page?

We have an existing legacy system where I'm able to download existing templates, update them and save them as documents with my edits. Here is a portion of the code.
#region Open existing Template and write something to it.
object missing = Type.Missing;
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open(#"C:\\test395310.dot", ref missing, true);
doc.Activate();
doc.Variables["CASE PLAN_PLAN_STATUS"].Value = "asdf1";
doc.Variables["CASE PLAN_PROGRAM"].Value = "asdf2";
doc.Fields.Update();
#endregion
Using similar logic I'm attempting to create a new word template with 2 variables.
private static void CreateTemplate()
{
//have it open word and create a template with fields from gen
object missing = Type.Missing;
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Add(ref missing, ref missing, true);
doc.Activate();
doc.Variables.Add("CASE PLAN_PLAN_STATUS", "XXXXX");
doc.Variables.Add("ANOTHER ONE", "XXXX");
doc.Fields.Update();
doc.SaveAs2(#"C:\\newTemplate.dot");
doc.Close();
app.Quit();
}
The document is getting created, however in word I cannot find either variable. My goal is to open word and placement in the template with formatting. ie
Plan Status: <<CASE PLAN_PLAN_STATUS>>
If I click insert/Explore Quick Parts/field, I can see various options, but my two variables aren't there. Anyone know where else to look? Or a better approach?
Within your template, you can use DocVariable fields.
https://support.microsoft.com/en-us/office/field-codes-docvariable-field-32a81e22-c5c1-4b16-8097-f0de851db67c?ui=en-US&rs=en-US&ad=US
It would look like { DocVariable Case_Plan_Plan_Status } with field codes revealed.
Unlike Document Properties, Document Variables can only be created, modified, or deleted using vba.
There are no built-in Document Variables. There is no tool built into the user interface for seeing what is available among variables.
I do know of several good Add-Ins for manipulating them in the user interface.
One is Chris Woodman's old Keyboard Shortcut Organizer.
http://addbalance.com/word/download.htm#ChrisWoodman
This one lets you view, create, edit and delete variables from within Word but does not create a DocVariable field, leaving that for you to do.
Another is Graham Mayor's or Greg Maxey's utility.
http://gregmaxey.com/word_tip_pages/cc_var_bm_doc_prop_tools_addin.html
or
http://www.gmayor.com/BookmarkandVariableEditor.htm
Both of these last two can add a DocVariable Field to your document after you select the variable from a list.

C#: print out Word document without saving it in advance document.PrintOut()

I want to print out a Word document without saving it in advance. Is this possible?
//I created an instance for word app
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
//I created a Word document (including pararaphs and tables):
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
//I can print the document, if I save it before. But I want to print it without saving the word document.
document.SaveAs2(#"C:\User\\Desktop\Test");
document.PrintOut()
//Export of the document as pdf-file.
document.ExportAsFixedFormat(label24.Text + "Document" + textBox13.Text, WdExportFormat.wdExportFormatPDF, true);
If the problem is that the document is closing before the print job has completed, then the best approach is to turn off background printing, at least for the duration of code execution.
winword.Options.PrintBackground = false;
Background printing was introduced in order to allow the user to continue working while a print job was processing. This is fine for the user, but a problem for code such as that in the question.

How to copy specific pages of Word document

I have a word document which contains multiple pages and i want to copy some pages into new word document using OpenXml SDK. I did some web search and got below code which reads entire document and copies into new one
string documentURL = filelocation;
byte[] docAsArray = File.ReadAllBytes(documentURL);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(docAsArray, 0, docAsArray.Length); // THIS performs doc copy
using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true))
{
// perform content control substitution here, making sure to call .Save()
// on any documents Part's changed.
}
File.WriteAllBytes(outputSplitDocpath, stream.ToArray());
}
Now, in the above code how can i read just specific pages and copy into new one? Please help with suggestions. Thanks
Unless a manual page break has been used to generate every page in the document, what you want to do is not possible.
Automatic page breaks are generated by Word, at run-time, when the document is open in the Word application. The actual placement of a page break is completely dynamic, based on the editing being done and is recalculated "all the time" during editing.
This information is not reliably saved in the document when the document is closed. One reason for this is because the document could lay out differently when opened on a different machine, or when a different printer (driver) is selected.
So it's not possible to work with individual pages using the Office Open XML file format unless there's some way each page can be recognized, such as a manual page break.
Use Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
instead of OpenXML
//dummy value to satisfy params
object oMissing = System.Reflection.Missing.Value;
//copy specific page/s
object what = WdGoToItem.wdGoToPage;
object which = WdGoToDirection.wdGoToFirst;
object count1 = 1;
Range startRange = word.Selection.GoTo(ref what, ref which, ref count1, ref oMissing);
object count2 = (int)count + 1;
Range endRange = word.Selection.GoTo(ref what, ref which, ref count2, ref oMissing);
endRange.SetRange(startRange.Start, endRange.End - 1);
endRange.Select();
word.Selection.Copy();
//save...

How to embed file to word docx?

I'm trying to insert a file programmatically (*.zip for example) into an existing docx file.
I looked at the docx open library but it doesn't have the function there.
Also tried using Microsoft.Office.Interop.Word. I created a word document with a table, and I'm trying to insert a file into a cell inside the table.
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
Word.Document doc = new Word.Document();
doc = wordApp.Documents.Open(Environment.CurrentDirectory + "\\test.docx");
doc.Tables[1].Rows[2].Cells[1].Range.InsertFile((Environment.CurrentDirectory + "\\tttt.zip"));
but it caused an error:
"The file appears to be corrupted"
Can anyone have experience and help with this?
After a lot of trial and error...
The function "Range.InsertFile" doesn't actually inserts a file, it reads and appends the text into the Range.
The solution was simple - Copy paste...
using System.Collections.Specialized;
...
...
//Copy the Filename to Clipboard (setFile function uses StringCollection)
StringCollection collection = new StringCollection();
collection.Add(Environment.CurrentDirectory + "\\MyFile.zip");
Clipboard.SetFileDropList(collection);
//Paste into the selected Range.
range.Paste();
*There is also function "PasteSpecial" which didn't work (Only specific data types are supported).

Generate a Word Document from StringBuilder

I have to make some Word Integration as part of an exam project. My problem is that I am rather new to Microsoft Word integration in C#. I have the Assemblies I need, and I got everything set-up ready to write code pretty much. The document will be generated from scratch.
But I am just starring at the blinking cursor, not really knowing how to start.
I have to take a StringBuilder (which should hold stuff like escape characters for new lines, perhaps italic bold, etc kind of formatting as well.) The StringBuilder will be given from another part of the application written by my friend.
Would you suggest that this is delivered in another form than a StringBuilder Object?
And where should I start with all this? It's a bit overwhelming.
Thanks
Try this
you need to add Microsoft.Office.Interop.Word reference
Word._Application oWord;
Word._Document oDoc;
object oMissing = Type.Missing;
oWord = new Word.Application();
oWord.Visible = true;
//Add Blank sheet to Word App
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWord.Selection.TypeText("Write your text here");
//FOrmatting your text
oWord.Selection.Font.Size = 8;
oWord.Selection.Font.Name = "Zurich BT";
oWord.Selection.Font.Italic = 1
oWord.Selection.Font.Bold = 1
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = //Havent tested the
//header and footer part
Word.WdSeekView.wdSeekCurrentPageFooter;
oDoc.Content.Application.Selection.TypeText(“Martens”);
I guess this might be what you are looking for

Categories