Word Programming - issue with Word versions and automation

A

Aminc

I have a program (DOT) running behind Msword via an application. One of the
automation is to save the active document using MS DOS With Layout format to
retain formatting etcetera. This works fine in Word 97/2000/XP. However, in
Word 2003, use of this converter requires InsertLineBreaks:=True so that line
breaks are inserted (without this a paragraph ends up as a single line in ASC
file).

Does anybody know a workaround method where the same program can work for
all versions without major changes?
 
M

Michael Bednarek

I have a program (DOT) running behind Msword via an application. One of the
automation is to save the active document using MS DOS With Layout format to
retain formatting etcetera. This works fine in Word 97/2000/XP. However, in
Word 2003, use of this converter requires InsertLineBreaks:=True so that line
breaks are inserted (without this a paragraph ends up as a single line in ASC
file).

Does anybody know a workaround method where the same program can work for
all versions without major changes?

Use Application.Build and If or Select Case to code for the different
versions.
 
A

Aminc

Sure. I can do that... but problem is when I use InsertLineBreaks:=True (for
Word 2003), I cannot compile it in Word 2000 or Word 97 because that property
is unavailable.
 
J

Jean-Guy Marcil

Aminc was telling us:
Aminc nous racontait que :
Sure. I can do that... but problem is when I use
InsertLineBreaks:=True (for Word 2003), I cannot compile it in Word
2000 or Word 97 because that property is unavailable.

I think you isunderstood Michael suggestion. He was suggesting womthing
like:

Dim myVer As Long
myVer = Val(Left$(Application.Version, 1))

Select Case myVer
Case 1
MsgBox "You are running Xp or higher."
Case 9 '2000
MsgBox "You are running 2000."
Case 8 '1997
MsgBox "You are running 97."
Case Else
MsgBox "I don't know what you are running."
End Select

Replace the MsgBox with calls to an appropriate Sub.


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

Aminc

Agreed Jean-Guy, but I guess I am not able to explain. Problem is that
InsertLineBreaks argument with the ActiveDocument.SaveAs is unavailable in
versions prior to 2003. So if I code to pass InsertLineBreaks:=True is user
is running say 2000 or 97, I am unable to compile the program.
 
J

Jean-Guy Marcil

Aminc was telling us:
Aminc nous racontait que :
Agreed Jean-Guy, but I guess I am not able to explain. Problem is that
InsertLineBreaks argument with the ActiveDocument.SaveAs is
unavailable in versions prior to 2003. So if I code to pass
InsertLineBreaks:=True is user is running say 2000 or 97, I am unable
to compile the program.

Are you saying that something like this will not work on your machine:

'_______________________________________
Public MyName As String

'_______________________________________
Sub test()

Dim myVer As Long
myVer = Val(Left$(Application.Version, 1))

With ActiveDocument
MyName = Left(.FullName, Len(.FullName) - 4) & "_New.doc"
End With

Select Case myVer
Case 1
WordXpAndMore
Case 8, 9 '97 or 2000
Word2000AndMinus
Case Else
MsgBox "I don't know what you are running."
End Select

End Sub
'_______________________________________

'_______________________________________
Sub Word2000AndMinus()

ActiveDocument.SaveAs FileName:=MyName

End Sub
'_______________________________________

'_______________________________________
Sub WordXpAndMore()

ActiveDocument.SaveAs FileName:=MyName, InsertLineBreaks:=True

End Sub
'_______________________________________

Or are you, like me, one of those people who understands things really
quickly if they are explained long enough? ;-)

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

Aminc

Look. I know how to switch between passing selective arguments based on Word
version. Here is the problem. If I code it like you suggested (I already had
done that before starting this whole communication), and compile in Word2000
or 97, there is a compile error because the InsertLineBreaks argument isn't
available in 2000 or 97. I surely can compile in Word 2003 using this
argument but then cannot run the program in 2000/97 because again
InsertLineBreaks is not available and there is a run time error.
 
E

Edward Thrashcort

Only conditional compilation will do it BUT (unbelievably) there is no
built-in compiler constant for Word version.

Sub ConditionalCompile()
'You cannot declare #Constants dynamically
#Const MyWordVersion = 10


#If MyWordVersion > 9 Then
MsgBox "You are running Xp or higher."
#ElseIf MyWordVersion = 9 Then
MsgBox "You are running 2000."
#ElseIf MyWordVersion = 8 Then
MsgBox "You are running 97."
#Else
MsgBox "I don't know what you are running."
#End If


End Sub


Eddie
 
E

Edward Thrashcort

Burying an invalid statement inside an if statement does NOT get it past the
compiler!

Sub ConditionalCompile()
'You cannot declare #Constants dynamically
#Const MyWordVersion = 10


#If MyWordVersion > 9 Then
MsgBox "You are running Xp or higher."
#ElseIf MyWordVersion = 9 Then
MsgBox "You are running 2000."
#ElseIf MyWordVersion = 8 Then
MsgBox "You are running 97."
#Else
MsgBox "I don't know what you are running."
#End If


End Sub



Eddie
 
J

Jean-Guy Marcil

Aminc was telling us:
Aminc nous racontait que :
Look. I know how to switch between passing selective arguments based
on Word version. Here is the problem. If I code it like you suggested
(I already had done that before starting this whole communication),
and compile in Word2000 or 97, there is a compile error because the
InsertLineBreaks argument isn't available in 2000 or 97. I surely
can compile in Word 2003 using this argument but then cannot run the
program in 2000/97 because again InsertLineBreaks is not available
and there is a run time error.

Sorry about your situation, but, I created the code I posted in a Word 2003
document, saved it and ran the code.
Then, I opened the document with Word 97 and Word 2000 and ran the code
without any problems...

I have done this kind of thing before and never had any problems, so I do
not understand why you are having problems with this.

Conditioning compiling (#If) is normally used for making a difference
between a Mac or a PC machine, but regular conditional branching has always
worked for my purposes. I guess this is why there are no Compiler constants
for Word versions, they are not necessary.

Of course I cannot compile this code under Word 2000 or 97, but why would I
want to? I coded,debugged and compiled the whole thing in Word 2003.
When you run the code, the compiler only compiles the Subs/Functions you
call, this is why I suggested calling the appropriate Sub according to the
version you are running, as in my example.

I mean, the following code will not work:

'_______________________________________
Sub test()

Dim myVer As Long
myVer = Val(Left$(Application.Version, 1))

With ActiveDocument
MyName = Left(.FullName, Len(.FullName) - 4) & "_New.doc"
End With

Select Case myVer
Case 1
ActiveDocument.SaveAs FileName:=MyName, InsertLineBreaks:=True
Case 8, 9 '97 or 2000
ActiveDocument.SaveAs FileName:=MyName
Case Else
MsgBox "I don't know what you are running."
End Select

End Sub
'_______________________________________

But the code I have already posted will.
Why did you not try it?
Or if you did, what kind of error message did you get?

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

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
Only conditional compilation will do it BUT (unbelievably) there is no
built-in compiler constant for Word version.

It would be nice, but I guess there are none becasue they are not necessary
since regular If blocks with calls to different Subs will work.

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

Aminc

OK. Here is the code. When I run it in Word 2000, I get Compile error - Named
argument on found (on InsertLineBreaks:=)

Sub Test()

Dim MyFile As String
Dim iAppversion As Integer

iAppversion = Left$(Application.Version, 2)

Select Case iAppversion
Case 11
Save2K3Document MyFile, wdFormatText ' Save the document in
text format
Case Else
SaveDocument MyFile, FileConverters(iFormat).SaveFormat
End Select

End Sub

Sub Save2K3Document(sDocName As String, Optional iFormat As Integer)
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat,
InsertLineBreaks:=True
End Sub

Sub SaveDocument(sDocName As String, Optional iFormat As Integer)
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat
End Sub
 
E

Edward Thrashcort

Keep up Bond!

We are talking about statements is higher versions of Word causing compiler
errors in earlier versions EVEN when inside regular If blocks

Eddie
 
E

Edward Thrashcort

Can you manually set the compiler variable at module level for each version
of Word that you want to compile in?

If not, you are stuck with distributing three or more versions of your
macro, each with identical #if blocks but with a different compiler constant
in each version.

Eddie
 
J

Jean-Guy Marcil

Aminc was telling us:
Aminc nous racontait que :
OK. Here is the code. When I run it in Word 2000, I get Compile error
- Named argument on found (on InsertLineBreaks:=)

Sub Test()

Dim MyFile As String
Dim iAppversion As Integer

iAppversion = Left$(Application.Version, 2)

Select Case iAppversion
Case 11
Save2K3Document MyFile, wdFormatText ' Save the document in
text format
Case Else
SaveDocument MyFile, FileConverters(iFormat).SaveFormat
End Select

End Sub

Sub Save2K3Document(sDocName As String, Optional iFormat As Integer)
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat,
InsertLineBreaks:=True
End Sub

Sub SaveDocument(sDocName As String, Optional iFormat As Integer)
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat
End Sub


I see some minor problems with your code:
iAppversion = Left$(Application.Version, 2)
will not work with Word 2000 or less because Application.Version will return
"9.0" or "8.0", so you will be trying to assign "9." or "8." to Integer as
per your variable declaration. Also, remember that Application.Version
returns a String, so in effect you are assigning a String to an Integer and
letting the compiler silently doing the conversion, which is not a good
idea, generally speaking..

Also, from your code I cannot tell how/where iFormat is defined, but that
does not matter for the matter at hand.

I tested the following code on a Word 2000 machine and it ran as expected,
i.e it worked, and I tested three times, closing Word in between.

Again, you cannot compile that code on a Word 2000 machine, but you can
execute it without manually compiling because I believe that the compiler
only compiles the Subs/Functions that are executed/called. In this case, on
a Word 200 machine, "Save2K3Document" will not be called, so it is not
compiled. At least this is how I understand it, and the only way I can think
of to account for the fact the code actually runs on Word 2000 or Word 97.

So, either my machine is weird and runs code it should not, or yours is
weird by not running code it should. It would be nice if someone else could
test the code I posted earlier in this thread. I have tried it as is on
three Word versions and it worked (Word 97, Word 200 and Word 2003).


Sub Test()

Dim MyFile As String
Dim iAppversion As Integer

MyFile = ActiveDocument.FullName

iAppversion = Left$(Application.Version, 1)

Select Case iAppversion
Case 11
Save2K3Document MyFile, wdFormatText ' Save the document in Text
Format
Case Else
SaveDocument MyFile, FileConverters(1).SaveFormat
End Select

End Sub

Sub Save2K3Document(sDocName As String, Optional iFormat As Integer)

ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat,
InsertLineBreaks:=True

End Sub

Sub SaveDocument(sDocName As String, Optional iFormat As Integer)

ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat

End Sub


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

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
Keep up Bond!

Was that really necessary?
We are talking about statements is higher versions of Word causing
compiler errors in earlier versions EVEN when inside regular If blocks

I know, have you read the whole thread or just the part where you jumped in?
Just in case, here is what I wrote earlier, complete with all the
syntactical, spelling and grammatical errors:

<quote>
Sorry about your situation, but, I created the code I posted in a Word 2003
document, saved it and ran the code.
Then, I opened the document with Word 97 and Word 2000 and ran the code
without any problems...

I have done this kind of thing before and never had any problems, so I do
not understand why you are having problems with this.

Conditioning compiling (#If) is normally used for making a difference
between a Mac or a PC machine, but regular conditional branching has always
worked for my purposes. I guess this is why there are no Compiler constants
for Word versions, they are not necessary.

Of course I cannot compile this code under Word 2000 or 97, but why would I
want to? I coded,debugged and compiled the whole thing in Word 2003.
When you run the code, the compiler only compiles the Subs/Functions you
call, this is why I suggested calling the appropriate Sub according to the
version you are running, as in my example.

I mean, the following code will not work:

'_______________________________________
Sub test()

Dim myVer As Long
myVer = Val(Left$(Application.Version, 1))

With ActiveDocument
MyName = Left(.FullName, Len(.FullName) - 4) & "_New.doc"
End With

Select Case myVer
Case 1
ActiveDocument.SaveAs FileName:=MyName, InsertLineBreaks:=True
Case 8, 9 '97 or 2000
ActiveDocument.SaveAs FileName:=MyName
Case Else
MsgBox "I don't know what you are running."
End Select

End Sub
'_______________________________________

But the code I have already posted will.
Why did you not try it?
Or if you did, what kind of error message did you get?
</quote>

And the code I refer to from an earlier mesaage is:
<quote>
'_______________________________________
Public MyName As String

'_______________________________________
Sub test()

Dim myVer As Long
myVer = Val(Left$(Application.Version, 1))

With ActiveDocument
MyName = Left(.FullName, Len(.FullName) - 4) & "_New.doc"
End With

Select Case myVer
Case 1
WordXpAndMore
Case 8, 9 '97 or 2000
Word2000AndMinus
Case Else
MsgBox "I don't know what you are running."
End Select

End Sub
'_______________________________________

'_______________________________________
Sub Word2000AndMinus()

ActiveDocument.SaveAs FileName:=MyName

End Sub
'_______________________________________

'_______________________________________
Sub WordXpAndMore()

ActiveDocument.SaveAs FileName:=MyName, InsertLineBreaks:=True

End Sub
'_______________________________________
</quote>

Would you care to test it on various versions of Word? It did work for me on
Word 2003, Word 2000 and Word 97.

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

Aminc

You know what Jean-Guy was right. All I have to do is code for the two
different versions but compile it in Word 2003. It works! both for 2000 and
2003. The only limitation I am stuck with now is that I will always have to
compile this program in 2K3 - no more 2K, but I guess I will have to live
with it.
 
J

Jean-Guy Marcil

Aminc was telling us:
Aminc nous racontait que :
You know what Jean-Guy was right. All I have to do is code for the two
different versions but compile it in Word 2003. It works! both for
2000 and 2003. The only limitation I am stuck with now is that I will
always have to compile this program in 2K3 - no more 2K, but I guess
I will have to live with it.

Phew! I thought I was going nuts in my office, thinking that my computer was
special or something like that!
If you remember, early in the thread, I asked why you wanted to compile code
in 2000 when it contained 2003 stuff

Maybe I am too verbose and you never got to the part where I wrote:

"Of course I cannot compile this code under Word 2000 or 97, but why would I
want to? I coded,debugged and compiled the whole thing in Word 2003."

Or, as I mentioned earlier:
"Or are you, like me, one of those people who understands things really
quickly if they are explained long enough? "

(In case the smiley isn't obvious enough, I am just kidding, I do not mean
to insult you with that comment. I am writing it because, I am truly like
that, sometimes very obvious stuff escapes me because I insist in focusing
on one detail, thereby not seeing the full picture...)

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

Aminc

Yes. I agree that I missed your remark in that email. I guess it was because
I simply could not comprehend not being able to compile in 97/2000 and the
reason is majority of our customers use these two and 2003 is a minority.
There are a lot of occassions where I will have to make minor changes (right
on the user machine) and now I will lose that ability because majority don't
have 2003. Anyway, being able to provide the solution is good enough for me
at this point. Many thanks.
 

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