2012-07-27 2 views
0

단어 .doc (단어 97 - 2003)에서 프로그래밍 방식으로 기존 사용자 지정 속성을 업데이트하려고합니다. 처음에는 Aspose로 해결했지만 제한된 라이센스로 인해이 프로젝트에는 사용할 수 없습니다.기존 사용자 지정 속성 업데이트 Word doc

이 코드는 excel Accessing Excel Custom Document Properties programatically 대신에 단어를 약간 수정하여 여기에서 도매로 가져옵니다.

첫 번째 메서드는 사용자 지정 속성이 없으면 추가하고 두 번째 메서드는 사용자 지정 속성을 가져올 수 있습니다. 이미 존재하는 속성을 업데이트하는 방법을 알지 못했습니다. InvokeMember()에서 동사와 관련이있을 수 있지만 많은 문서를 찾을 수 없다고 생각합니다.

public void SetDocumentProperty(string propertyName, string propertyValue) 
    { 
     object oDocCustomProps = oWordDoc.CustomDocumentProperties; 
     Type typeDocCustomProps = oDocCustomProps.GetType(); 

     object[] oArgs = {propertyName,false, 
          MsoDocProperties.msoPropertyTypeString, 
          propertyValue}; 

     typeDocCustomProps.InvokeMember("Add", 
             BindingFlags.Default | BindingFlags.InvokeMethod, 
             null, 
             oDocCustomProps, 
             oArgs); 

    } 

    public object GetDocumentProperty(string propertyName, MsoDocProperties type) 
    { 
     object returnVal = null; 

     object oDocCustomProps = oWordDoc.CustomDocumentProperties; 
     Type typeDocCustomProps = oDocCustomProps.GetType(); 

     object returned = typeDocCustomProps.InvokeMember("Item", 
                  BindingFlags.Default | 
                  BindingFlags.GetProperty, null, 
                  oDocCustomProps, new object[] { propertyName }); 

     Type typeDocAuthorProp = returned.GetType(); 
     returnVal = typeDocAuthorProp.InvokeMember("Value", 
                BindingFlags.Default | 
                BindingFlags.GetProperty, 
                null, returned, 
                new object[] { }).ToString(); 

     return returnVal; 
    } 
+0

http://docx.codeplex.com/ –

+0

이 라이브러리가 좋아 보인다하지만 난 – Curtis

답변

0

일반적으로 모든 소품을 목록으로 검색하고 문서에서 삭제하고, 값을 변경하고 다시 삽입합니다. DSOFile.dll 접근 방식을 사용합니다.

+0

어떻게 당신은 모든 사용자 지정 속성을 삭제합니까 .doc 파일이 될 수있는 요구 사항이 있습니까? – baru

1

정답을 찾았는지 모르겠지만이 페이지를 방문한 사용자는 기존 사용자 지정 문서 속성을 설정하려고합니다. BindingFlags.GetProperty을 사용하여 먼저 문서 속성을 검색 한 다음 BindingFlags.SetProperty을 사용하여 검색된 특정 항목의 값을 설정해야합니다.

사용자 지정 검사를 추가하여 설정할 개체가 유효한지 확인할 수도 있습니다.

public void SetDocumentProperty(string propertyName, object value) 
    { 
     // Get all the custom properties 
     object customProperties = wordDocument.CustomDocumentProperties; 
     Type customPropertiesType = customProperties.GetType(); 

     // Retrieve the specific custom property item 
     object customPropertyItem = customPropertiesType.InvokeMember("Item", 
      BindingFlags.Default | BindingFlags.GetProperty, null, customProperties, 
      new object[] { propertyName }); 
     Type propertyNameType = customPropertyItem.GetType(); 

     // Set the value of the specific custom property item 
     propertyNameType.InvokeMember("Value", BindingFlags.Default | BindingFlags.SetProperty, null, 
      customPropertyItem, new object[] { value }); 
    }