How to open new Word docx document in word Add-in - ms-word

I have developed a word add-in using word javascript api. My Document .docx file is on server and i need to open that .docx document as a new word document on a button click in add-in.
Please guide me how i can open new document in word add-in.
Thanks.

There is a new method we are adding to the API that you can actually use to achieve this. Notice that is in preview, which means will be in production in a couple of months. You need latest Office version plus reference our preview office.js to try it. The office.js preview is here https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
Check out this code sample on how easy is to do it.
function onaddOpenDoc() {
Word.run(function (context) {
// this getDocumentAsBase64 assumes a valid base64-encoded docx file
var myNewDoc = context.application.createDocument(getDocumentAsBase64());
context.load(myNewDoc);
return context.sync()
.then(function () {
myNewDoc.open();
context.sync();
}).catch(function (myError) {
//otherwise we handle the exception here!
showNotification("Error", myError.message);
})
}).catch(function (myError) { showNotification("Error", myError.message); });
}

Opening a document in a new instance (i.e. a new, separate Word window) is, at least for the time being, not supported by the JavaScript-based Office.js API. You are always starting from a Context object, which will give you access to the currently active document via the Context.document property.
What you can do is insert content into an existing document, e.g. via the body.insertOoxml method.
Currently, the Office.js API is still limited as compared to the classic COM API. If you need the full feature-set you still might consider developing a COM or VSTO solution today. The only downside is that your add-in won't run on any platform other than Windows desktop.

Related

Update docvariable or docproperty in Word online

Is ther a way to update docvariable or docproperty of a Word document when opening in the Word online by the microsoft graph API or another API.
I do this in Word for desktop using C# code, but I need to do this in Word online too.
It can't be done in Word on-line. If you had an Office JS add-in you might be able to change document properties (but no DocProperty fields that reflect those values). The Word JS APIs do not access the Document Variables, however.
But you should be able to do it on the closed file (before it's opened in Word on-line, for example) by leveraging the Word Open XML file format. There are numerous libraries to work with it. Microsoft's is the Open XML SDK (free, as a .NET Framework / Visual Studio extension) which can access all content of Office files (except VBA binaries).

How to load a Word add-in with a specific file read from file system with Office.js

I'm a newbie in Office365 add-in development, and I'm trying create a poc for a office 365 word add-in and I need to load the add-in with a docx file read from filesystem (alternately from one-drive). Using
Word.run(function (context) {
var body = context.document.body;
...
the document is the current blank word document. Is it possible to start the add-in with a file read from file system? or one the add-in is loaded, can I open a docx file and work on it. I Search in the documentation, but I didn't find anything useful.
Thanks
the method that you want to use in this case is the insertFileFromBase64 method. This method takes as first parameter a base64 encoded docx that then you can insert it.
Check out the reference here
https://dev.office.com/reference/add-ins/word/body?product=word?product=word
note that you need to provide an experience of the user picking a file from the system (using regular HTML file controls) and then base-64 encode it.
Hope this sets you up in the right direction.

Word Add-in - Save/load document to/from a specific location

I'm currently developing a Word add-in using the word-15.js script. I'm trying to save the document on some location. I assume that calling document.save(), saves the document to a location where the document already exists, so only if the document has been saved before and isn't a new document.
In my scenario, I'm trying to save/load a new document. For instance, when the user has created a new document and entered some text or other data, my add-in can load a SharePoint document library and save the that location. For instance: https://my-tenant.sharepoint.com/sites/my-site/_api/web/GetFolderByServerRelativeUrl('Shared Documents')/Files/add(url='test.docx', overwrite=true).
I haven't seen this done in any examples that I've been able to scramble through. Is this even possible? And if so, how?
there is no such API. you can, however, get the document (as docx, pdf or txt) using the getFileAsync API. and then once once you have the bits of the document you can upload it anywhere you need.
Here are more details and an example on how to use getFileAsync.
https://github.com/OfficeDev/office-js-docs/blob/master/reference/shared/document.getfileasync.md

Dynamic data manipulation in word online

I am trying to build a task-pane add-in for word online which can do mail merge like desktop office. Now I understand that word online does not support mail merge out of the box, so I thought about the following steps -
Create a template (DOTX) using desktop office which will contain Region or something like a merge field etc. Upload the template to one drive.
Open a blank doc and add a task pane add-in in word online. This is temporary step for now.
Click a button on the add-in.
This click will stream the template (DOTX) from server using an app service call.
Render the stream into word online web page.
Edit and save.
Close word online and reopen.
Click the doc link.
This will render the doc in word online.
Add the add-in in the task pane (temp step).
Click edit button.
Call app service to fetch the latest merge data source.
Traverse through the word xml and replace the values corresponding to the region or special keys.
Re-render the doc.
My users will have E1 subscription of office 365.
Is this thought process logical and achievable with word online? Thanks in advance!...
I think your approach is much more complicated than it needs to be. Word Online supports Add-ins created using the JavaScript APIs, as supported by the information in this article: https://msdn.microsoft.com/EN-US/library/office/dn833104.aspx
It should be possible for you to create the "template" on the Desktop (Word 2013 or 2016 is required - but only 2016 supports the current full scope of the JS API) and "insert" your add-in, which links it to the document. Opening the document in Word online should automatically load the add-in, that can then link to the data source and fill in the data.
As data targets ("merge fields"), I recommend you use Content Controls.

Saving document to server in Java

We have an existing web application written in Java. We want to give our users the ability to open a Word document that is stored in our application on the server. So when the user downloads the document from our server, Word pops up and they then can edit this document on the client. Once finished editing the document we would like to give them the ability to save this document automatically back to the server where they retrieved it, by simply selecting on the Word Save icon. Similar to how SharePoint does it.
Seems to me SharePoint would be a good solution for this and they do it very well, but unfortunately we cannot use SharePoint in our architecture.
There also seems to be a lot of server-side possibilities that we can use to upload and open word documents from our server, I feel we can solve these use cases easily.
My big problem is the automatic saving of an edited document by the client back to our server where the document was downloaded
I am not Microsoft proficient to know what the best solution would be.
Should we write a Word AddIn to save to our server?
How do we get the Word AddIn into our users desktops?
Can we embed a Word AddIn into the Word document that is opened on the client?
1.Should we write a Word AddIn to save to our server?
If you don't want to use SharePoint you can develop an add-in where you can implement all the required functionality and much more.
2.How do we get the Word AddIn into our users desktops?
You can develop an installer for the add-in. See Deploying an Office Solution for more information. Also you can use the group policy objects for deploying the software automatically.
3.Can we embed a Word AddIn into the Word document that is opened on the client?
You can develop a document-level add-in, i.e. the code will be run for a specific document only. But you will need to install it as an application level add-in. See Architecture of Document-Level Customizations for more information.