PC Review


Reply
Thread Tools Rate Thread

Calling worksheet-level names using insert Autotext in Word

 
 
peturg@gmail.com
Guest
Posts: n/a
 
      2nd Apr 2007
I am trying to using the code below to insert paragraphs into Word
documents based on the information in an Excel worksheets
.....

Dim myWB As Excel.Workbook
Dim myCell as String

Set myWB = GetObject("{path}\filename.xls")
With myWB.Application
.GoTo Reference:="RangeName"
myCell = .ActiveCell.Value

If myCell = "SomeValue" Then
NormalTemplate.AutoTextEntries("CannedText").Insert _
Where:=ActiveDocument.Paragraphs(n).Range
Else
End If

End With
....

The code works with workbook-level names. However, I am having
trouble pointing it to worksheet-level names: changing "RangeName" to
"'Sheet'!RangeName" hasn't worked.

(Ultimately, I want to iterate through RangeName 1-n. I also want to
iterate through sheets, generating and saving one word doc per sheet.)

Do I need to activate the sheet before calling the name? How is that
done? Should I be using a different kind of conditional statement?

Thanks in advance,
Petur G

 
Reply With Quote
 
 
 
 
NickHK
Guest
Posts: n/a
 
      2nd Apr 2007
Petur,
You can loop the different names collection and/or use the Name.Parent to
tell if a name is WB or WS level.

Private Sub CommandButton1_Click()
Dim nam As Name
Dim WS As Worksheet

For Each nam In ThisWorkbook.Names
Debug.Print nam.RefersTo, nam.Parent.Name
Next

For Each WS In ThisWorkbook.Worksheets
For Each nam In WS.Names
Debug.Print nam.RefersTo, nam.Parent.Name
Next
Next

End Sub

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am trying to using the code below to insert paragraphs into Word
> documents based on the information in an Excel worksheets
> ....
>
> Dim myWB As Excel.Workbook
> Dim myCell as String
>
> Set myWB = GetObject("{path}\filename.xls")
> With myWB.Application
> .GoTo Reference:="RangeName"
> myCell = .ActiveCell.Value
>
> If myCell = "SomeValue" Then
> NormalTemplate.AutoTextEntries("CannedText").Insert _
> Where:=ActiveDocument.Paragraphs(n).Range
> Else
> End If
>
> End With
> ...
>
> The code works with workbook-level names. However, I am having
> trouble pointing it to worksheet-level names: changing "RangeName" to
> "'Sheet'!RangeName" hasn't worked.
>
> (Ultimately, I want to iterate through RangeName 1-n. I also want to
> iterate through sheets, generating and saving one word doc per sheet.)
>
> Do I need to activate the sheet before calling the name? How is that
> done? Should I be using a different kind of conditional statement?
>
> Thanks in advance,
> Petur G
>



 
Reply With Quote
 
peturg@gmail.com
Guest
Posts: n/a
 
      2nd Apr 2007
On Apr 2, 12:51 am, "NickHK" <TungChe...@Invalid.com> wrote:
> Petur,
> You can loop the different names collection and/or use the Name.Parent to
> tell if a name is WB or WS level.
>
> Private Sub CommandButton1_Click()
> Dim nam As Name
> Dim WS As Worksheet
>
> For Each nam In ThisWorkbook.Names
> Debug.Print nam.RefersTo, nam.Parent.Name
> Next
>
> For Each WS In ThisWorkbook.Worksheets
> For Each nam In WS.Names
> Debug.Print nam.RefersTo, nam.Parent.Name
> Next
> Next
>
> End Sub
>
> NickHK
>
> <pet...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > I am trying to using the code below to insert paragraphs into Word
> > documents based on the information in an Excel worksheets
> > ....

>
> > Dim myWB As Excel.Workbook
> > Dim myCell as String

>
> > Set myWB = GetObject("{path}\filename.xls")
> > With myWB.Application
> > .GoTo Reference:="RangeName"
> > myCell = .ActiveCell.Value

>
> > If myCell = "SomeValue" Then
> > NormalTemplate.AutoTextEntries("CannedText").Insert _
> > Where:=ActiveDocument.Paragraphs(n).Range
> > Else
> > End If

>
> > End With
> > ...

>
> > The code works with workbook-level names. However, I am having
> > trouble pointing it to worksheet-level names: changing "RangeName" to
> > "'Sheet'!RangeName" hasn't worked.

>
> > (Ultimately, I want to iterate through RangeName 1-n. I also want to
> > iterate through sheets, generating and saving one word doc per sheet.)

>
> > Do I need to activate the sheet before calling the name? How is that
> > done? Should I be using a different kind of conditional statement?

>
> > Thanks in advance,
> > Petur G- Hide quoted text -

>
> - Show quoted text -


Thanks for that suggestion, Nick.

The For Each command worked, but I only want to call a few of the
names.
Conversely, I am not having any luck with Application.GoTo. I've
tried a number of different formats for the reference, and changed the
names from local to global. I just keep getting run-time errors or
object errors.

Any thoughts? Anyone?


 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      3rd Apr 2007
Petur,
It is not normally necessary to .select objects before you use them. For a
WB level name, you can:

myCell = myWB.Range("Rangename").Value

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Apr 2, 12:51 am, "NickHK" <TungChe...@Invalid.com> wrote:
> > Petur,
> > You can loop the different names collection and/or use the Name.Parent

to
> > tell if a name is WB or WS level.
> >
> > Private Sub CommandButton1_Click()
> > Dim nam As Name
> > Dim WS As Worksheet
> >
> > For Each nam In ThisWorkbook.Names
> > Debug.Print nam.RefersTo, nam.Parent.Name
> > Next
> >
> > For Each WS In ThisWorkbook.Worksheets
> > For Each nam In WS.Names
> > Debug.Print nam.RefersTo, nam.Parent.Name
> > Next
> > Next
> >
> > End Sub
> >
> > NickHK
> >
> > <pet...@gmail.com> wrote in message
> >
> > news:(E-Mail Removed)...
> >
> >
> >
> > > I am trying to using the code below to insert paragraphs into Word
> > > documents based on the information in an Excel worksheets
> > > ....

> >
> > > Dim myWB As Excel.Workbook
> > > Dim myCell as String

> >
> > > Set myWB = GetObject("{path}\filename.xls")
> > > With myWB.Application
> > > .GoTo Reference:="RangeName"
> > > myCell = .ActiveCell.Value

> >
> > > If myCell = "SomeValue" Then
> > > NormalTemplate.AutoTextEntries("CannedText").Insert _
> > > Where:=ActiveDocument.Paragraphs(n).Range
> > > Else
> > > End If

> >
> > > End With
> > > ...

> >
> > > The code works with workbook-level names. However, I am having
> > > trouble pointing it to worksheet-level names: changing "RangeName" to
> > > "'Sheet'!RangeName" hasn't worked.

> >
> > > (Ultimately, I want to iterate through RangeName 1-n. I also want to
> > > iterate through sheets, generating and saving one word doc per sheet.)

> >
> > > Do I need to activate the sheet before calling the name? How is that
> > > done? Should I be using a different kind of conditional statement?

> >
> > > Thanks in advance,
> > > Petur G- Hide quoted text -

> >
> > - Show quoted text -

>
> Thanks for that suggestion, Nick.
>
> The For Each command worked, but I only want to call a few of the
> names.
> Conversely, I am not having any luck with Application.GoTo. I've
> tried a number of different formats for the reference, and changed the
> names from local to global. I just keep getting run-time errors or
> object errors.
>
> Any thoughts? Anyone?
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worksheet level names AndreasHermle Microsoft Excel Programming 5 12th Apr 2011 04:45 AM
how to insert workbook names and worksheet names to access columns =?Utf-8?B?Z29rb3A=?= Microsoft Access Queries 12 31st Aug 2007 03:52 PM
How do I insert AutoText in an Excel worksheet? =?Utf-8?B?TWFyaW5hIFouIEhhbGx3b3J0aA==?= Microsoft Excel Misc 2 22nd May 2006 02:27 PM
Duplicating worksheet-level names jmg092548 Microsoft Excel Misc 5 14th Aug 2005 11:11 PM
Worksheet-level names Tony B. Microsoft Excel Worksheet Functions 1 24th Dec 2003 05:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 PM.