2017-08-08 53 views
0

我有下面的代碼刪除最後作者和Word的版本號文件如何刪除最後作者和Word的版本號文件

using Microsoft.Office.Core; 
using Word = Microsoft.Office.Interop.Word; 
using System.Reflection; 
using System.IO; 
... 


Word.Application oWord; 
Word._Document oDoc; 

oWord = new Word.Application(); 
oWord.Visible = false; 

List<string> lstDocFile = new List<string>(); 
//Add doc files here 
List<string> g_lstCheck = new List<string>(); 
//Add list check here "Last Author" and "Revision Number" 

foreach (string path in lstDocFile) 
{ 
    oDoc = oWord.Documents.Open(path, ReadOnly: false); 
    foreach (string chkItem in g_lstCheck) 
    { 
     strValue = oDoc.BuiltInDocumentProperties[chkItem].Value; 
     if (!string.IsNullOrEmpty(strValue)) 
     { 
      oDoc.BuiltInDocumentProperties[chkItem].Value = string.Empty); 
     } 
    } 
    oDoc.Close(Word.WdSaveOptions.wdSaveChanges); 
} 
oWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges); 

運行的代碼後,我希望最後的作者和版本號來爲空字符串。但結果卻是最後作者變成了我和版本號增加1。我明白髮生,因爲我用下面的代碼保存Word文檔

oDoc.Close(Word.WdSaveOptions.wdSaveChanges); 

請幫我刪除最後一個作者和版本號爲C#。

回答

0

*根據this article,作者Mr.Vivek Singh爲我們提供了一些有用的代碼。

**另外我們有微軟的this library -Dsofile.dll

這樣走吧。

第1步:下載Dsofile.dll庫(**),提取和獲取文件Interop.Dsofile.dll(檢索日期2017年8月8日)

第2步:添加引用文件的互操作。 Dsofile.dll爲您的C#項目。

3步:使用此代碼(從第一條編輯* - 由於維韋克·辛格,我只是刪除單詞類在OleDocumentPropertiesClass防止生成錯誤,並編輯了一下,解決這個問題)

 string fileName = "";//Add the full path of the Word file 

     OleDocumentProperties myDSOOleDocument = new OleDocumentProperties(); 
     myDSOOleDocument.Open(fileName, false, 
DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess); 

     myDSOOleDocument.SummaryProperties.LastSavedBy = string.Empty; 
     //myDSOOleDocument.SummaryProperties.RevisionNumber = string.Empty; //This can't be edit -readonly 

     myDSOOleDocument.Save(); 
     myDSOOleDocument.Close(); 

無論如何,我無法編輯RevisionNumber因爲它是隻讀的。好吧,我只能對我能得到的東西感到滿意。

+0

我指的這個解決方案比OpenXML的,因爲它更簡單刪除這兩個新老文檔和Excel類型 – 123iamking

1

對於.docx(Open Xml)文件,最簡單的方法是使用官方Open XML SDK nuget package。有了這一點,很容易操作文檔屬性:

// open for read write 
using (var package = WordprocessingDocument.Open("myfile.docx", true)) 
{ 
    // modify properties 
    package.PackageProperties.Creator = null; 
    package.PackageProperties.LastModifiedBy = null; 
    package.PackageProperties.Revision = null; 
} 

對於.DOC(字.97-> 2003)的文件,這裏是一個小的C#方法,將能夠去除屬性(在技術上存儲completely differently):

RemoveProperties("myfile.doc", SummaryInformationFormatId, PIDSI_AUTHOR, PIDSI_REVNUMBER, PIDSI_LASTAUTHOR); 

... 

public static void RemoveProperties(string filePath, Guid propertySet, params int[] ids) 
{ 
    if (filePath == null) 
     throw new ArgumentNullException(nameof(filePath)); 

    if (ids == null || ids.Length == 0) 
     return; 

    int hr = StgOpenStorageEx(filePath, STGM.STGM_DIRECT_SWMR | STGM.STGM_READWRITE | STGM.STGM_SHARE_DENY_WRITE, STGFMT.STGFMT_ANY, 0, IntPtr.Zero, IntPtr.Zero, typeof(IPropertySetStorage).GUID, out IPropertySetStorage setStorage); 
    if (hr != 0) 
     throw new Win32Exception(hr); 

    try 
    { 
     hr = setStorage.Open(propertySet, STGM.STGM_READWRITE | STGM.STGM_SHARE_EXCLUSIVE, out IPropertyStorage storage); 
     if (hr != 0) 
     { 
      const int STG_E_FILENOTFOUND = unchecked((int)0x80030002); 
      if (hr == STG_E_FILENOTFOUND) 
       return; 

      throw new Win32Exception(hr); 
     } 

     var props = new List<PROPSPEC>(); 
     foreach (int id in ids) 
     { 
      var prop = new PROPSPEC(); 
      prop.ulKind = PRSPEC.PRSPEC_PROPID; 
      prop.union.propid = id; 
      props.Add(prop); 
     } 
     storage.DeleteMultiple(props.Count, props.ToArray()); 
     storage.Commit(0); 
    } 
    finally 
    { 
     Marshal.ReleaseComObject(setStorage); 
    } 
} 

// "The Summary Information Property Set" 
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa380376.aspx 
public static readonly Guid SummaryInformationFormatId = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9"); 
public const int PIDSI_AUTHOR = 4; 
public const int PIDSI_LASTAUTHOR = 8; 
public const int PIDSI_REVNUMBER = 9; 

[Flags] 
private enum STGM 
{ 
    STGM_READ = 0x00000000, 
    STGM_READWRITE = 0x00000002, 
    STGM_SHARE_DENY_NONE = 0x00000040, 
    STGM_SHARE_DENY_WRITE = 0x00000020, 
    STGM_SHARE_EXCLUSIVE = 0x00000010, 
    STGM_DIRECT_SWMR = 0x00400000 
} 

private enum STGFMT 
{ 
    STGFMT_STORAGE = 0, 
    STGFMT_FILE = 3, 
    STGFMT_ANY = 4, 
    STGFMT_DOCFILE = 5 
} 

[StructLayout(LayoutKind.Sequential)] 
private struct PROPSPEC 
{ 
    public PRSPEC ulKind; 
    public PROPSPECunion union; 
} 

[StructLayout(LayoutKind.Explicit)] 
private struct PROPSPECunion 
{ 
    [FieldOffset(0)] 
    public int propid; 
    [FieldOffset(0)] 
    public IntPtr lpwstr; 
} 

private enum PRSPEC 
{ 
    PRSPEC_LPWSTR = 0, 
    PRSPEC_PROPID = 1 
} 

[DllImport("ole32.dll")] 
private static extern int StgOpenStorageEx([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, STGM grfMode, STGFMT stgfmt, int grfAttrs, IntPtr pStgOptions, IntPtr reserved2, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IPropertySetStorage ppObjectOpen); 

[Guid("0000013A-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] 
private interface IPropertySetStorage 
{ 
    void Unused1(); 
    [PreserveSig] 
    int Open([MarshalAs(UnmanagedType.LPStruct)] Guid rfmtid, STGM grfMode, out IPropertyStorage storage); 
} 

[Guid("00000138-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] 
private interface IPropertyStorage 
{ 
    void Unused1(); 
    void Unused2(); 
    void DeleteMultiple(int cpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPSPEC[] rgpspec); 
    void Unused4(); 
    void Unused5(); 
    void Unused6(); 
    void Commit(uint grfCommitFlags); 
    // rest ommited 
} 
+0

非常感謝你,我看到Dsofile.dll EULA說:「用戶應承擔全部風險「,」使用...自負風險「,...。所以我使用Dsofile.dll時有點擔心。所以我想問,Open XML SDK Nuget包是否比Dsofile.dll更安全。 – 123iamking

+0

@ 123iamking - 是的,這是一個官方的開源Microsoft包:https://github.com/OfficeDev/Open-XML-SDK –

+0

有一點需要注意的是必須添加WindowsBase.dll來修復構建錯誤:https:// stackoverflow.com/a/7814593/4608491 – 123iamking

相關問題