Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Open Word with XSL

已查看 3 次
跳至第一个未读帖子

Lawrence007

未读,
2007年11月19日 13:42:012007/11/19
收件人
Hi,

I want to open a XML document with the stylesheet already applied
programmatically. Is that possible? How?

Thanks

willib

未读,
2007年11月20日 00:29:352007/11/20
收件人
Hello

You may add these two programminginstructions to your xml-document

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<?xml-stylesheet type="text/xsl" href="xml2wordml.xslt"?>
<your_xml />

hth
willi

"Lawrence007" <Lawre...@discussions.microsoft.com> schrieb im Newsbeitrag
news:63195B41-A473-4EC8...@microsoft.com...

Lawrence007

未读,
2007年11月20日 07:28:012007/11/20
收件人
willib,

Thanks for the reply. I was not clear in my post yesterday and I apologize.

Can I open XML using Microsoft Word Programmatically? This is what I have
this far:
Dim oWord As Microsoft.Office.Interop.Word.Application = New
Microsoft.Office.Interop.Word.Application
Dim filename As String = Application.StartupPath + "\Output\Output.xml"
Dim missing As Object = System.Reflection.Missing.Value
oWord.Visible = True
oWord.Documents.Open(filename, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing)
oWord.Activate()

I know it is possible, but I don't know how. I can't find anything on
Microsoft explaining how, so any help would be appreciated.

Thanks

Ed

未读,
2007年11月20日 21:28:532007/11/20
收件人
Lawrence when the XSL is added to the schema in question as a solution
in the schema library dialog (from Template and Add-ins dialog) Word
will apply the xslt when opening an xml-doc.

You can also programmatically add the stylesheet to the schema by adding
it to the right collection. (ns = oWord.XMLNamespaces.Add and
NS:XSLTransforms.Add).

Here's a link to a simple tutorial explaining the process in detail (but
you probably already know most of this).

http://www.softwareobjectives.com.au/VoConversion/Article%20Archive/Generating%20business%20documents%20using%20XML%20and%20MS.mht

Cheers,
Ed Richard


"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message
news:854E44FB-A8E9-4BB0...@microsoft.com:

Lawrence007

未读,
2007年11月22日 11:58:012007/11/22
收件人
Ed,

I love the document. Thanks for the reply.

I have another question: Do I HAVE to go to all the PC's that will use the
XSD for that word document I want to create? Can't I do that pragmatically?

Ed

未读,
2007年11月22日 23:11:212007/11/22
收件人
You can add an XSD using Application.XMLNamespaces.Add and add the XSL
by calling Add on the XSLTransforms collection of the object you
previously added.

(
http://msdn2.microsoft.com/de-de/library/microsoft.office.interop.word.xsltransforms.add(VS.80).aspx
)

We have a menu option in our app that reads an XML file containing the
names of our XSD's and XSL's and uses word-automation to update their
local Word schema library.

You could also do it in a macro.

Ed

"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message

news:BB37DD80-8E43-4D46...@microsoft.com:

Lawrence007

未读,
2007年11月24日 10:09:002007/11/24
收件人
Ed,

Thank you very much for your help this far. I loved your article on Word and
XML. It explains in detail the steps to follow. I tried to look on your
website to find the C# example, but with no luck. My application keeps on
crashing. I think it is in my code but have no examples to work of.

Do you know of any? or am I not looking in the right area on your website?

Thanks Ed

Ed

未读,
2007年11月26日 19:00:252007/11/26
收件人
Lawrence,

There's no C# code on my site showing the automation of adding schema's
because we had to do that in a language called Visual Objects, which is
what our primary application is written in.

But if you can show your code here I'm sure we can find what's wrong ;-)

The below C# code basically does it (using VS 2005 and VSTO 2005 SE);

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Word.XMLNamespace ons;
object sname;
sname = "MySchema";
object tname;
tname = "MySchemaXSL 1";
object missing = Type.Missing;
ons = this.Application.XMLNamespaces.Add(@"C:\MySchema.XSD",
ref missing, ref sname, true);
ons.XSLTransforms.Add(@"C:\MyXSL.XSL", ref tname , true);
}


HTH,
Ed

"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message

news:A47196C8-4295-4EBE...@microsoft.com:

Lawrence007

未读,
2007年11月27日 09:03:002007/11/27
收件人
Ed,

This is what I have so far. I am working off your example and will work in
the code you gave me today. The code I am getting errors on is below:

As you can see I have some added functionality to my Word application. I am
restricting the menu and so on. I have tested my xsl file and all works
perfect. If you open the XML file in word it recognize the xsl file and if
you manually apply the xsl file it works perfect, but I do not want my users
to manually apply the xsl file and hence my error.


private object AddXSLT()
{
Microsoft.Office.Interop.Word.XMLNamespace objSchema;
//= "Sample"
//objSchema.Location = "G:\Visual Studio 2005 Projects\XML to Word
HTML\XML to Word HTML\XSDFile\Copy of StylusBill.xsd"
Microsoft.Office.Interop.Word.XSLTransform objTransform;

// objSchema = Microsoft.Office.Interop.Word.XMLNamespace("SimpleSample")
objTransform = objSchema.XSLTransforms.Add("G:\\Visual Studio 2005
Projects\\XML to Word HTML\\XML to Word HTML\\XSL Files\\billres.xsl");
return objTransform;
}

private void Button1_Click(object sender, System.EventArgs e)
{
//Dim File As String = "G:\Testing Output\h13_ih.xml"
//TestingControl1.OpenWord(File)

string FileNameTesting = "G:\\Testing Output\\h13_ih.xml";
try {
Microsoft.Office.Interop.Word.Application oWord = new
Microsoft.Office.Interop.Word.Application();

object missing = System.Reflection.Missing.Value;

oWord.Documents.Open(FileNameTesting, missing, missing, missing,

missing, missing, missing, missing, missing, missing,

missing, missing, missing, missing, missing, AddXSLT);
int counter = oWord.ActiveWindow.Application.CommandBars.Count;
for (int i = 1; i <= counter; i++) {
try {

string nm =
oWord.ActiveWindow.Application.CommandBars(i).Name;
if (nm == "Standard") {

int count_control =
oWord.ActiveWindow.Application.CommandBars(i).Controls.Count;
for (int j = 1; j <= 2; j++) {


oWord.ActiveWindow.Application.CommandBars(i).Controls(j).Enabled = false;
}
}

if (nm == "Menu Bar") {
oWord.ActiveWindow.Application.CommandBars(i).Enabled =
false;
}
nm = "";
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
oWord.Visible = true;
oWord.Activate();
}

catch (Exception ex) {
MessageBox.Show(ex.Message, "Error!!!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}

}

Thank you very much for your help this far.

Lawrence

Lawrence007

未读,
2007年11月28日 12:45:042007/11/28
收件人
Ed,

Using your code I get the following error:
Public member 'XMLSchemaReferences' on type 'ApplicationClass' not found.

Any Ideas?

Thanks

Ed

未读,
2007年12月3日 18:45:522007/12/3
收件人
Sorry was off-line for a while Lawrence,

My code was cut out of a test-project that I created and verified it
worked , note using VSTO 2005 SE and tested on Office 2007. The code
does not have a reference to XMLSchemaReferences so I don't see where
that error would come from?

Have you created a new VSTO- add-in and copied the code I posted in just
for test?

The code you posted has objSchema commented out, you would need that to
be a valid object, now the problem with finding an already added schema
is that the collection only has a get_item method that expects a number,
not very useful, so you will have to find it by name using it's 'URI';
something like this:
.......

// the uri is in the .xsd: xmlns="
http://www.softwareobjectives.com.au/whatever.xsd"
ons =
getbyuri(@"http://www.softwareobjectives.com.au/whatever.xsd");
...........

private Word.XMLNamespace getbyuri(string uri)
{
Word.XMLNamespace oret = null;
foreach (Word.XMLNamespace o in
this.Application.XMLNamespaces)
{
if (o.URI.Equals(uri))
{
oret = o;
}
}
return oret;
}

Ed


"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message

news:56AAE6F5-FC54-4565...@microsoft.com:

Lawrence007

未读,
2007年12月12日 14:36:112007/12/12
收件人
No worries Ed,

Sorry I was off-line now, I had a family emergency to tend to.

Your code helped me tremendously! And I am going to ask one last question if
it is possible.

I can open my word document with XSL changes and so on. I am using a
Winforms application now to open my documents, but it opens the document in a
separate window... Not "inside" my .net application. How can I open the word
document embedded in my .NEt application? If I can do that I will be
extremely great full!

Thanks Ed!


"Ed" wrote:

> Sorry was off-line for a while Lawrence,
>
> My code was cut out of a test-project that I created and verified it
> worked , note using VSTO 2005 SE and tested on Office 2007. The code
> does not have a reference to XMLSchemaReferences so I don't see where
> that error would come from?
>
> Have you created a new VSTO- add-in and copied the code I posted in just
> for test?
>
> The code you posted has objSchema commented out, you would need that to
> be a valid object, now the problem with finding an already added schema
> is that the collection only has a get_item method that expects a number,
> not very useful, so you will have to find it by name using it's 'URI';
> something like this:

> ........


>
> // the uri is in the .xsd: xmlns="
> http://www.softwareobjectives.com.au/whatever.xsd"
> ons =
> getbyuri(@"http://www.softwareobjectives.com.au/whatever.xsd");

> ............

Ed

未读,
2007年12月12日 21:04:182007/12/12
收件人
Hi Lawrence,

I'm not aware of a clean way to 'host' MS-word in a .net application,
I'd be interested in a solution for that as well ;-) What you can do is
have an Internet explorer on a Winform and use that to open a Word
document.

Having said that, I just googled and it seems that this post has pretty
much all of the info we are looking for
http://blogs.msdn.com/johnrdurant/archive/2006/01/23/host-word-winform.aspx

Please let us know here how this works out for you.

Ed


"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message

news:E1479D03-7854-4917...@microsoft.com:

Lawrence007

未读,
2007年12月13日 14:51:022007/12/13
收件人
Ed,

That worked Perfect for me! Word 2003 looks great in that user control, but
2007 will need some work.

Let me know if you want to take a look at it.

Thanks a bunch!

Lawrence

Ed

未读,
2007年12月16日 18:28:452007/12/16
收件人
Yes I sure do,

Let's take this private -> Ed@ _remove_ SoftwareObjectives.com.au

Cheers,
Ed

"Lawrence007" <Lawre...@discussions.microsoft.com> wrote in message

news:4D7A1D2A-6A51-4A35...@microsoft.com:

0 个新帖子