Unable to set style in active document using a sub procedure  
Author Message
Asmath





PostPosted: Fri Sep 29 12:07:14 CDT 2006 Top

MS Word VBA >> Unable to set style in active document using a sub procedure

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

Word155  
 
 
Jean-Guy





PostPosted: Fri Sep 29 12:07:14 CDT 2006 Top

MS Word VBA >> Unable to set style in active document using a sub procedure 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

Word MVP site: http://www.word.mvps.org