We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How do I edit a word document on the server

czutshi
czutshi asked
on
Medium Priority
1,582 Views
Last Modified: 2013-11-24
I have a microsoft word document on the server and I would like to know if it possible to open this document  from the server on the client machine edit the same and save it back to the server using javascript or java code
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
not possible unless you have an application than can save via http. Not aware of any that can
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you would need to write your own word editor to achieve it
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
You can make a web page where user can select, say, the name of the word  document, download it
and on the next page you can offer the form for uploading the file, which could
use servlet as an action, which would upload the file and replace it in its original location.
Awarded 2011
Awarded 2011

Commented:
If the word files are not too complex and required editing is not too dramatic
you may ihink about writing the servlet where the user will select the file, specify in some form
what editing is necessary
the servlet could open it using Apache POI
http://poi.apache.org/hwpf/quick-guide.html
and do some basic editing.
That would be of course difficult to implement if substantial
editing  would be required.

Author

Commented:
Check the code below
<script>
var w=new ActiveXObject('Word.Application');
var docText;
var obj;
 
var path="http://192.168.1.41:8080/pavementdev/images/SqlProc.doc";
if (w != null)
{
w.Visible = true;
obj=w.Documents.Open(path);
docText = obj.Content;
//w.Selection.TypeText("Hello");
w.Documents.Save();
}
</script>

I wanted to know if the above code can Append "Hello" and save the changes to the original location then is there no way it can accept user input and do the same

Author

Commented:
M sorry the above code prompts the user to save the modified file on the local machine but is there a way i can open the file in edit mode I came across this link which i thought are trying to achieve somethin similar

 http://dotnetninja.wordpress.com/2009/01/12/how-to-open-sharepoint-documents-in-edit-mode-with-internet-explorer-6-and-windows-xp-without-regfix/
Awarded 2011
Awarded 2011

Commented:

This does not seem straightforward to me.
I doubt if Word.Application active X could
just directly save it across the network to the server
without any participation of the server.

I think what I suggested - is more feasible - if eidting
is very basic and could be described in the form,
then it is better not to upload the file to the client but rather
do it all on the server side using java library
accessing Word document like Apache POI.

This is not too simple either, but still should be doable.

Author

Commented:
Is it possible to write a sever side code that would retain the contents of the word document as it is and after modification save the changes to the original file
Awarded 2011
Awarded 2011

Commented:

Yes, that seems to me that it is doable - the server side program exposed to user as a servlet
will collect information waht to do and with what file and then open it with POI and
then modify it and write back to the same place - provided that file is not too complex and editing
pretty basic that should be doable.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Is it possible to write a sever side code that would retain the contents of the word document as it is and after modification save the changes to the original file

thats basically what I suggested above
the client would be editting a copy of the doc and upload it once done
Awarded 2011
Awarded 2011

Commented:
If the editing is substantial then server-side editing from inside java would be difficult and
you can do it as I suggested above:

You can make a web page where user can select, say, the name of the word  document, download it
and on the next page you can offer the form for uploading the file, which could
use servlet as an action, which would upload the file and replace it in its original location.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
thats an interesting article about sharepoint. If you're ina pure MS environment then there may be more possiblities.
Another option would be to share the document in the cloud instead of using your own server. That way it could be easily editted by anyone you grant access

Author

Commented:
Can anyone explain what is being achieved in this HTML and corresponding js file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <title></title>

 
HTML CODE-
 <form id="form2" runat="server">

<div>

 <a href="http://192.168.1.41:8080/pavementdev/images/SqlProc.doc">link to file</a>

 <a href="" onclick="editDocumentWithProgID2('http://192.168.1.41:8080/pavementdev/images/SqlProc.doc', '', 'SharePoint.OpenDocuments', '0', 'http://192.168.1.41:8080/pavementdev', '0')">Edit onclick</a></div>

</form>

 <script src="sp.js" type="text/javascript"></script>

</HTML>

JS FILE-

var L_EditDocumentProgIDError_Text="'Edit Document' requires a Windows SharePoint Services-compatible application and Microsoft Internet Explorer 6.0 or greater.";
var L_EditDocumentRuntimeError_Text="The document could not be opened for editing.  A Windows SharePoint Services compatible application could not be found to edit the document.";var browseris=new Browseris();
function editDocumentWithProgID2(strDocument,varProgID,varEditor,bCheckout,strhttpRoot,strCheckouttolocal)
{var errorCode=editDocumentWithProgIDNoUI(strDocument,varProgID,varEditor,bCheckout,strhttpRoot,strCheckouttolocal);if(errorCode==1){alert(L_EditDocumentRuntimeError_Text);window.onfocus=RefreshOnNextFocus;}else if(errorCode==2)alert(L_EditDocumentProgIDError_Text);}
function editDocumentWithProgIDNoUI(strDocument,varProgID,varEditor,bCheckout,strhttpRoot,strCheckouttolocal)
{var objEditor;var fRet;var fUseLocalCopy=false;varEditor=varEditor.replace(/(?:\.\d+)$/,'');
if(strDocument.charAt(0)=="/"||strDocument.substr(0,3).toLowerCase()=="%2f")
strDocument=document.location.protocol+"//"+document.location.host+strDocument;
var strextension=SzExtension(unescapeProperly(strDocument));
if(FSupportCheckoutToLocal(strextension))
{try{objEditor=new ActiveXObject(varEditor+".3");
if(objEditor!=null)
{if(bCheckout=="1"){
	if(!objEditor.CheckoutDocumentPrompt(strDocument,true,varProgID))return 1;}
	else
	{if(strCheckouttolocal=="1")fUseLocalCopy=true;if(!objEditor.EditDocument3(window,strDocument,fUseLocalCopy,varProgID))return 1;}
	var fRefreshOnNextFocus=false;
	fRefreshOnNextFocus=objEditor.PromptedOnLastOpen();
	if(fRefreshOnNextFocus)
	{window.onfocus=RefreshOnNextFocus;}
	else
	{SetWindowRefreshOnFocus();}return;}}
	catch(e)
	{}}
	if(bCheckout=="1")
	{if(confirm(L_ConfirmCheckout_Text))NavigateToCheckinAspx(strhttpRoot,"FileName="+escapeProperly(unescapeProperly(strDocument))+"&Checkout=true");
	else return ;}
	try{objEditor=new ActiveXObject(varEditor+".2");if(!objEditor.EditDocument2(window,strDocument,varProgID))
	return 1;
	window.onfocus=RefreshOnNextFocus;
	return;}
	catch(e)
	{}
	try
	{objEditor=new ActiveXObject(varEditor+".1");window.onfocus=null;
	if(SzExtension(strDocument)=="ppt"&&varProgID=="")
	varProgID="PowerPoint.Slide";
	if(!objEditor.EditDocument(strDocument,varProgID))
	return 1;
	SetWindowRefreshOnFocus();
	return;}
	catch(e)
	{return 2;}}
function RefreshOnNextFocus()
{SetWindowRefreshOnFocus();}
function Browseris()
{var agt=navigator.userAgent.toLowerCase();this.osver=1.0;if(agt){var stOSVer=agt.substring(agt.indexOf("windows ")+11);this.osver=parseFloat(stOSVer);}this.major=parseInt(navigator.appVersion);this.nav=((agt.indexOf('mozilla')!=-1)&&((agt.indexOf('spoofer')==-1)&&(agt.indexOf('compatible')==-1)));this.nav6=this.nav&&(this.major==5);this.nav6up=this.nav&&(this.major>=5);this.nav7up=false;if(this.nav6up){var navIdx=agt.indexOf("netscape/");if(navIdx>=0)this.nav7up=parseInt(agt.substring(navIdx+9))>=7;}this.ie=(agt.indexOf("msie")!=-1);this.aol=this.ie&&agt.indexOf(" aol ")!=-1;if(this.ie){var stIEVer=agt.substring(agt.indexOf("msie ")+5);this.iever=parseInt(stIEVer);this.verIEFull=parseFloat(stIEVer);}else this.iever=0;this.ie4up=this.ie&&(this.major>=4);this.ie5up=this.ie&&(this.iever>=5);this.ie55up=this.ie&&(this.verIEFull>=5.5);this.ie6up=this.ie&&(this.iever>=6);this.winnt=((agt.indexOf("winnt")!=-1)||(agt.indexOf("windows nt")!=-1));this.win32=((this.major>=4)&&(navigator.platform=="Win32"))||(agt.indexOf("win32")!=-1)||(agt.indexOf("32bit")!=-1);this.mac=(agt.indexOf("mac")!=-1);this.w3c=this.nav6up;this.safari=(agt.indexOf("safari")!=-1);this.safari125up=false;if(this.safari&&this.major>=5){var navIdx=agt.indexOf("safari/");if(navIdx>=0)this.safari125up=parseInt(agt.substring(navIdx+7))>=125;}}
function unescapeProperlyInternal(str)
{if(str==null)return "null";var ix=0,ixEntity=0;var strResult="";var rgUTF8Bytes=new Array;var ixUTF8Bytes=0;var hexString,hexCode;while(ix<str.length){if(str.charAt(ix)=='%'){if(str.charAt(++ix)=='u'){hexString="";for(ixEntity=0;ixEntity<4&&ix<str.length;++ixEntity){hexString+=str.charAt(++ix);}while(hexString.length<4){hexString+='0';}hexCode=parseInt(hexString,16);if(isNaN(hexCode)){strResult+='?';}else{strResult+=String.fromCharCode(hexCode);}}else{hexString="";for(ixEntity=0;ixEntity<2&&ix<str.length;++ixEntity){hexString+=str.charAt(ix++);}while(hexString.length<2){hexString+='0';}hexCode=parseInt(hexString,16);if(isNaN(hexCode)){if(ixUTF8Bytes){strResult+=Vutf8ToUnicode(rgUTF8Bytes);ixUTF8Bytes=0;rgUTF8Bytes.length=ixUTF8Bytes;}strResult+='?';}else{rgUTF8Bytes[ixUTF8Bytes++]=hexCode;}}}else{if(ixUTF8Bytes){strResult+=Vutf8ToUnicode(rgUTF8Bytes);ixUTF8Bytes=0;rgUTF8Bytes.length=ixUTF8Bytes;}strResult+=str.charAt(ix++);}}if(ixUTF8Bytes){strResult+=Vutf8ToUnicode(rgUTF8Bytes);ixUTF8Bytes=0;rgUTF8Bytes.length=ixUTF8Bytes;}
return strResult;}
function unescapeProperly(str)
{var strResult=null;if((browseris.ie55up||browseris.nav6up)&&(typeof (decodeURIComponent)!="undefined")){strResult=decodeURIComponent(str);}else{strResult=unescapeProperlyInternal(str);}return strResult;}
function SzExtension(szHref){var sz=new String(szHref);var re=/^.*\.([^\.]*)$/;return sz.replace(re,"$1").toLowerCase();}
var g_ExtensionNotSupportCheckoutToLocal=new Array("ascx","asp","aspx","htm","html","master","odc","exe","bat","com","cmd","onetoc2");var g_ExtensionDefaultForRead=new Array("jpg","jpeg","bmp","png","gif","onetoc2","one","odc");function FSupportCheckoutToLocal(strExtension){var fRet=true;if(strExtension==null||strExtension=="")return false;strExtension=strExtension.toLowerCase();var ix=0;for(ix=0;ix<g_ExtensionNotSupportCheckoutToLocal.length;ix++){if(strExtension==g_ExtensionNotSupportCheckoutToLocal[ix])return false;}return true;}var g_varSkipRefreshOnFocus=0;function RefreshOnFocus(){if(typeof (g_varSkipRefreshOnFocus)=="undefined"||!g_varSkipRefreshOnFocus){var url=window.location.href;var iPosition = url.indexOf("#");if(iPosition==-1)window.location.href=url;else window.location.href=url.substring(0,iPosition);}}function DisableRefreshOnFocus(){g_varSkipRefreshOnFocus=1;}function SetWindowRefreshOnFocus(){window.onbeforeunload=DisableRefreshOnFocus;window.onfocus=RefreshOnFocus;}

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
its loading an activex editor

Author

Commented:
Is it possible to write a java/javascript code that will use this activex editor to edit word documents on the fly and here it is ok if this can be achieved only in I.E.Are there any restrictions to this like server type etc.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you'd need documentation on it to determine that

Author

Commented:
The solution works but is not the optimal one
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.