Luacom open the Word file in the Chinese path

Source: Internet
Author: User
Background

Luacom is a very powerful module that allows us to apply various COM components, such as Word. However, there is a problem that the Chinese file name is not recognized. Why? Because the COM is Unicode, luacom requires that all inputs be UTF-8, And the luacom output is also UTF-8. Can this be swollen?

Iconv

GNU has a libiconv library. If there is one, we will not be afraid of it! Search on luaforge and find LUA-iconv. Install it!

Luarocks install LUA-iconv sorry, an error occurred! There are two reasons for the error:

    • We have not installed the libiconv library.
    • Lua-iconv does not provide Cl compilation methods
Compile LUA-iconv by yourself
    1. Download the compiled Windows Version
      Libiconv
    2. Download
      Lua-iconvSource code
    3. Decompress the libiconv-1.9.1.bin.woe32.zip file and add the include directory toInclude filesDirectory to add the lib directory toLibrary filesDirectory (see the previous article)
    4. Decompress the LUA-iconv SourceCode
    5. Create an empty Win32 DLL project in vs2008 (see the previous article), name it luaiconv, and add the luaiconv. c file to the project.
    6. Modify source code: int luaopen_iconv (lua_state * l)-> _ declspec (dllexport) int luaopen_luaiconv (lua_state * l)
      • Note: The generated DLL file name must be the same as luaiconv in luaopen_luaiconv (see the previous article)
    7. Project properties-> linker-> input-> additional library file: lua51.lib iconv. Lib charset. Lib
    8. Compile and generate the luaiconv. dll file for release
    9. Copy the luaiconv.dllfile to the clibs directory of luaforwindows together with the iconv. dll file extracted by libiconv-1.9.1.bin.woe32.zip (you know why I want to change the luaopen_iconv function name ).
Test

Require "luacom" require "luaiconv" function createiconv (to, from) Local Cd = iconv. new (to, from) return function (txt) return CD: iconv (txt) endendl = createiconv ("UTF-8", "GBK") -- Note: running this file will modify C: \ export word.docx file, please note that backup wordapp = assert (luacom. createobject ("word. application ") wordapp. visible = trueworddocpath = l "C: \ export word.docx" if not pcall (function () worddoc = wordapp. documents: open (worddocpath) End) thenworddoc = wordapp. documents: add () endwordapp. selection: typetext (L "word") worddoc: saveas2 (worddocpath, wdformatdocument)

The above test code will create the c: \ mongoword.docx file during the first run. The file will be opened later, and the "Hello word" text will be entered each time you run it. If you are using Word 2003, change docx to Doc.

References

Http://hi.baidu.com/nivrrex/blog/item/17c231adad9e8a0f4b36d6ca.html

This eldest brother wrote a Conversion Function Using VC functions, but it was not encapsulated into a library. In addition, I think it is not concise enough. It is better to use the iconv library and it is not prone to errors.

Http://www.cppblog.com/darkdestiny/archive/2009/04/25/81055.html

This eldest brother realized the conversion with iconv and did not encapsulate it into a library. My "L" function is also borrowed from him. Thank you very much! However, I think it has two weaknesses compared to mine:

1. Each call to the L function must go through the iconv open, conversion, and Close processes. However, I only need to open one form of conversion (LUA-iconv implements ^_^)

2. If you want to implement reverse conversion, that is, UTF-8 to GBK, You have to modify the module, and I will not use it here (of course, it is also the ^_^ implemented by LUA-iconv)

Libiconv description

The solution here is not quite right, the end-level solution see http://www.cnblogs.com/windtail/archive/2012/09/06/2674260.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.