A lot of doc files need to be converted into pdf files. WPS has this function, but the whole person will become annoyed after too many clicks.
I spent one afternoon searching for the tool software and found several. Some of them are free and trial, but they are always available. Although the generated file name contains unauthorized words, it is much easier to modify the file name in batches.
Who knows what printer is wrong on the lab computer!
So I decided to write one by myself,
The next morning, I began searching for information and found that WPS had a secondary development function.
However, there is no C ++ development interface information, and the C ++ example in the official forum is for the old version.
So I tried to find out the examples of C # and VB written by others and finally finished the work.
Void CTestDocDlg: OnBnClickedButton1 () {_ beginthreadex (NULL, 0, convertThread, this, 0, NULL); // StartConvert (m_FileSrc);} void CTestDocDlg: OnBnClickedButton2 () {// TODO: add the control notification handler code TCHAR Buffer [MAX_PATH]; BROWSEINFO bi; ZeroMemory (& bi, sizeof (BROWSEINFO); bi. hwndOwner = m_hWnd; bi. ulFlags = BIF_RETURNONLYFSDIRS; // you must return the file system directory bi. pszDisplayName = Buffer; // if this parameter is NULL, the dialog box bi cannot be displayed. lpszTitle = _ T ("select a folder "); Bi. lpfn = NULL; bi. iImage = IDR_MAINFRAME; LPITEMIDLIST pIDList = SHBrowseForFolder (& bi); // call the show selection dialog box if (pIDList) {SHGetPathFromIDList (pIDList, Buffer ); // obtain the folder path to the Buffer UpdateData (FALSE); m_FileSrc = Buffer; // Save the folder path in a CString object if (m_FileSrc! = "" & M_FileSrc.GetAt (m_FileSrc.GetLength ()-1 )! = '\') M_FileSrc + = "\"; m_destPath.SetWindowText (m_FileSrc);} else {}} intCTestDocDlg: StartConvert (CString path) {CFileFind fileFinder; CString filePth = path + _ T ("*. doc "); BOOL bFinished = fileFinder. findFile (filePth); // collect and save file information before concentrated processing! While (bFinished) {bFinished = fileFinder. findNextFile (); CString fileName = fileFinder. getFileName (); AddFileInfo (fileName. getBuffer (0); // ConvertFile (path + fileName);} fileFinder. close (); std: vector <std: string >:: iterator theIter; for (theIter = m_vecFileName.begin (); theIter! = M_vecFileName.end (); theIter ++) {ConvertFile (path + theIter-> c_str ();} return 0;} intCTestDocDlg: ConvertFile (CString szFileName) {CApplication app; app. createDispatch ("WPS. APPLICATION "); // app. setVisible (TRUE); // app.doc app. put_Visible (FALSE); CDocuments docs = app. get_Documents (); CDocument0 doc = docs. open (szFileName, FALSE, TRUE, FALSE, NULL, NULL, TRUE, NULL, NULL, 0, 0, FALSE, FALSE, 0, FALSE); CString parameter name = szFileName; parameter name. replace ("doc", _ T ("pdf"); doc. exportPdf (portable name, NULL, NULL); // docs. close (NULL, NULL, NULL); // doc. close (NULL, NULL, NULL); COleVariant vtOptional (long) DISP_E_PARAMNOTFOUND, VT_ERROR), vtTrue (short) TRUE), vtFalse (short) FALSE); doc. close (vtFalse, vtOptional, vtOptional); return 0;} unsigned int WINAPI CTestDocDlg: convertThread (void * pParam) {CoInitialize (NULL); (CTestDocDlg *) pParam) -> ReadConvert ();: CoUninitialize (); return 0;} intCTestDocDlg: ReadConvert () {StartConvert (m_FileSrc); return 0;} void CTestDocDlg :: addFileInfo (CString strFileName) {m_vecFileName.push_back (strFileName. getBuffer (0 ));}
The second WPS development of the original C ++ version will be known later. The interface is also referred to in WORD !!!
Reference http://stackoverflow.com/questions/145573/creating-opening-and-printing-a-word-file-from-c
Finally solved the document close problem (to PDF) is not closed, the doc file has been occupied, but the call to close function always fails, refer to the http://support.microsoft.com/kb/220911/en-us
Re-constructed the close function parameter to solve the problem.
Put the conversion code in the thread to open the file is always failed, the subsequent thread is finally resolved, with a function to initialize COM, and each thread needs to be initialized! The original reason is that COM components and threads are associated with the http://support.microsoft.com/kb/238393/en-ushttp://www.qtcn.org/bbs/simple? T31216.html