[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1467
  • Last Modified:

Opening file

I have a word document file located in c:/docupat.doc of my local machine which is a webserver, is it possible to open this file, edit and save using .NET in webforms....




Moiz
0
Moizsaif123
Asked:
Moizsaif123
  • 23
  • 19
  • 5
1 Solution
 
_TAD_Commented:

Yes, but you have to open it using MS Word API



using Word;

private object docPath = (object)@"C:\temp\myDoc.doc";
private object missing = System.Reflection.Missing.Value;
private object readOnly = false;
private object isVisible = true;


try
{
Word.Document aDoc = WordApp.Documents.Open(ref docPath, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing, ref missing, ref missing);
aDoc.Activate();
WordApp.Visible = true;
}
catch{}
0
 
Moizsaif123Author Commented:
no actually i just need to edit the word document, I dont need to open it, should be on server side..
0
 
Moizsaif123Author Commented:
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
_TAD_Commented:



You still need to open the word document using the word api.  You can open the document, but just not have it visible.

appending to the file is easy, once you have it open
0
 
Moizsaif123Author Commented:
is it possible for you to send the code in Vb instead of c#








Moiz
0
 
_TAD_Commented:
Syntactically there is very little difference between C# and VB
Go here: http://support.microsoft.com/?kbid=308470
Download and Print (12 pages).  


imports Word;

Dim docPath as Object = "C:\temp\myDoc.doc"
Dim  missing as Object = reflection.Missing.Value;
Dim readOnly as Object= false;
Dim isVisible as Object = true;


begin try
  Dim aDoc as Word.Document

  aDoc = WordApp.Documents.Open(ref docPath, ref missing, ref readOnly,
  ref   missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
  ref missing, ref isVisible,ref missing, ref missing, ref missing);

  aDoc.Activate();

  WordApp.Visible = true;
end try

begin catch

end catch
0
 
davidlars99Commented:
you have to refference "winwordcontrol.dll", it's in system32 folder
0
 
Moizsaif123Author Commented:



I edited the code above and i marked below where it blows and gives me the error and i am using webforms...

Object reference not set to an instance of an object.


Dim docPath As Object = "C:\Documents and Settings\client\Desktop\moiz.doc"
        Dim missing As Object = Reflection.Missing.Value
        Dim readOnly1 As Object = False
        Dim isVisible As Object = True


        Try
            Dim aDoc As Microsoft.Office.Interop.Word.Document


aDoc.Application.Documents.Open(docPath  _                      << blows here
, missing, readOnly1, missing,missing ,missing _
,missing ,missing ,   missing,missing , missing,  _
missing, missing, missing, missing)

            aDoc.Activate()

            aDoc.Application.Visible = True

        Catch ex As Exception
            textbox1.text = ex.Message
        End Try
0
 
Moizsaif123Author Commented:
the complete error message is:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
0
 
_TAD_Commented:


you may have to change

  Dim aDoc As Microsoft.Office.Interop.Word.Document
to
  Dim aDoc As New Microsoft.Office.Interop.Word.Document
0
 
Moizsaif123Author Commented:

now it blows over at the same place, with this ....this is an internal error, is the dll corrupt..I downloaded this dll from link below..

http://www.microsoft.com/downloads/details.aspx?familyid=c41bd61e-3060-4f71-a6b4-01feba508e52&displaylang=en


An unhandled exception of type 'System.ExecutionEngineException' occurred in webapplication35.dll


Server Application Unavailable
The web application you are attempting to access on this web server is currently unavailable.  Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the system event log of the web server. Please review this log entry to discover what caused this error to occur.
0
 
davidlars99Commented:
0
 
Moizsaif123Author Commented:

I referenced that winwordcontrol dll, getting this error on lin34


QueryInterface for interface Word._Document failed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: QueryInterface for interface Word._Document failed.

Source Error:


Line 32:         Dim aDoc As New Word.Document()
Line 33:
Line 34:         aDoc.Application.Documents.Open(docPath, missing, readOnly1, _
Line 35:         missing, missing, missing, missing, missing, missing, _
Line 36:         missing, missing, isVisible)
 
0
 
davidlars99Commented:
that looks like one of two things or both of them


1) it might be a permission issue, ASPNET account doesn't have write access on the folder where word document is located, in other words
you may need to give ASPNET write permissions

2) everytime you create office COM document from .NET you must let it go afterwords

Private Sub Letgo(ByRef wDoc as Object)
    Try    
          System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc)
    Catch ex as Exception
          ' handle some error
     Finally      
          wDoc = nothing
End Sub  
0
 
davidlars99Commented:
if you don't want to give ASPNET write permission, then you can create and impersonate windows user
0
 
davidlars99Commented:
0
 
davidlars99Commented:
after all you may need to restart yout computer and start all over the right way!
0
 
Moizsaif123Author Commented:

I tried to get full permission after adding the asp.net for that folder...the event log i get in my error log i guess its for the server unavailable error page...


Event Type:      Error
Event Source:      ASP.NET 1.0.3705.288
Event Category:      None
Event ID:      1000
Date:            04/12/2005
Time:            1:08:17 PM
User:            N/A
Computer:      PROG6
Description:
aspnet_wp.exe  (PID: 2492) stopped unexpectedly.

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
0
 
Moizsaif123Author Commented:
maybe its occuring because im running office 2000 and running a higher versioned dll of office word. im having office 2000, where in my computer can i find the dll for word application...




Moiz
0
 
davidlars99Commented:
try to disable "script blocking" from you anti virus software and I think DLL for word must be in system32, but you have to refference it thru
visual studio > select components tab from toolbox and right click on it > select Add and window like this will come up http://www.codeproject.com/aspnet/wordapplication.asp > add "MS Word 1.x Object Library" and you should be good to go
0
 
davidlars99Commented:
@  maybe its occuring because im running office 2000 and running a higher versioned dll of...

Visual Studio will do this for you, if you follow the steps I've just told you
0
 
Moizsaif123Author Commented:
hmm , after extensive searching with ppl experiencing the same issue, its finally worked, below using this code...its called late binding which is to reference the dll on runtime and not design time... the .net app finds the dll for yu...so how can i change or append the .doc file now...


 Dim oWord As Object

oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open("C:\Documents and Settings\client\Desktop\6-10-2004(latest)\kk.doc")
0
 
davidlars99Commented:
evetualy all the function will remain the same as it is in this example http://www.exceltip.com/st/Control_Word_from_Excel_using_VBA_in_Microsoft_Excel/465.html
but in .NET you can't use the "set" keyword
0
 
Moizsaif123Author Commented:
after opening and changing the file or document... there are like 3 sessions on the taskmanager that add up...how to then kill them after creating and changing a new file...
0
 
_TAD_Commented:

You'll probably want to add this line at the end:

System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord)
0
 
Moizsaif123Author Commented:
i dint try that but from yur most recent post.....

wrdApp.Quit >worked
0
 
davidlars99Commented:
try this

Private Sub Letgo(ByRef wDoc as Object)
    Try    
          System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc)
    Catch ex as Exception
          ' handle some error
     Finally      
          wDoc = nothing
End Sub  


and if it fails, don't use it... and I don't think "wrdApp.Quit" will release the object completely from the memory, it's always a good practice
to clean up afterwards
0
 
Moizsaif123Author Commented:
any such links available in appending acrobat reader...id like to try that out too...




Moiz
0
 
Moizsaif123Author Commented:
or do i have to convert the word doc into adobe...?
0
 
davidlars99Commented:
I don't think it's as easy as creating ms office documents but there's a .NET components available that can do that or you have to develop
one on your own, to get started I think you better call Adobe customer service and they'll tell you where to look and ect...

http://www.rarefind.com/pdfnmore/dotnet/index.html
0
 
davidlars99Commented:
I just called them and they gave me this link and told me buy $99.00 ASN pachage which contains full SDK and hot sample  :)

http://partners.adobe.com/public/asn/developer/detail.html
0
 
davidlars99Commented:
that's good but I think you need to have

Dim pdf As ACRODISTXLib.PdfDistiller = New ACRODISTXLib.PdfDistiller()

is that freely availabe with acrobat reader..?
0
 
Moizsaif123Author Commented:
yea i guess so...if you look at the link below...

http://www.experts-exchange.com/Web/Graphics/Adobe_Acrobat/Q_20896653.html
0
 
Moizsaif123Author Commented:
i tried to download the sdk software for acrobat 6.0 first by creating a username and paswd...it doesnt let me go through, saying insufficient privledges...dont know..
0
 
davidlars99Commented:
like I told you have to purchase it from here, they have three packages and I think chippest one is $99, what country are you in..? I'm in US

http://partners.adobe.com/public/asn/developer/detail.html
0
 
Moizsaif123Author Commented:
check out this, and itextsharp tutorials...

http://www.aspnetworld.com/articles/2004011801.aspx
0
 
Moizsaif123Author Commented:
0
 
davidlars99Commented:
cool link... I like them, I was thinking about buying $99 package..  :) thanks
0
 
Moizsaif123Author Commented:
the thing is now that i am working on that...the two libraries needed are the itextsharp and the ICSharpCode.SharpZipLib.

the itextsharp ver 1.0.4... doesnt work with ICSharpCode.SharpZipLib version 0.83.0.0

atleast it didnt for me...get this error..

The located assembly's manifest definition with name 'ICSharpCode.SharpZipLib' does not match the assembly reference.

i looked it up on google, says i need version 0.5.0.0 of the zip library.

You can reply here if yu want to test it out...

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21390963.html
0
 
davidlars99Commented:
unfortunalely I'm not familiar with that stuff, but I'll be watching it...
0
 
Moizsaif123Author Commented:
I know your not working with the pdf but this edcomponent site was helpful, so check it out...and no pay for it...

http://www.experts-exchange.com/Web/Q_21404396.html
0
 
Moizsaif123Author Commented:
0
 
Moizsaif123Author Commented:
anyone had this error below stated as below...It was working before, now i suddenly open my project and i see this error while creating a word document..

oWord = CreateObject("Word.Application")
wrddoc = CreateObject("Word.Document")
oWord.Visible = True

wrddoc = oWord.Documents.Add     <<blows here


Could not open macro storage.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Could not open macro storage.
0
 
davidlars99Commented:
nope.. never!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 23
  • 19
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now