Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the filename of a Word document from C#?

I'm working on a MS Word AddIn (VSTO). The idea is that I work on a Word document and I send it somewhere.

I have all the code I need except one thing: is there a way I can get the filename of the Word document I'm working on? (assuming that is already saved)

like image 467
p4bl0 Avatar asked Apr 27 '09 08:04

p4bl0


People also ask

How do I show the FileName and path in a Word document?

Click the Insert tab and choose Field from the Quick Parts dropdown in the Text group (Figure A). In Word 2003, you'll find this setting in the AutoText options. Select FileName from the Field Names list. If you want to display the full path, check the Add Path To FileName Option (Figure B).

How do I create an automatic FileName in Word?

Go to Insert > Header or Footer. Select Edit Header or Edit Footer. Select Quick Parts, and select Field. In the Field names list, choose the field you want (such as FileName, Date, Author, or Title), choose the format you want in the Field properties section.

How do I print file names?

Select File -> Print and go to the Caption panel in the Print dialog window. Select Filename or None from the Type drop-down menu. If the Filename has been selected, press the Font button to alter the size and font.

What does ~$ mean in front of a file name?

Files that suddenly appear with a tilde are usually backups of a file that was opened or still opened. For example, with a file called myfile. doc, when it is opened in Microsoft Word, the ~$myfile. doc is created. It is a temporary backup file, used to recover data if the software crashes or stops unexpectedly.


1 Answers

this.FullName property in the code-behind .cs file should give you the entire path of the current doc.

Here's the exact query in my QuickWatch window.

((Microsoft.Office.Tools.Word.Document)(this)).FullName
like image 60
Gishu Avatar answered Oct 14 '22 20:10

Gishu