Jump to content

Problem exporting data to MSWord


SMARAM

Recommended Posts

Hi there,

 

Could anyone help me with exporting data to MSWord?

I have a procedure that works on desktop but doesn't work when on ISAPI. It works on unigui exe too. 

 

CoInitialize(NULL);

try
{
Variant vMSWord;
Variant vWDocuments, vWDocument;
vMSWord = Variant::CreateObject("Word.Application");

String vNomeArq = "";
try
{
vMSWord.OlePropertySet("Visible", true);
vNomeArq = UniServerInstance()->StartPath + "modelo.doc";

vWDocuments = vMSWord.OlePropertyGet("Documents");

OleVariant vFileName = vNomeArq;
Variant vLink = Unassigned;
Variant vReadOnly = false;
Variant vFormat = Unassigned;
vWDocument  = vWDocuments.OleFunction("Open", vFileName, vLink, vReadOnly, vFormat);

OleVariant vTxtOld = "<NOME>";
OleVariant vTxtNew = "FULANO DA SILVA";

vMSWord.OlePropertyGet("Selection").OleFunction("HomeKey", 6);
vMSWord.OlePropertyGet("Selection").OlePropertyGet("Find").OleFunction("Execute", vTxtOld, false, true, false, false, false, true, false, false, vTxtNew, 2, false, false, false, false);

vWDocument.OleProcedure("Save");
vWDocuments.OleFunction("Close");
vMSWord.OleFunction("Quit");
}
catch (Exception &erro)
{
ShowMessage("Erro: " + vNomeArq + " - " + erro.Message);
}

}
__finally
{
  CoUninitialize;
}

 

Link to comment
Share on other sites

Hi,

A couple of days ago i've found the same issue editing an excel workbook.

It was related to the access grants for the user assigned to run the windows service. In my case i've found a workaround in windows 7 creating the folder c:\Windows\System32\config\systemprofile\Desktop (the write access for the SYSTEM user is inherited), If yours is a 32 bits service running under a 64bits environment check the same but with c:\windows\syswow64\config\systemprofile\Desktop.

In my case the production server is a win2008 and it didn't work, so i was forced to run the service as administrator.

 

References:

https://forums.embarcadero.com/thread.jspa?threadID=252184

https://blogs.msdn.microsoft.com/dataaccesstechnologies/2012/12/19/error-microsoft-office-excel-cannot-access-the-file-while-accessing-microsoft-office-11-0-object-library-from-ssis/

 

Good luck.

Link to comment
Share on other sites

Hi,

A couple of days ago i've found the same issue editing an excel workbook.

It was related to the access grants for the user assigned to run the windows service. In my case i've found a workaround in windows 7 creating the folder c:\Windows\System32\config\systemprofile\Desktop (the write access for the SYSTEM user is inherited), If yours is a 32 bits service running under a 64bits environment check the same but with c:\windows\syswow64\config\systemprofile\Desktop.

In my case the production server is a win2008 and it didn't work, so i was forced to run the service as administrator.

 

References:

https://forums.embarcadero.com/thread.jspa?threadID=252184

https://blogs.msdn.microsoft.com/dataaccesstechnologies/2012/12/19/error-microsoft-office-excel-cannot-access-the-file-while-accessing-microsoft-office-11-0-object-library-from-ssis/

 

Good luck.

 

I think that the problem is with access permissions too. 

My server is Windows 2012 R2 64 bits and the application is 32 bits. The service is already running as administrator but didn't work. 

 

I'm trying to run this other code that found on this link:

http://forums.unigui.com/index.php?/topic/1538-not-found-excelapplication/

But it gives the message: Access is denied, ProgID: "Excel.Application"

procedure TMainForm.UniFileUploadCompleted(Sender: TObject;
  AStream: TFileStream);
var
  Excel: Variant;
  WorkSheet : Variant;
  s: string;
begin
  try
  try
    Excel := CreateOleObject('Excel.Application');
    Excel.Visible := False;

    memo.Lines.Add('Trying to load: ' + AStream.FileName);
    Excel.Workbooks.Open(AStream.FileName);
    memo.Lines.Add('File loaded');
    WorkSheet := Excel.Workbooks[1].WorkSheets[1];
    s := WorkSheet.Cells[1, 1];
    memo.Lines.Add(Format('[1,1]="%s"', [s]));
  except
    on E: Exception do memo.Lines.Add('Error in Excel: ' + E.Message);
  end;
  finally
    if not VarIsEmpty(Excel) then Excel.Quit;
  end;

end;
Link to comment
Share on other sites

The embarcadero post refers to another post that summarizes posible workarounds for the excel issue. I tried myself without success, but maybe can help.

 

http://troyvssharepoint.blogspot.cl/2012/07/stumbled-upon-interesting-one-today.html

 

Thank you!

It works now. :D

I didn't understand the first time you replied about the Desktop folder. Everything became clear after seeing the link above.

Do you need any help or it's already working for you too?

Link to comment
Share on other sites

Nop, for some reason it didn't work for me in server2008, so i was forced to run the service as administrator, but never mine, it's working.

I'm glad I helped ;).

 

I'm running the service as administrator too.  :lol:

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...