This tutorial은 .NET을 사용하여 매크로를 호출하는 방법을 보여줍니다.
는 마이크로 소프트 워드 라이브러리에 대한 참조를 추가 :
- 이 시나리오에 대한 해당 단계의 요약 (이미의 GridView 수출 관리 한 경우 이미이 설정의 일부가있을 수 있습니다)입니다 프로젝트 메뉴에서 참조 추가 클릭하십시오. (매크로를 실행 처리하는)
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
이 도우미 메서드를 추가 : COM 탭에서
- ,
Microsoft Word 10.0 Object Library
또는 Microsoft Word 11.0 Object Library
- 가 클릭
Select
이 그런 뒤에 코드에 이러한 참조를 추가 위치 :
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}
그런 다음 y (당신은 당신의 필요에 따라이를 수정해야합니다) 다음과 같은 우리의 매크로 사용 : 당신이 당신의 문서, 설정에게 이와 같은 매크로를이있는 경우
private void CallCustomHeaderFooterMacro(string filename, string pageTitle, string author)
{
Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = false;
Word.Documents oDocs = oWord.Documents;
object oFile = filename;
// Used for optional arguments
object oMissing = System.Reflection.Missing.Value;
// CHOOSE THE APPROPRIATE CALL, DEPENDING ON THE LIBRARY YOU REFERENCE
// Microsoft Word 10.0 Object Library
Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Microsoft Word 11.0 Object Library
// Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Run the 'CustomHeaderFooter' Macro.
/* Change "CustomHeaderFooter" to your macro name.
* You can send parameters to your macro such as:
* a Page title, author, date or other data you need to create the customised
* header and footer. You add and remove these as required. I have used pageTitle
* and author as an example
*/
RunMacro(oWord, new Object[]{"CustomHeaderFooter", pageTitle, author});
// Save (as required)
// Quit Word and clean up.
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc);
oDoc = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs);
oDocs = null;
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord);
oWord = null;
}
이 작동합니다.
Public Sub CustomHeaderFooter(sPageTitle As String, sAuthor As String)
' Your code to create the header and footer as required.
' The easiest way to get the macro code is to open the base document and
' and record the actions that you need such as adjusting the print margins
' and adding the custom headers and footers, then modify the resultant code
' to accept your parameters.
End Sub
당신이 부를 것이다 ASP.NET에서 (파일 경로 및 적절한 매개 변수로 조정) :
CallCustomHeaderFooterMacro(Server.MapPath("~/mydocument.docx"), "Sample Title", "John Smith");
나는이 분명하다 바랍니다. 분명히 실제 머리글과 바닥 글을 생성하기 위해 매크로 코드를 제공 할 수는 없지만 위와 같이 가장 쉬운 방법은 작업을 기록하고 생성 된 코드를 수동으로 조정하는 것입니다.
편집 : 그냥 보조 노트로. Microsoft는 서버 측 응용 프로그램 (예 : ASP.NET에서 호출)에서 Office 자동화를 사용하지 않는 것이 좋습니다. This article 여기에서 설명하고 Office 문서를 조작하기위한 대체 메커니즘을 제공합니다.이 기능은 사용자에게 유용 할 수도 있고 그렇지 않을 수도 있습니다. 하지만 프로젝트에 필요한 확장성에 따라 도움이 될 것이라고 생각했습니다.
감사합니다. 감사를 전하기 전에 먼저 구현하고 싶습니다. –
우수하고 기꺼이 도와 줬습니다. – Scott