I'm attempting to open and read text from a Word document in J#, using Office interop.
The intellisense tip for the first parameter is /** #ref */Object fileName. If I pass it a String with an Object typecast, I get error VJS1252: Actual parameter for byref 'Object' must be assignable. I'm not sure what this means and I can't find any useful references to this error online.
ex:
String fileName = "c:\\document.doc";
Microsoft.Office.Interop.Word._Document document = word.getDocuments().Open((object)fileName, ...
throws the error. I've already written the rest of the program in J#, everything else, including Excel interop, has gone well; but I'm completely stuck with this problem with Word.
Ahah, figured it out. for anyone else that has this problem, you can't typecast inside the function call. So I had to do
Object file = (object)fileName;
Microsoft.Office.Interop.Word._Document document = word.getDocuments().Open(file, ...
Related
I am working on a PowerShell script making use of the Microsoft.BizTalk.ExplorerOM to dynamically update the SB-Messaging SAS key for BizTalk Receive Locations and Send Ports. This is to enable us to roll the SAS keys for our Service Bus queues, and update BizTalk with the new keys as painlessly as possible.
I have this working correctly for Receive Locations, but Send Ports are giving me a different issue.
As soon as I read the PrimaryTransport properties of the Send Port, it seems that some change is made under the covers, that then prevents SaveChanges from working, instead throwing an "Invalid or malformed XML data exception".
This is compared to the the ReceiveLocation, where I can read any of its properties, and then SaveChanges successfully.
Note that in both of this cases, no changes have been made by me. I am simply doing a Read, and then a Save.
Can anyone offer any advice as to what could be causing the issue, and any possible solutions to try?
Had this very same issue, when using Powershell to replace values in ServiceBus ReceiveLocations & SendPorts.
The problem is with the none valid xml symbols in the TransportTypeData, which are converted when the script reads them out in the PS cmd.
All none valid xml symbols (such as the one occuring for Namespace value, ) need to be converted to amp, and if I'm not mistaken even double amp:ed.
Here's an example article showing examples on what I mean by "double amp:ed":
How do I escape ampersands in XML so they are rendered as entities in HTML?
Hope this make sense, and if not, then let me know and I'll give it another go.
Just tried doing this from C#, seems to work ok:
var root = new Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer() { ConnectionString = "Data Source=(local);Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;" };
var sendPort = root.SendPorts["xxxx.ServiceBusQueue"];
System.Diagnostics.Trace.TraceInformation(sendPort.PrimaryTransport.TransportTypeData);
sendPort .PrimaryTransport.TransportTypeData = sendPort.PrimaryTransport.TransportTypeData.Replace("RootManageSharedAccessKey", "MySharedAccessKey");
root.SaveChanges();
I have asked here a question on how setting the filename of a Word document via automation without saving it. Thanks to Remou, I have received a nice way doing this via calling the FileSummaryInfo-Dialog and setting the Title-property.
However now I have the problem that the client wants to have document names with special chars in (point and underscore) and it seems to be a bug (or a feature) of word, that it cuts the title and only takes the chars before the first special char for building the file name! I have already googled a lot, however was not able to find a resolution for this problem.
The problem is also noticed here (see under gotcha), however without a solution.
Has anybody another solution for setting the filename without saving, or a workaround/bugfix for the mentioned odd behavior?
Try easyhook, since do not have Windows machine besides my hand these days.
following is just the call flow (something like what i did years ago, changed a software's socket bind port to different one by Detours)
About Hook the CreateFileW:
The example in the easyhook's wiki is just what we want here.
CreateFileHook = LocalHook.Create(
LocalHook.GetProcAddress("kernel32.dll", "CreateFileW"),
new DCreateFile(CreateFile_Hooked),
this);
In the CreateFile_Hooked you can change the parameter InFileName, then call real CreateFileW
static IntPtr CreateFile_Hooked(
String InFileName,
UInt32 InDesiredAccess,
UInt32 InShareMode,
IntPtr InSecurityAttributes,
UInt32 InCreationDisposition,
UInt32 InFlagsAndAttributes,
IntPtr InTemplateFile)
{
// FIGURE OUT THE FILE NAME THAT YOU WANT HERE
// IF the InFileName is not your Document name "My.doc", then call orignal CreateFile
// with all the parameter unchanged.
// call original API...
return CreateFile(
YOUR_CHANGED_FILE_NAME_HERE,
InDesiredAccess,
InShareMode,
InSecurityAttributes,
InCreationDisposition,
InFlagsAndAttributes,
InTemplateFile);
}
Call flow:
After you changed the title to "My_Document_2012_11_29",
then hook the CreateFileW of Word process.
For example when the InFileName is "My.doc",
then you should change it to "My_Document_2012_11_29".
Because this is done in the Word process, so the Detoured function do not know
"My.doc" is mapping to "My_Document_2012_11_29".
There is lot ways to get this mapping info, one is save this mapping info to
a known file in your app, and read the file in the Detoured function.
We are facing an error of "This method or property is not available because the clipboard is empty or not valid" in one of our Lotus Notes applications. The code basically opens a document, selects all the text, copies it and then pastes it in MS Word document. It does this continuously for a bunch of documents. I found this link which basically suggests adding Application.CutCopyMode = False to the code to resolve the issue.
I need to know can this property be accessed and set via UI for MS Word?
In the example you reference, that Keyword is actually part of the Excel object model, not the Word object model. So you are unlikely to find it in Word's UI. Perhaps you are doing something with Excel as well, or perhaps it is a keyword in the relevant Lotus object model as well?
[
The OP mentioned a link, http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-06/msg00446.html
that suggested changing variables names, but that that did not work.
The linked article also suggests "sleeping for a few milliseconds between copying and pasting"
]
I am getting following exception in perl. Also i am now to perl technology.
Exception is :
Win32::OLE<0.1709> error 0x800a1423
in METHOD/PROPERTYGET "Close" at getWordComments.pl line no 350
here is the sample code of getWordComments.pl where exception is comming.
A) Following code for opening the document
#Open the document in MS Word
use Win32::OLE;
{
no warnings;
use Win32::OLE::Const 'Microsoft.Word'; # wd constants
}
$word=Win32::OLE->new('Word.Application');
$word->{Visible} = 1;
$word->{DisplayAlerts} = 0;
$Document=$word->Documents->Open({Filename => $filename, ReadOnly => 1});
B) Then i am reading the comment.
C) Following code for Closing the document.
$Document->{Saved}=1;
$Document->Close;
undef $Document;
#Close Word
$word->Quit;
undef $word;
is this problem with office version?
because document is with .docx. its working properly for .doc.
Please help me to solve this issue.
I am reading the comment form the document and saving the document on server. Its working fine for rest of the document with extension *.docx and *.doc
Also can you please provide me like how i can do this in perl.
i want to close the document for 2003 office and 2007 office version.
Does this is Version issue?
Thanks and regards
Arvind Porlekar
Wait! You're opening it ReadOnly and then marking it as Saved?? That right there throws flags in my mental processor.
The documentation that I can find seems to indicate that this is an issue regarding saving to a different format. That might account for the it-works-in-one-but-not-the-other case.
Also, I've seen indications that this is a COM error. It helps to know something about COM. Likely doc and docx are completely different implementations of the same interface defined by the previous doc logic. And it might be the case that the older implementation (doc) is okay with saying that you want to open it ReadOnly, but then wanting to mark it as saved, while the new implementation has the idea that you really should not do this.
As you can see here, one of the arguments handled is OriginalFormat, and it could be that if you don't specify that argument it defaults to a doc format, which then throws the exception that you are trying to save in a different format without explicit instructions. As well another of the arguments is SaveChanges.
So it could be that you are implicitly telling it to save changes in a default doc format, which works in the the doc format, but complains about trying to save it in a different format in the docx format. (understandably)
As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"
However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.
Am I doing something stupid? is there any way to actually disable macros when opening a Word document?
Try:
WordBasic.DisableAutoMacros 1
Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").
I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).
EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx
For c# you can use
(_wordApp.WordBasic as dynamic).DisableAutoMacros();
The whole code I'm using is:
using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
_wordApp = new Word.Application
{
Visible = false,
ScreenUpdating = false,
DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
FileValidation = MsoFileValidationMode.msoFileValidationSkip
};
_wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
(_wordApp.WordBasic as dynamic).DisableAutoMacros();