I need to open a Microsoft Word 2003 file and change its file properties. Such as changing the Subject in the Summary Tab.
Microsoft provides a very useful little assembly called DSOFile. With a reference to it in your project, you can modify Office document properties. It won't necessarily let you open the actual Office file's properties dialog, but you could certainly simulate it.
According to Microsoft:
The Dsofile.dll files lets you edit
Office document properties when you do
not have Office installed
More details and a download link can be found at http://support.microsoft.com/kb/224351
Here's a snippet some (very old) VB code I used ages ago. Sorry I haven't converted to C# and be aware that it's part of a class so there are references to instance variables. Still, it should be pretty easy to understand and covert to your own needs:
Private Sub ProcessOfficeDocument(ByVal fileName As String)
Dim docDSO As New DSOFile.OleDocumentPropertiesClass
Dim docTitle, docModified, docAuthor, docKeywords As String
Try
docDSO.Open(fileName, True)
Dim docSummary As DSOFile.SummaryProperties = docDSO.SummaryProperties
docTitle = docSummary.Title
docAuthor = docSummary.Author
docKeywords = docSummary.Keywords
docModified = CStr(docSummary.DateLastSaved)
If (Not String.IsNullOrEmpty(docTitle)) Then
_Title = docTitle
End If
If (Not String.IsNullOrEmpty(docAuthor)) Then
_Author = docAuthor
End If
If (Not String.IsNullOrEmpty(docModified)) Then
_DateModified = DateTime.Parse(docModified)
End If
Catch ex As Exception
'Do whatever you need to do here...'
Finally
If (Not docDSO Is Nothing) Then
docDSO.Close()
End If
End Try
End Sub
I can think of 2 ways to do this:
Use the Microsoft Office APIs. You
will have to reference them in your
project, and you will need the
Primary Interop Assemblies.
Convert the file to the Word 2003
XML format and change that value in
the XML document. Here is the MSDN
documentation on the document
properties:
http://msdn.microsoft.com/en-us/library/aa223625(office.11).aspx
I would go with the second option if you can, because that way you don't have to depend on Word being installed on the system.
Related
I want to read the binary contents of a .lnk file.
As long as the target of the shortcut (lnk file) exists this works fine with IO.File.ReadAllBytes(string file).
BUT
If the target of the shortcut does not exist (believe me I want this) the method only returns zero's. I guess this is because the OS follows the link and if it does not exist it returns zero's
Is there some way to bypass the fact that the framework follows the target of the .lnk before displaying the contents of the .lnk file?
It doesn't make a lot of sense, don't have an easy way to check it. I reckon the best approach is to read the .lnk file the way it is supposed to be read. You can use COM to do so, the ShellLinkObject class implements the IShellLink interface. Get started with Project + Add Reference, Browse tab and navigate to c:\windows\system32\shell32.dll. That generates an interop library. Write code like this:
public static string GetLnkTarget(string lnkPath) {
var shl = new Shell32.Shell(); // Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath);
var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
var lnk = (Shell32.ShellLinkObject)itm.GetLink;
return lnk.Target.Path;
}
I don't believe ReadAllBytes induces the OS to follow the lnk to its target. I suspect, the OS has already resolved the lnk file (maybe when you browsed to the file in Windows Explorer).
Turns out the file was locked because it was copied from another machine (i'm using server 2008 r2) unlocking it returned the behavoir to expected.
Stupid me.
This is a niche problem so I will try to define everything to the best of my ability. I am using Excel 2007.
Our company uses excel to produce our forms. We have many workers who use these forms. We do not want our spreadsheets to contain macros for security reasons, but we still need to use VBA to perform mass operations on data in the normal workbook. As a result, all of our macros are defined in each employee's PERSONAL.xlsb file and can be called on our normal workbook. Since we run many different macros over this workbook, it is really handy to have shortcuts to call them. The shortcuts are defined in excel as seen here:
The PERSONAL.xlsb file is special because Excel will automatically open this as a hidden background workbook whenever you open any excel file. This is great because it enables you to call user-defined macros in PERSONAL.xlsb on the current workbook, without needing the current workbook to be macro enabled.
The .xlsb is a binary extension and uses some proprietary Microsoft compression algorithm. The issue is that writing macros and VBA code is done through the VBA IDE bundled in Excel. This means that the code that you write isn't stored in plaintext, and excel is acting as a middleman that reads/writes your VBA code to a binary blob in the .xlsb file.
Since we use (and plan on adding) many macros, It would be highly advantageous to code them as plain files in an editor of my choice and use git to keep track of changes. Ideally, I want a development folder with a bunch of different .vba files to logically separate each macro. I then want a plain text configuration file that defines optional shortcut keys to each one of the .vba code modules.
Then, when I am happy with changes, I want to run a program that uses the Microsoft Excel Interop to compile this all into a nice PERSONAL.xlsb file that can be easily kept up to date and sent to other employees. I have nearly all of this implemented, but I can't figure out how to assign shortcut keys to a code module.
Here is my example code that compiles VBA files to a .xlsb file:
using Microsoft.Office.Interop.Excel;
using Microsoft.Vbe.Interop;
using System;
using System.IO;
namespace BinaryCompile
{
class Program
{
private static string path = #"C:\Users\Path\To\Source";
static void Main(string[] args)
{
var excel = new Microsoft.Office.Interop.Excel.Application();
//Console.WriteLine(path);
var workbook = excel.Workbooks.Add();
var project = workbook.VBProject;
include_files(get_vba_files(), ref project);
excel.MacroOptions();
workbook.SaveAs(path + "/PERSONAL.xlsb", XlFileFormat.xlExcel12);
workbook.Close();
Console.Read();
excel.Quit();
}
static string[] get_vba_files()
{
return Directory.GetFiles(path, "*.vba");
}
static void include_files(string[] vba_files, ref VBProject project)
{
foreach (string source in vba_files)
{
var module = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
module.CodeModule.AddFromFile(source);
module.Name = Path.GetFileNameWithoutExtension(source);
}
}
}
}
I can't find any documentation on how to assign shortcut keys to these programatically generated modules.
This is one option, if you're free to modify the vba file. If you create an macro in Excel, assign a shortcut, and then export the module, you will see the file has the following structure:
Attribute VB_Name = "MyModule"
Sub MyFunction()
Attribute MyFunction.VB_ProcData.VB_Invoke_Func = "y\n14"
'Do Your stuff
End Sub
Sub MyFunction2()
Attribute MyFunction2.VB_ProcData.VB_Invoke_Func = "q\n14"
'Do more stuff
End Sub
The first character in "y\n14" and "q\n14" will be your shortcut key to be used with Ctrl key, in these cases, Ctrl+y and Ctrl+q.
Side note: I tried your code, and I only could make it work by removing the excel.MacroOptions(); line.
I am using the most recent Aspose.PDF DLL in Visual Studio with the appropriate (in the code applied) license.
For my conversion from pdf files to pdfa types I use the following code:
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(pdfPath);
bool converted = pdf.Convert(temptext, PdfFormat.PDF_A_1A, ConvertErrorAction.None);
Now I receive the following errors, extracted from the temptext txtfile:
<Problem Severity="Error" Clause="6.8.3.3" Convertable="True">Catalog shall have struct tree root entry</Problem>
<Problem Severity="Error" Clause="6.8.2.2" Convertable="True">Catalog shall have MarkInfo entry</Problem>
Now to get a MarkInfo entry into the structure of my PDF file, I am supposed to be able to add elements to the catalog or root structure ( I am not sure exactly) which will give me the ability to create this entry tag to the logical structure of the PDF file.
Then these two errors will be avoided and the PDFa file will be converted correctly.
I noticed PDFSharp had a solution for this problem with their dll’s in the following way:
PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(pdfPath);
PdfSharp.Pdf.PdfDictionary structureTreeRoot = new PdfSharp.Pdf.PdfDictionary(doc);
structureTreeRoot.Elements["/StructElem"] = new PdfSharp.Pdf.PdfName("/Entry1");
PdfSharp.Pdf.PdfArray array = new PdfSharp.Pdf.PdfArray(doc);
doc.Internals.AddObject(structureTreeRoot);
doc.Internals.Catalog.Elements["/StructTreeRoot"] = PdfInternals.GetReference(structureTreeRoot);
I want to only use the Aspose dll. Does anyone know how I can apply this with aspose dll?
Currently, Aspose.Pdf does not support to add MarkInfo entry in logical structure of PDF. Please check the forum thread for similar question.
My name is Tilal Ahmad and I am developer evangelist at Aspose.
I'm trying to break a multi page crystal report into multiple files by page and name according from their respective fields. I've been fooling around with the code from this question however I run into all type of SAP error's. Is there not a simple way to iterate say like:
foreach(var page in CrystalReport)
{
report.ExportTiDisk(ExportFormatType.WordForWindows, page.[NameField]);
}
Worst case I could do this with the Word API but that another can of worms I'd rather not open.
Thank you in advance
Dear please check the code.
Why you need to export in multiple files, it is bad as suppose you have a report with 300 pages result, then are you think to make 300 files to export and customer to check each & every files. Think again.
Still here is solution, Please refer to the following VB.Net code for exporting to seperate pdf files.
Dim rdoc As New ReportDocument
'------------------------------------
'Add your code to set rdoc object
'--------------------------------------
Dim exportOpts As ExportOptions = New ExportOptions()
Dim pdfRtfWordOpts As PdfRtfWordFormatOptions = ExportOptions.CreatePdfRtfWordFormatOptions()
Dim destinationOpts As DiskFileDestinationOptions = ExportOptions.CreateDiskFileDestinationOptions()
For li_count As Integer = 1 To pagecount
pdfRtfWordOpts.FirstPageNumber = li_count
pdfRtfWordOpts.LastPageNumber = li_count
pdfRtfWordOpts.UsePageRange = True
exportOpts.ExportFormatOptions = pdfRtfWordOpts
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
destinationOpts.DiskFileName = "D:\report File" & li_count & ".pdf"
exportOpts.ExportDestinationOptions = destinationOpts
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
rdoc.Export(exportOpts)
Next
refer link export in multiple file
You have not written your CR-version, So please refer to this link also which says, not able to export in multiple files in cr-2008.
http://social.msdn.microsoft.com/Forums/en-US/f85e167d-edb3-44d0-82fc-2d2b6f92f57b/how-do-i-export-multiple-pdf-files-from-a-single-crystal-report-ie-a-pdf-file-for-each?forum=vscrystalreports
http://scn.sap.com/thread/1132776
The process of splitting a report in multiple files is known as bursting. You can split by group , not by page. However splitting by group will allow you to split based on the data , not by mechanical reason like page number. As a result if you have a customer with more data which is printed on 2 pages the report will be spitted correctly and the generated file for this customer will be 2 pages too. Bursting is a little bit complicate to develop but there are few tools on the market that can do it. Check this video :
http://www.r-tag.com/Pages/Preview_Bursting.aspx
I believe the tool in this video is free.
I have opened Excel file in my C# WinForm Application adding reference to Microsoft.Office.Interop.Excel.dll and using DSO FRAMER CONTROL. But i want to open my excel file with read only protection.I have successfully done this for WORD Application like this
Word.Document wordDoc = (Word.Document)axFramerControl1.ActiveDocument;
Word.Application wordApp = wordDoc.Application;
wordDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading);
In the same i want to do this work for Excel.But i couldn't able to protect Excel file on that way.
string path = "C:\\test-wb.xlsx";
axFramerControl1.Open(path, true,"excel.sheet", "", "");
Excel._Workbook excelDoc =(Microsoft.Office.Interop.Excel._Workbook)axFramerControl1.ActiveDocument;
Excel.Application excelApp =excelDoc.Application;
//What code should i write to protect Excel Workbook with read - only.
excelDoc.Protect(misval, true, misval);//It is not working.
Call theOpen method with third parameter (ReadOnly) = true.
See MSDN documentation :
ReadOnly
Optional Object. True to open the workbook in read-only mode.
The WorkBook class has a Protect method, similar (but not identical) to the one supported by Word. I can't find the COM/interop documentation, but the VSTO documentation covers the same ground, and the method signatures are the same:
Protect
Protects a workbook so that it cannot be modified.
public virtual void Protect (
[OptionalAttribute] Object Password,
[OptionalAttribute] Object Structure,
[OptionalAttribute] Object Windows
)
(This all assumes that what you wanted to achieve was to protect the document, since that's what the Word code does, as opposed to opening the document read only, which is what your narrative seems to be saying, in which case, #gdoron's answer is more suitable