|
Posted on 2010-12-07 11:02 jacky_zz 阅读(406) 评论(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 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 12int _tmain(int argc, _TCHAR* argv[]) 13{ 14 if(argc != 2) 15 { 16 MessageBox(GetActiveWindow(), HINT, _T("Error"), MB_OK | MB_ICONERROR); 17 return 0; 18 } 19 20 wchar_t filename[MAX_PATH]; 21 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 29 // initialization vole 30 comstl::com_init init; 31 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 38 // get root element 39 object documentElement = xml.get_property<object>(L"documentElement"); 40 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 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 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 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 57 // starting find bookmark, insert value after bookmark if found 58 for(long i = 0; i < length; i++) 59 { 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 64 object node1 = attributes.invoke_method<object>(L"getNamedItem", L"name"); 65 object node2 = attributes.invoke_method<object>(L"getNamedItem", L"value"); 66 67 std::string value1 = node1.get_property<std::string>(L"nodeValue"); 68 std::string value2 = node2.get_property<std::string>(L"nodeValue"); 69 70 for(int j = 1; j <= count; j++) 71 { 72 object bookmark = bookmarks.invoke_method<object>(L"Item", j); 73 std::string name = bookmark.get_property<std::string>(L"Name"); 74 75 char* name1 = const_cast<char*>(name.c_str()); 76 char* name2 = const_cast<char*>(value1.c_str()); 77 78 if(strcmp(name1, name2) == 0) 79 { 80 char* value = const_cast<char*>(value2.c_str()); 81 size_t len = strlen(value); 82 83 wchar_t* w_value = (wchar_t*)malloc(len * 2 + 1); 84 MultiByteToWideChar(CP_ACP, 0, value, len, w_value, len); 85 86 object range = bookmark.get_property<object>(L"Range"); 87 range.invoke_method_v(L"InsertAfter", (LPCWSTR)w_value); 88 89 free(w_value); 90 w_value = NULL; 91 value = NULL; 92 93 break; 94 } 95 96 name1 = NULL; 97 name2 = NULL; 98 } 99 } 100 101 document.invoke_method_v(L"Save"); 102 document.invoke_method_v(L"Close", false); 103 word.invoke_method_v(L"Quit"); 104 105 system("pause"); 106 return 0; 107}
|