LibreOffice Module sw (master) 1
vbawordbasic.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column:100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include "vbaapplication.hxx"
22#include "vbamailmerge.hxx"
23#include "vbawordbasic.hxx"
24
25#include <basic/sbx.hxx>
26#include <basic/sbxvar.hxx>
29#include <osl/file.hxx>
30#include <sal/log.hxx>
31#include <tools/urlobj.hxx>
32
33#include <com/sun/star/text/XTextDocument.hpp>
34#include <com/sun/star/util/thePathSettings.hpp>
35#include <ooo/vba/word/XBookmarks.hpp>
36#include <ooo/vba/word/XDocuments.hpp>
37
38using namespace ::ooo::vba;
39using namespace ::com::sun::star;
40
42 : mpApp(pApp)
43{
44}
45
47{
48 return SwVbaMailMerge::get(mpApp->getParent(), mpApp->getContext())->getMainDocumentType();
49}
50
51void SAL_CALL SwWordBasic::setMailMergeMainDocumentType(sal_Int32 _mailmergemaindocumenttype)
52{
54 ->setMainDocumentType(_mailmergemaindocumenttype);
55}
56
57void SAL_CALL SwWordBasic::FileOpen(const OUString& Name, const uno::Any& ConfirmConversions,
58 const uno::Any& ReadOnly, const uno::Any& AddToMru,
59 const uno::Any& PasswordDoc, const uno::Any& PasswordDot,
60 const uno::Any& Revert, const uno::Any& WritePasswordDoc,
61 const uno::Any& WritePasswordDot)
62{
63 uno::Any aDocuments = mpApp->Documents(uno::Any());
64
65 uno::Reference<word::XDocuments> rDocuments;
66
67 if (aDocuments >>= rDocuments)
68 rDocuments->Open(Name, ConfirmConversions, ReadOnly, AddToMru, PasswordDoc, PasswordDot,
69 Revert, WritePasswordDoc, WritePasswordDot, uno::Any(), uno::Any(),
71}
72
73void SAL_CALL SwWordBasic::FileSave()
74{
75 uno::Reference<frame::XModel> xModel(mpApp->getCurrentDocument(), uno::UNO_SET_THROW);
76 dispatchRequests(xModel, ".uno:Save");
77}
78
80 const css::uno::Any& Name, const css::uno::Any& Format, const css::uno::Any& /*LockAnnot*/,
81 const css::uno::Any& /*Password*/, const css::uno::Any& /*AddToMru*/,
82 const css::uno::Any& /*WritePassword*/, const css::uno::Any& /*RecommendReadOnly*/,
83 const css::uno::Any& /*EmbedFonts*/, const css::uno::Any& /*NativePictureFormat*/,
84 const css::uno::Any& /*FormsData*/, const css::uno::Any& /*SaveAsAOCELetter*/)
85{
86 SAL_INFO("sw.vba", "WordBasic.FileSaveAs(Name:=" << Name << ",Format:=" << Format << ")");
87
88 uno::Reference<frame::XModel> xModel(mpApp->getCurrentDocument(), uno::UNO_SET_THROW);
89
90 // Based on SwVbaDocument::SaveAs2000.
91
92 OUString sFileName;
93 Name >>= sFileName;
94
95 OUString sURL;
96 osl::FileBase::getFileURLFromSystemPath(sFileName, sURL);
97
98 // Detect if there is no path then we need to use the current folder.
99 INetURLObject aURL(sURL);
101 if (sURL.isEmpty())
102 {
103 // Need to add cur dir ( of this document ) or else the 'Work' dir
104 sURL = xModel->getURL();
105
106 if (sURL.isEmpty())
107 {
108 // Not path available from 'this' document. Need to add the 'document'/work directory then.
109 // Based on SwVbaOptions::getValueEvent()
110 uno::Reference<util::XPathSettings> xPathSettings
112 OUString sPathUrl;
113 xPathSettings->getPropertyValue("Work") >>= sPathUrl;
114 // Path could be a multipath, Microsoft doesn't support this feature in Word currently.
115 // Only the last path is from interest.
116 // No idea if this crack is relevant for WordBasic or not.
117 sal_Int32 nIndex = sPathUrl.lastIndexOf(';');
118 if (nIndex != -1)
119 {
120 sPathUrl = sPathUrl.copy(nIndex + 1);
121 }
122
123 aURL.SetURL(sPathUrl);
124 }
125 else
126 {
127 aURL.SetURL(sURL);
128 aURL.Append(sFileName);
129 }
131 }
132 sal_Int32 nFileFormat = word::WdSaveFormat::wdFormatDocument;
133 Format >>= nFileFormat;
134
135 uno::Sequence aProps{ comphelper::makePropertyValue("FilterName", css::uno::Any()),
136 comphelper::makePropertyValue("FileName", sURL) };
137
138 setFilterPropsFromFormat(nFileFormat, aProps);
139
140 dispatchRequests(xModel, ".uno:SaveAs", aProps);
141}
142
143void SAL_CALL SwWordBasic::FileClose(const css::uno::Any& Save)
144{
145 uno::Reference<frame::XModel> xModel(mpApp->getCurrentDocument(), uno::UNO_SET_THROW);
146
147 sal_Int16 nSave = 0;
148 if (Save.hasValue() && (Save >>= nSave) && (nSave == 0 || nSave == 1))
149 FileSave();
150
151 // FIXME: Here I would much prefer to call VbaDocumentBase::Close() but not sure how to get at
152 // the VbaDocumentBase of the current document. (Probably it is easy and I haven't looked hard
153 // enough.)
154 //
155 // FIXME: Error handling. If there is no current document, return some kind of error? But for
156 // now, just ignore errors. This code is written to work for a very specific customer use case
157 // anyway, not for an arbitrary sequence of COM calls to the "VBA" API.
158 dispatchRequests(xModel, ".uno:CloseDoc");
159}
160
162 const css::uno::Any& DraftFont, const css::uno::Any& WrapToWindow,
163 const css::uno::Any& PicturePlaceHolders, const css::uno::Any& FieldCodes,
164 const css::uno::Any& BookMarks, const css::uno::Any& FieldShading,
165 const css::uno::Any& StatusBar, const css::uno::Any& HScroll, const css::uno::Any& VScroll,
166 const css::uno::Any& StyleAreaWidth, const css::uno::Any& Tabs, const css::uno::Any& Spaces,
167 const css::uno::Any& Paras, const css::uno::Any& Hyphens, const css::uno::Any& Hidden,
168 const css::uno::Any& ShowAll, const css::uno::Any& Drawings, const css::uno::Any& Anchors,
169 const css::uno::Any& TextBoundaries, const css::uno::Any& VRuler,
170 const css::uno::Any& Highlight)
171{
172 SAL_INFO("sw.vba", "WordBasic.ToolsOptionsView("
173 "DraftFont:="
174 << DraftFont << ", WrapToWindow:=" << WrapToWindow
175 << ", PicturePlaceHolders:=" << PicturePlaceHolders
176 << ", FieldCodes:=" << FieldCodes << ", BookMarks:=" << BookMarks
177 << ", FieldShading:=" << FieldShading << ", StatusBar:=" << StatusBar
178 << ", HScroll:=" << HScroll << ", VScroll:=" << VScroll
179 << ", StyleAreaWidth:=" << StyleAreaWidth << ", Tabs:=" << Tabs
180 << ", Spaces:=" << Spaces << ", Paras:=" << Paras
181 << ", Hyphens:=" << Hyphens << ", Hidden:=" << Hidden
182 << ", ShowAll:=" << ShowAll << ", Drawings:=" << Drawings
183 << ", Anchors:=" << Anchors << ", TextBoundaries:=" << TextBoundaries
184 << ", VRuler:=" << VRuler << ", Highlight:=" << Highlight << ")");
185}
186
187css::uno::Any SAL_CALL SwWordBasic::WindowName(const css::uno::Any& /*Number*/)
188{
189 return css::uno::Any(mpApp->getActiveSwVbaWindow()->getCaption());
190}
191
192css::uno::Any SAL_CALL SwWordBasic::ExistingBookmark(const OUString& Name)
193{
194 uno::Reference<word::XBookmarks> xBookmarks(mpApp->getActiveDocument()->Bookmarks(uno::Any()),
195 uno::UNO_QUERY);
196 return css::uno::Any(xBookmarks.is() && xBookmarks->Exists(Name));
197}
198
200 const OUString& Name, const css::uno::Any& Format, const css::uno::Any& ConfirmConversions,
201 const css::uno::Any& ReadOnly, const css::uno::Any& LinkToSource,
202 const css::uno::Any& AddToRecentFiles, const css::uno::Any& PasswordDocument,
203 const css::uno::Any& PasswordTemplate, const css::uno::Any& Revert,
204 const css::uno::Any& WritePasswordDocument, const css::uno::Any& WritePasswordTemplate,
205 const css::uno::Any& Connection, const css::uno::Any& SQLStatement,
206 const css::uno::Any& SQLStatement1, const css::uno::Any& OpenExclusive,
207 const css::uno::Any& SubType)
208{
209 mpApp->getActiveDocument()->getMailMerge()->OpenDataSource(
210 Name, Format, ConfirmConversions, ReadOnly, LinkToSource, AddToRecentFiles,
211 PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate,
212 Connection, SQLStatement, SQLStatement1, OpenExclusive, SubType);
213}
214
215css::uno::Any SAL_CALL SwWordBasic::AppMaximize(const css::uno::Any& WindowName,
216 const css::uno::Any& State)
217{
218 SAL_INFO("sw.vba", "WordBasic.AppMaximize( WindowName:=" << WindowName << ", State:=" << State);
219
220 // FIXME: Implement if necessary
221 return css::uno::Any(sal_Int32(0));
222}
223
224css::uno::Any SAL_CALL SwWordBasic::DocMaximize(const css::uno::Any& State)
225{
226 SAL_INFO("sw.vba", "WordBasic.DocMaximize(State:=" << State << ")");
227
228 // FIXME: Implement if necessary
229 return css::uno::Any(sal_Int32(0));
230}
231
232void SAL_CALL SwWordBasic::AppShow(const css::uno::Any& WindowName)
233{
234 SAL_INFO("sw.vba", "WordBasic.AppShow(WindowName:=" << WindowName << ")");
235
236 // FIXME: Implement if necessary
237}
238
239css::uno::Any SAL_CALL SwWordBasic::AppCount()
240{
241 SAL_INFO("sw.vba", "WordBasic.AppCount()");
242
243 // FIXME: Implement if necessary. Return a random number for now.
244 return css::uno::Any(sal_Int32(2));
245}
246
247void SAL_CALL SwWordBasic::MsgBox(const OUString& sPrompt)
248{
249 SbxArrayRef pArgs = new SbxArray;
250 SbxVariable* pVar = new SbxVariable();
251 pVar->PutString(sPrompt);
252 pArgs->Put(pVar, 1);
253
254 if (!executeRunTimeLibrary(u"MsgBox", pArgs.get()))
255 SAL_WARN("sw.vba", "failed to execute runtime library function MsgBox (" << sPrompt << ")");
256}
257
258void SAL_CALL SwWordBasic::ScreenUpdating(const uno::Any& On)
259{
260 sal_Int32 nOn;
261 if (On >>= nOn)
262 mpApp->setScreenUpdating(nOn != 0);
263}
264
265/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool PutString(const OUString &)
virtual css::uno::Any SAL_CALL Documents(const css::uno::Any &aIndex) override
css::uno::Reference< css::uno::XComponentContext > const & getContext() const
rtl::Reference< SwVbaWindow > getActiveSwVbaWindow()
virtual css::uno::Reference< css::frame::XModel > getCurrentDocument() override
virtual css::uno::Reference< ov::word::XDocument > SAL_CALL getActiveDocument() override
static rtl::Reference< SwVbaMailMerge > const & get(const css::uno::Reference< ooo::vba::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual void SAL_CALL MailMergeOpenDataSource(const OUString &Name, const css::uno::Any &Format, const css::uno::Any &ConfirmConversions, const css::uno::Any &ReadOnly, const css::uno::Any &LinkToSource, const css::uno::Any &AddToRecentFiles, const css::uno::Any &PasswordDocument, const css::uno::Any &PasswordTemplate, const css::uno::Any &Revert, const css::uno::Any &WritePasswordDocument, const css::uno::Any &WritePasswordTemplate, const css::uno::Any &Connection, const css::uno::Any &SQLStatement, const css::uno::Any &SQLStatement1, const css::uno::Any &OpenExclusive, const css::uno::Any &SubType) override
virtual void SAL_CALL ScreenUpdating(const css::uno::Any &On) override
virtual void SAL_CALL ToolsOptionsView(const css::uno::Any &DraftFont, const css::uno::Any &WrapToWindow, const css::uno::Any &PicturePlaceHolders, const css::uno::Any &FieldCodes, const css::uno::Any &BookMarks, const css::uno::Any &FieldShading, const css::uno::Any &StatusBar, const css::uno::Any &HScroll, const css::uno::Any &VScroll, const css::uno::Any &StyleAreaWidth, const css::uno::Any &Tabs, const css::uno::Any &Spaces, const css::uno::Any &Paras, const css::uno::Any &Hyphens, const css::uno::Any &Hidden, const css::uno::Any &ShowAll, const css::uno::Any &Drawings, const css::uno::Any &Anchors, const css::uno::Any &TextBoundaries, const css::uno::Any &VRuler, const css::uno::Any &Highlight) override
virtual sal_Int32 SAL_CALL getMailMergeMainDocumentType() override
virtual css::uno::Any SAL_CALL AppCount() override
virtual void SAL_CALL MsgBox(const OUString &sPrompt) override
virtual css::uno::Any SAL_CALL DocMaximize(const css::uno::Any &State) override
virtual void SAL_CALL FileSaveAs(const css::uno::Any &Name, const css::uno::Any &Format, const css::uno::Any &LockAnnot, const css::uno::Any &Password, const css::uno::Any &AddToMru, const css::uno::Any &WritePassword, const css::uno::Any &RecommendReadOnly, const css::uno::Any &EmbedFonts, const css::uno::Any &NativePictureFormat, const css::uno::Any &FormsData, const css::uno::Any &SaveAsAOCELetter) override
virtual css::uno::Any SAL_CALL AppMaximize(const css::uno::Any &WindowName, const css::uno::Any &State) override
SwVbaApplication * mpApp
virtual void SAL_CALL setMailMergeMainDocumentType(sal_Int32 _mailmergemaindocumenttype) override
virtual void SAL_CALL FileClose(const css::uno::Any &Save) override
virtual void SAL_CALL AppShow(const css::uno::Any &WindowName) override
virtual void SAL_CALL FileOpen(const OUString &Name, const css::uno::Any &ConfirmConversions, const css::uno::Any &ReadOnly, const css::uno::Any &AddToMru, const css::uno::Any &PasswordDoc, const css::uno::Any &PasswordDot, const css::uno::Any &Revert, const css::uno::Any &WritePasswordDoc, const css::uno::Any &WritePasswordDot) override
virtual css::uno::Any SAL_CALL WindowName(const css::uno::Any &Number) override
virtual css::uno::Any SAL_CALL ExistingBookmark(const OUString &Name) override
virtual void SAL_CALL FileSave() override
SwWordBasic(SwVbaApplication *pApp)
T * get() const
URL aURL
float u
sal_Int32 nIndex
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
VBAHELPER_DLLPUBLIC void dispatchRequests(const css::uno::Reference< css::frame::XModel > &xModel, const OUString &aUrl)
bool executeRunTimeLibrary(const std::u16string_view &rSbRtl_command, SbxArray *pParameters)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
State
Reference< XModel > xModel
OUString Name