|
한민우 님이 쓰신 글 :
: 혹시 빌더에서 엑셀의 매크로 함수를 직접 호출할수 있나요
: 아니면 빌더안에서 엑셀 매크로 함수를 코딩할수 있나요..
:
: 제가 할려는것...빌더에서 버튼 누르면 데이터가 엑셀로 포워딩 되고
: 엑셀의 매크로 함수를 호출해서 엑셀에서 그래프 그릴려고 합니다.
:
: 내장함수는 호출 하수 있는걸로 알고 있는대...
: 제가 직접 코딩한 엑셀 매크로 함수는 호출 할수 있는지..
: 아니면..다른 방법으로 가능한지 조언 좀 부탁 드립니다.
아래는 MS Word 문서에 외부 프로그램에서 매크로를 추가하고 실행하는 예제입니다.
[도구] [옵션] [매크로보안...] [신뢰할 수 있는 원본] 메뉴의
"Visual Basic 프로젝트에 안전하게 액세스할 수 있음" 이 체크되어 있어야 매크로를 외부에서 추가하거나 실행할 수 있습니다.
Variant wordApp, wordDocuments, wordDocument,wordNewLine, wordLine, wordModule, wordSec;
wordApp = Variant::CreateObject("Word.Application");
wordDocuments = wordApp.OlePropertyGet("Documents");
wordSec = wordApp.OlePropertyGet("AutomationSecurity");
// wordApp.OlePropertySet("AutomationSecurity",msoAutomationSecurityForceDisable);
wordApp.OlePropertySet("AutomationSecurity",msoAutomationSecurityLow);
WideString strMacro="sub TestFunc\r\n"
"ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _\r\n"
"5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _\r\n"
"wdAutoFitFixed\r\n"
"With Selection.Tables(1)\r\n"
"If .Style <> \"표 구분선\" Then\r\n"
".Style = \"표 구분선\"\r\n"
"End If\r\n"
".ApplyStyleHeadingRows = True\r\n"
".ApplyStyleLastRow = True\r\n"
".ApplyStyleFirstColumn = True\r\n"
".ApplyStyleLastColumn = True\r\n"
"End With\r\n"
"end sub\r\n"
;
// wordDocument = wordDocuments.OleFunction("Open","??");
wordDocument = wordDocuments.OleFunction("Add",Variant::NoParam(),Variant::NoParam(),wdNewBlankDocument,Variant::NoParam());
wordModule = wordDocument.OlePropertyGet("VBProject").OlePropertyGet("VBComponents").OleFunction("Add",vbext_ct_StdModule);
wordModule.OlePropertySet("Name", "MyTestModule");
wordModule.OlePropertyGet("CodeModule").OleFunction("AddFromString",strMacro); //.AddFromFile(file_name)
wordApp.OleFunction("Run","TestFunc");
wordDocument.OleFunction("SaveAs","TestWordDoc1.doc",wdFormatDocument,false);
wordDocument.OleFunction("Close");
wordApp.OlePropertySet("AutomationSecurity",wordSec);
wordDocument = Unassigned;
wordDocuments=Unassigned;
wordApp=Unassigned;
|