|
Posted on 2010-12-07 11:02 jacky_zz 阅读(399) 评论(0) 编辑 收藏 引用 所属分类: C/C++
stdafx.h
1 #pragma once 2 3 #include "targetver.h" 4 5 #include <stdio.h> 6 #include <tchar.h> 7 #include <windows.h> 8 #include <string> 9 10 #include <vole/vole.hpp> 11 #include <comstl/util/initialisers.hpp> 12 13 using vole::object; 14 using vole::collection;
vword.cpp
1 #include "stdafx.h" 2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 3 #define HINT _T("please provide a xml file first.\n") \ 4 _T("format is:\n") \ 5 _T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") \ 6 _T("<bookmarks>\n") \ 7 _T("\t<bookmark name=\"\" value=\"\"/>\n") \ 8 _T("\t \n") \ 9 _T("\t<bookmark name=\"\" value=\"\"/>\n") \ 10 _T("</bookmarks>") \ 11![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 12 int _tmain(int argc, _TCHAR* argv[]) 13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 14 if(argc != 2) 15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 16 MessageBox(GetActiveWindow(), HINT, _T("Error"), MB_OK | MB_ICONERROR); 17 return 0; 18 } 19![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 20 wchar_t filename[MAX_PATH]; 21![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 22 #ifdef _UNICODE 23 // wcscpy_s(filename, MAX_PATH, _T("./1.xml")); 24 wcscpy_s(filename, MAX_PATH, argv[1]); 25 #else 26 MultiByteToWideChar(CP_ACP, 0, argv[1], strlen(argv[1]), filename, MAX_PATH); 27 #endif 28![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 29 // initialization vole 30 comstl::com_init init; 31![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 32 // create xml DOM object 33 object xml = object::create("Msxml2.DOMDocument"); 34 bool success = xml.invoke_method<bool>(L"load", filename); 35 if(!success) 36 return 0; 37![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 38 // get root element 39 object documentElement = xml.get_property<object>(L"documentElement"); 40![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 41 // get child nodes of root element 42 object childNodes = documentElement.get_property<object>(L"childNodes"); 43 long length = childNodes.get_property<long>(L"length"); 44![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 45 // create word automation object 46 object word = object::create("Word.Application", CLSCTX_LOCAL_SERVER, vole::coercion_level::naturalPromotion); 47 word.put_property(L"Visible", false); 48![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 49 // open local word file 50 collection documents = word.get_property<collection>(L"Documents"); 51 object document = documents.invoke_method<object>(L"Open", L"C:\\1.doc"); 52![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 53 // retrieve bookmarks in opened document 54 collection bookmarks = document.get_property<collection>(L"Bookmarks"); 55 int count = bookmarks.get_property<int>(L"Count"); 56![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 57 // starting find bookmark, insert value after bookmark if found 58 for(long i = 0; i < length; i++) 59![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 60 object node = childNodes.get_property<object>(L"item", i); 61 object attributes = node.get_property<object>(L"attributes"); 62 long attribute_length = attributes.get_property<long>(L"length"); 63![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 64 object node1 = attributes.invoke_method<object>(L"getNamedItem", L"name"); 65 object node2 = attributes.invoke_method<object>(L"getNamedItem", L"value"); 66![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 67 std::string value1 = node1.get_property<std::string>(L"nodeValue"); 68 std::string value2 = node2.get_property<std::string>(L"nodeValue"); 69![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 70 for(int j = 1; j <= count; j++) 71![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 72 object bookmark = bookmarks.invoke_method<object>(L"Item", j); 73 std::string name = bookmark.get_property<std::string>(L"Name"); 74![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 75 char* name1 = const_cast<char*>(name.c_str()); 76 char* name2 = const_cast<char*>(value1.c_str()); 77![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 78 if(strcmp(name1, name2) == 0) 79![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 80 char* value = const_cast<char*>(value2.c_str()); 81 size_t len = strlen(value); 82![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 83 wchar_t* w_value = (wchar_t*)malloc(len * 2 + 1); 84 MultiByteToWideChar(CP_ACP, 0, value, len, w_value, len); 85![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 86 object range = bookmark.get_property<object>(L"Range"); 87 range.invoke_method_v(L"InsertAfter", (LPCWSTR)w_value); 88![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 89 free(w_value); 90 w_value = NULL; 91 value = NULL; 92![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 93 break; 94 } 95![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 96 name1 = NULL; 97 name2 = NULL; 98 } 99 } 100![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 101 document.invoke_method_v(L"Save"); 102 document.invoke_method_v(L"Close", false); 103 word.invoke_method_v(L"Quit"); 104![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 105 system("pause"); 106 return 0; 107 }
|