Unable to set style in active document using a sub procedure

E

eholz1

Hello Group,

I am trying to pass a word doc object, a work app object, a string, and
a count
from my "main" program (in Excel vba) to set a style in my word doc.

I am formatting the word doc, from an excel spread sheet using vba from
the excel side.

here is the code (dStyle = "Header 3")

Sub SetStyle(wApp As Word.Application, dStyle As String)
With wApp.Selection
'.MoveDown Unit:=wdParagraph, Count:=nCount, Extend:=wdMove
.Style = ActiveDocument.Styles(dStyle)
End With
End Sub

If I put this code in-line where the (the main sub) doc and app objects
are defined, it works fine.
It works for the move if I have the .Style commented out. It seems i
cannot set the style from a sub proc. What am I doing wrong??

ewholz
 
J

Jean-Guy Marcil

eholz1 was telling us:
eholz1 nous racontait que :
Hello Group,

I am trying to pass a word doc object, a work app object, a string,
and a count
from my "main" program (in Excel vba) to set a style in my word doc.

I am formatting the word doc, from an excel spread sheet using vba
from the excel side.

I do not see the Word Doc object and the count being passed.
here is the code (dStyle = "Header 3")

Sub SetStyle(wApp As Word.Application, dStyle As String)
With wApp.Selection
'.MoveDown Unit:=wdParagraph, Count:=nCount, Extend:=wdMove
.Style = ActiveDocument.Styles(dStyle)
End With
End Sub

If I put this code in-line where the (the main sub) doc and app
objects are defined, it works fine.
It works for the move if I have the .Style commented out. It seems i
cannot set the style from a sub proc. What am I doing wrong??

SetStyle is in the Excel project, right?
Why are you passing the whole Word Application instead of just a range?

Can you show us how you instantiate the Word Application and the document
object?
How is the "Selection" being determined form Excel?

You should not user the Selection object in code like this (In fact, you
should never use it, except when you have no choice as a few
properties/methods are only available to the Selection object).

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

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