Winword visible=false problem

M

mpalma

Hi

I'm trying to open, process and close a document in Winword using a VB application. It work just fine when I use "oWordApp.Visible=True" but it doesn't when I try "oWordApp.Visible=False"
As far as I can tell, when I'm moving around the document, "oWordApp.Selection.Text" does not return the same string in both cases. That is, it just work correctly in the "oWordApp.Visible=True" cenario

Anybody had the same, or a similar problem

Thanks in advance
Miguel Palma
 
J

Jonathan West

When the application is not visible, you should use Range objects rather
than the Selection for moving round the document.

If you can show some sample code, I'll show you how to convert it to using
ranges.

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup



mpalma said:
Hi.

I'm trying to open, process and close a document in Winword using a VB
application. It work just fine when I use "oWordApp.Visible=True" but it
doesn't when I try "oWordApp.Visible=False".
As far as I can tell, when I'm moving around the document,
"oWordApp.Selection.Text" does not return the same string in both cases.
That is, it just work correctly in the "oWordApp.Visible=True" cenario.
 
J

Jonathan West

Unfortunately, you have hot on onw of the few things which a Selection is
really needed for - navigating by lines.

Is each line a separate paragraph? If so, I could do a per-paragraph
equivalent?

Also, since this is code controlling Word from outside, could you include
the code where you declare and initialise oWordApp and open the document? I
need to see whether you are using early or late binding

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup


mpalma said:
Thank you Jonathan.

Here is an example of the code:

oWordApp.Selection.HomeKey wdStory
oWordApp.Selection.MoveUp wdLine
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
oWordApp.Selection.Delete
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
If InStr(oWordApp.Selection.Text, "-") > 0 Then
oWordApp.Selection.HomeKey wdLine
While oWordApp.Selection.Text <> "("
oWordApp.Selection.Delete
Wend
oWordApp.Selection.Delete
While Right(oWordApp.Selection.Text, 1) <> ")"
oWordApp.Selection.MoveRight , , wdExtend
Wend
oWordApp.Selection.MoveLeft , , wdExtend
Else
While InStr(oWordApp.Selection.Text, " ") > 0
oWordApp.Selection.MoveLeft , , wdExtend
Wend
End If
lngPagina = Val(oWordApp.Selection.Text)
oWordApp.Selection.MoveLeft
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
oWordApp.Selection.Delete

This is a the range equivalent

Dim wrdRange as Object
Set wrdRange = oWordApp.ActiveDocument.Range(0, 0)
oWordApp.Selection.HomeKey wdStory
oWordApp.Selection.MoveUp wdLine
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
oWordApp.Selection.Delete
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
If InStr(oWordApp.Selection.Text, "-") > 0 Then
oWordApp.Selection.HomeKey wdLine
While oWordApp.Selection.Text <> "("
oWordApp.Selection.Delete
Wend
oWordApp.Selection.Delete
While Right(oWordApp.Selection.Text, 1) <> ")"
oWordApp.Selection.MoveRight , , wdExtend
Wend
oWordApp.Selection.MoveLeft , , wdExtend
Else
While InStr(oWordApp.Selection.Text, " ") > 0
oWordApp.Selection.MoveLeft , , wdExtend
Wend
End If
lngPagina = Val(oWordApp.Selection.Text)
oWordApp.Selection.MoveLeft
oWordApp.Selection.MoveDown wdLine, 1, wdExtend
oWordApp.Selection.Delete
 
M

mpalma

Hi again Jonathan. Thank you very much for your support

Yes, I have a different paragraph for each line

The code that you request
Dim oWordApp As Word.Applicatio
Set oWordApp = New Word.Applicatio
oWordApp.Visible = [True | False
oWordApp.Documents.Open (mstrFileInput

Can you please explain what is early and late binding

Thank you again Jonathan

Miguel
 
J

Jonathan West

mpalma said:
Hi again Jonathan. Thank you very much for your support.

Yes, I have a different paragraph for each line.

The code that you request:
Dim oWordApp As Word.Application
Set oWordApp = New Word.Application
oWordApp.Visible = [True | False]
oWordApp.Documents.Open (mstrFileInput)

Can you please explain what is early and late binding?

Early binding os the code as you have it, where you have set a reference to
the Word object model in your project references.

Late binding is where you do something like this

Dim oWordApp As Object
Set oWordApp = CreateObject("Word.Application")

The reason it is called late binding is because the program doesn't know
until it is run what kind of object is going to get loded into oWordApp - it
is defined as Object

As for the rest of your code from the earlier message, here is how I would
proceed. I haven't done a line-by-line conversion of your macro, because it
became clear to me that all the movibng of the selection this way and that
was for the purpose of extracting a specific string from within the
paragraph. It is much quicker to get the whole of the paragraph into a
string and then get the relevant bit out of the string. This is what I have
done.

Dim oWordApp As Word.Application
Dim oDoc as Word.Document

Set oWordApp = New Word.Application
oWordApp.Visible = [True | False]
Set oDoc = oWordApp.Documents.Open(mstrFileInput)

Dim myRange as Range
Dim strPagina as String

oDoc.Paragraphs(1).Range.Delete

Set myRange = oDoc.Paragraphs(1).Range
strPagina = myRange.Text

If InStr(strPagina, "-") > 0 Then
strPagina = Mid$(strPagina, Instr(strPagina, "(") + 1)
strPagina = Left$(strPagina, Instr(strPagina, "(") - 1)

Else
strPagina = Left$(strPagina, Len(strPagina) - 1)
strPagina = RTrim$(strPagina)
End If
lngPagina = Val(strPagina)
myRange.Delete
Set myRange = oDoc.Paragraphs(1).Range
 

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