Word automation different when word is hidden

C

Claus Nielsen

Hello all

I'm having a problem with automating word on the .NET platform with C#.

Using the command:
oDoc = oWordApplic.Documents.Open(ref fileName, 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,
ref missing);

.... you can set if you want to be visible using the object isVisible = true
or false.
But when Word is hidden, the width of the tables is not the same, as when
Word is visible.

Does anyone know anything that can help me, or get me in contact with
someone somewhere?

Hope someone can help! Pretty urgent..

Regards
Claus Nielsen
 
J

Jean-Guy Marcil

Claus Nielsen was telling us:
Claus Nielsen nous racontait que :
Hello all

I'm having a problem with automating word on the .NET platform with
C#.
Using the command:
oDoc = oWordApplic.Documents.Open(ref fileName, 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, ref missing);

... you can set if you want to be visible using the object isVisible
= true or false.
But when Word is hidden, the width of the tables is not the same, as
when Word is visible.

???
Without knowing what it is you are doing, it is difficult to comment on this
particular problem.

If you are using the Selection object, then it is perfectly normal that the
result is different if Word is visible or not.
Try using not the Range object.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Claus Nielsen

Hello Jean, thank you for posting!

I seem to have a problem doing what you suggested. I use the .Selection
property to select Tables.Add.
Tables.Add method is described in VS.NET like this:
Tables.Add(Word.Range Range, int NumRows, int NumColumns, ref object
DefaultTableBehavier, ref object AutoFitBehavier)

.... so I don't see how I can avoid using a range property, when the
Tables.Add method take a Word.Range parameter as the first argument?

This is translated from VB makro:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed

Here is my C#:
object defaultTableBehavior =
Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
activeTable = oWordApplic.Selection.Tables.Add(rng, 1, 2, ref
defaultTableBehavior, ref autoFitBehavior);

Can you help?

I can send you my class which I've devoloped for easier progress...

Regards
Claus Nielsen
 
J

Jean-Guy Marcil

Claus Nielsen was telling us:
Claus Nielsen nous racontait que :
Hello Jean, thank you for posting!

I seem to have a problem doing what you suggested. I use the
.Selection property to select Tables.Add.
Tables.Add method is described in VS.NET like this:
Tables.Add(Word.Range Range, int NumRows, int NumColumns, ref object
DefaultTableBehavier, ref object AutoFitBehavier)

... so I don't see how I can avoid using a range property, when the
Tables.Add method take a Word.Range parameter as the first argument?

Sorry, my last sentence should have read:
Try using the Range object.
instead of
Try using not the Range object.
This is translated from VB makro:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1,
NumColumns:= _ 3, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _ wdAutoFitFixed

Range:=Selection.Range,
can also be:
Range:=ActiveDocument.Paragraphs(1).Range,
for example, or any other valid range.
Of course, you can set the range to be the current cursor location with
Range:=Selection.Range
Or assign that range to a range variable at the begining of the code, and
use this range variable in the Table.Add argument list.
Here is my C#:
object defaultTableBehavior =
Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
activeTable = oWordApplic.Selection.Tables.Add(rng, 1, 2, ref
defaultTableBehavior, ref autoFitBehavior);

Can you help?

I do not know enough about C# sharp to comment on that code...

Only a tentative suggestion:
Can
activeTable = oWordApplic.Selection.Tables.Add(rng, 1, 2, ref
defaultTableBehavior, ref autoFitBehavior);
be
activeTable = oWordApplic.oDoc.Tables.Add(rng, 1, 2, ref
defaultTableBehavior, ref autoFitBehavior);

Where oDoc would be Document variable previously assigned to the active
document?
Would that help?

Hopefully, now that you have posted the C# code, someone will be along
shortly to assist you directly with that code.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Claus Nielsen

Hello again Jean

Thanks very much for your reply, and now it makes just a little bit more
sense :)

.... but unfortunatly it did not help. I already use the:
Word.Range rng = oWordApplic.Selection.Range;
.... so that would be the same as you suggested, right?

I tried using the:
Word.Range rng = oDoc.Paragraphs(1).Range;
... but that just fucked up my document, and got some serius COM errors in my
eventlog...

I changed the oWordApplic.Selection.Tables.Add to oDoc.Tables.Add.

This is my full function which inputs a table:
public void insertQuestionListTable()
{
Word.Range rng = oWordApplic.Selection.Range;
// Add the table.
object defaultTableBehavior =
Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
activeTable = oDoc.Tables.Add(rng, 1, 3, ref defaultTableBehavior, ref
autoFitBehavior);
object style = "QuestionList";
activeTable.set_Style(ref style);
activeTable.PreferredWidthType =
Word.WdPreferredWidthType.wdPreferredWidthPercent;
activeTable.PreferredWidth = 100;
}

This creates a table as converted from VBA. But unfortunatly when I input
text and set columns width, It fucks up my table:
http://www.daimi.au.dk/~cln/errorPicture.JPG

This is what it should look like:
http://www.daimi.au.dk/~cln/correctPicture.JPG

Do you have any other suggestions? I'm getting kind of desperate. I even
tried just created a macro, and then call it from my app, but that didn't
work either. :(

Can you help?

Thanks !

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top