Discussion:
VB6 to MS WORD help
(too old to reply)
mkol
2011-04-03 15:17:22 UTC
Permalink
Hi;
I am trying to use MS Word as a reporting tool thru VB6. I have a problem
with
getting the 'superscripts and subscripts'. What I have so far is not good.
I would be grateful if anyone has a 'Micro to do this' and can post it or
send it to me -or can direct me to a web site to download something that
will help me.

I would also be interested in third party tools at a reasonable price.

Regards,
M. Kol
ralph
2011-04-03 17:32:32 UTC
Permalink
Post by mkol
Hi;
I am trying to use MS Word as a reporting tool thru VB6. I have a problem
with
getting the 'superscripts and subscripts'. What I have so far is not good.
I would be grateful if anyone has a 'Micro to do this' and can post it or
send it to me -or can direct me to a web site to download something that
will help me.
Have no idea what problems you are having so unable to provide any
specific information. However, the following hints may help.

The application (in this case Word) Object Model exposed through
Automation is identical to the Objects that are accessed when you
create a Macro from within Word.

One quick way to discover how to do something is simply create a Macro
in Word that does what you want to do, and then save it as a VBA
module. You can use Keystrokes, or code you can download from the many
Word Macro sites. Use that code as a guide of how to perform the task
from VB through Automation. (In many cases just copy 'n paste.).

One warning. Most of the time the generated code, especially through
Keystrokes, is rather verbose and less than optimal. Works fine for an
'in-word' macro, but not for VB code. So you may need to do a bit of
massage.

-ralph
Tom Shelton
2011-04-03 18:10:41 UTC
Permalink
Post by mkol
Hi;
I am trying to use MS Word as a reporting tool thru VB6. I have a problem
with
getting the 'superscripts and subscripts'. What I have so far is not good.
I would be grateful if anyone has a 'Micro to do this' and can post it or
send it to me -or can direct me to a web site to download something that will
help me.
I would also be interested in third party tools at a reasonable price.
Regards,
M. Kol
If you're talking about creating documents, and your dealing with 2007
and above - those documents are just zip files with xml inside. I
generate excel 2007 doc all the time - it's just a matter of knowing
the right xml and directory structure then zipping it up.
--
Tom Shelton
mkol
2011-04-03 22:24:56 UTC
Permalink
Mr. Shelton
:I appreciate your interest in helping me. It looks like I wasn't clear in
my posting.
Please take a look at my response to get some idea of what I am trying to
do. My problem
is with MS WORD.
Regards,
M. Kol
Post by mkol
Hi;
I am trying to use MS Word as a reporting tool thru VB6. I have a problem
with
getting the 'superscripts and subscripts'. What I have so far is not good.
I would be grateful if anyone has a 'Micro to do this' and can post it or
send it to me -or can direct me to a web site to download something that
will help me.
I would also be interested in third party tools at a reasonable price.
Regards,
M. Kol
If you're talking about creating documents, and your dealing with 2007 and
above - those documents are just zip files with xml inside. I generate
excel 2007 doc all the time - it's just a matter of knowing the right xml
and directory structure then zipping it up.
--
Tom Shelton
ralph
2011-04-04 03:03:44 UTC
Permalink
Post by mkol
Mr. Shelton
:I appreciate your interest in helping me. It looks like I wasn't clear in
my posting.
Please take a look at my response to get some idea of what I am trying to
do. My problem
is with MS WORD.
Regards,
M. Kol
Actually Mr. Selton WAS addressing your post by suggesting an
alternative method of producing a Word document other than using
Automation.

Since Word 2007 the file format is XML instead of a proprietary
'binary' format, so in practice it be easier to write out XML text.
However, that requires yet another skillset - "a matter of knowing the
right xml and directory structure". There are a few resources to help
you get started, but mostly you'll be writing a lot of 'sample'
documents and associated macros and then peeking at what is generated.
Thus no matter what what you decided to do, getting familar with the
Object Model is necessary.

-ralph
mkol
2011-04-03 20:29:20 UTC
Permalink
Ralph;
Thank you for your quick response .What I am trying to to do is write some
math equations
say any equation...exmp ..equation$="y=a^bv*vcv*vdv2") (^=superscript ,,
v=subscript)
WordApp.Selection.TypeText Text:=pEqW(WordApp, "y=a^bv*vcv*vdv2")

Here is the function that does't work properly - including fonts, size-

Function pEqW(WordApp As Word.Application, ByVal equation As String)

' Dim rText As Range
Dim ch As String
Dim ixx As Long
'On Error GoTo pEqW_Error

WordApp.ActiveDocument.Select
With WordApp.Selection
.ClearFormatting
.Font.Name = "CourierNew"
.Font.Size = 10
.Font.Bold = False

For ixx = 1 To Len(equation)
ch = Mid$(equation, ixx, 1)
If ch = "^" Then
.Font.Size = 30
'.Font.Subscript = True
'.InsertBefore "^"
.Font.Superscript = True
.InsertAfter "^"
'.TypeText Text:=Mid$(equation, ixx + 1, 1)
ElseIf ch = "v" Then
.Font.Size = 30
'.Font.Superscript = True
.Font.Subscript = True
' '.MoveRight Unit:=wdCharacter, Count:=1
.InsertAfter "v"
ElseIf ch = " " Then
Else
'.TypeText Text:=ch
.Font.Size = 20
'.TypeText Text:=ch 'Mid$(equation, ixx, 1) '
'.Font.Subscript = False
'.Font.Superscript = False
.TypeText Text:=ch 'Mid$(equation, ixx, 1) '
.Font.Subscript = False
.Font.Superscript = False
End If
.Font.Subscript = False
.Font.Superscript = False
Next ixx
End With

WordApp.Selection.Font.Subscript = False
WordApp.Selection.Font.Superscript = False
WordApp.Selection.Font.Size = 8
WordApp.Selection.Font.Bold = False
'On Error GoTo 0
Exit Function
~~~~~~~~~~~~~~~~~~
Ralph,
Again Thanks,
M. Kol
Post by mkol
Hi;
I am trying to use MS Word as a reporting tool thru VB6. I have a problem
with
getting the 'superscripts and subscripts'. What I have so far is not good.
I would be grateful if anyone has a 'Micro to do this' and can post it or
send it to me -or can direct me to a web site to download something that
will help me.
I would also be interested in third party tools at a reasonable price.
Regards,
M. Kol
ralph
2011-04-04 03:37:14 UTC
Permalink
Post by mkol
Ralph;
Thank you for your quick response .What I am trying to to do is write some
math equations
say any equation...exmp ..equation$="y=a^bv*vcv*vdv2") (^=superscript ,,
v=subscript)
WordApp.Selection.TypeText Text:=pEqW(WordApp, "y=a^bv*vcv*vdv2")
Here is the function that does't work properly - including fonts, size-
Function pEqW(WordApp As Word.Application, ByVal equation As String)
' Dim rText As Range
Dim ch As String
Dim ixx As Long
'On Error GoTo pEqW_Error
WordApp.ActiveDocument.Select
With WordApp.Selection
.ClearFormatting
.Font.Name = "CourierNew"
.Font.Size = 10
.Font.Bold = False
For ixx = 1 To Len(equation)
ch = Mid$(equation, ixx, 1)
If ch = "^" Then
.Font.Size = 30
'.Font.Subscript = True
'.InsertBefore "^"
.Font.Superscript = True
.InsertAfter "^"
'.TypeText Text:=Mid$(equation, ixx + 1, 1)
ElseIf ch = "v" Then
.Font.Size = 30
'.Font.Superscript = True
.Font.Subscript = True
' '.MoveRight Unit:=wdCharacter, Count:=1
.InsertAfter "v"
ElseIf ch = " " Then
Else
'.TypeText Text:=ch
.Font.Size = 20
'.TypeText Text:=ch 'Mid$(equation, ixx, 1) '
'.Font.Subscript = False
'.Font.Superscript = False
.TypeText Text:=ch 'Mid$(equation, ixx, 1) '
.Font.Subscript = False
.Font.Superscript = False
End If
.Font.Subscript = False
.Font.Superscript = False
Next ixx
End With
WordApp.Selection.Font.Subscript = False
WordApp.Selection.Font.Superscript = False
WordApp.Selection.Font.Size = 8
WordApp.Selection.Font.Bold = False
'On Error GoTo 0
Exit Function
~~~~~~~~~~~~~~~~~~
Ralph,
Again Thanks,
M. Kol
Hopefully Mr. Shelton will be back with a response. He is one of the
Word gurus around here. (and I'm not set up to test your code atm)

But at first glance it appears you are not chewing on what you think
you are, or rather what you think you have 'selected' compared to what
Word thinks is currectly 'selected'.

Here is what I would try (it is essentially what I would do if I was
setup to test your code).
Open Word
Create a paragraph/panel or object where you want the result to
appear.
Create another dialog or paragraph/panel/object and enter the
characters as they would be delivered by VB in that panel.
Select the incoming text then create a keystroke macro to make the
format changes. That is likely going to take a while to get right.
Output the result to a separate buffer.
Then paste to where you want it.
(Most math/graphics translators do work with a separate buffer to
avoid confusing the intial selection with temp selections and final
results).

After that you can optimize it for your VB6 application.

If you intend to use a lot of math nomenclature/symbols etc. and you
aren't in the mood to become a Word Macro/Automation guru <g> there
are several 3rd party toys out there that will help. Some are limited
'free' tools, others are amazingly pricey.

MS Word comes with the Microsoft Equation Editor. I have no idea if it
can be automated or not, but might be worth looking into.

-ralph

Loading...