Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default make a "selection" in a word document from a module in excel

Hello Group,

I am trying to add bookmarks and text to a word doc. I can add a
bookmark using code from Excel like this:

Set wdApp = CreateObject("Word.Application")
Set myDoc = wdApp.Documents.Add(Template:=strDocumentNamePath)
With wdApp
..Visible = True
..WindowState = wdWindowStateMinimize 'wdWindowStateMaximize
End With

With wrdDoc.Bookmarks
.Add Name:=bName, Range:=rng
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

The way that works in a word module works fine in word, but not when I
try to do the same thing from excel. the word code is below:
'With ActiveDocument.Bookmarks
'.Add Range:=Selection.Range, Name:="TestBookMark"
'.DefaultSorting = wdSortByName
'.ShowHidden = False
'End With

The problem I am having is understanding how to acess/define the word
document property/method (whichever it is) "Selection.Range" or
Selection.xxx in the module in my excel code. I know that the
"Selection" is local to the word document, but do not understand how to
do a "selection" from my excel module.

any info will be greatly appreciated.

thanks

eholz1

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 163
Default make a "selection" in a word document from a module in excel

Hi eholz1,

how do you define rng in your code?

This is a simple example of setting bookmarks
to the first three words in a word doc.
The bookmarks are Mark1, Mark2, Mark 3.

Sub BookmarkinWord()
' early binding assumed
Dim WdApp As Word.Application
Dim MyDoc As Word.Document
Dim rTmp As Word.Range
Dim lCnt As Long
Set WdApp = CreateObject("Word.Application")
Set MyDoc = WdApp.Documents.Open("c:\test\lazy.doc")
Set rTmp = MyDoc.Range
With WdApp
..Visible = True
..WindowState = wdWindowStateMinimize 'wdWindowStateMaximize
End With
For lCnt = 1 To 3
With MyDoc.Bookmarks
.Add name:="Mark" & CStr(lCnt), _
Range:=MyDoc.Words(lCnt)
End With
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

  #3   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default make a "selection" in a word document from a module in excel

You are much better off avoiding the Selection object in Word, especially
when you are accessing the doc from Excel. You will do much better to set a
range (and it must be Dim'd as Word.Range when you are writing in Excel
VBA). The Range can be anywhere in the doc you define it to be, regardless
of where the cursor is; the Selection is going to be only where the
insertion point or highlight is. The same goes for ActiveDocument - you are
setting a Document object to call the doc and open it; why not use that for
working in the doc?

BTW, in the code you showed us, you have
Set myDoc = wdApp.Documents.Add(Template:=strDocumentNamePath)

and
With wrdDoc.Bookmarks


Ed

"eholz1" wrote in message
oups.com...
Hello Group,

I am trying to add bookmarks and text to a word doc. I can add a
bookmark using code from Excel like this:

Set wdApp = CreateObject("Word.Application")
Set myDoc = wdApp.Documents.Add(Template:=strDocumentNamePath)
With wdApp
.Visible = True
.WindowState = wdWindowStateMinimize 'wdWindowStateMaximize
End With

With wrdDoc.Bookmarks
.Add Name:=bName, Range:=rng
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

The way that works in a word module works fine in word, but not when I
try to do the same thing from excel. the word code is below:
'With ActiveDocument.Bookmarks
'.Add Range:=Selection.Range, Name:="TestBookMark"
'.DefaultSorting = wdSortByName
'.ShowHidden = False
'End With

The problem I am having is understanding how to acess/define the word
document property/method (whichever it is) "Selection.Range" or
Selection.xxx in the module in my excel code. I know that the
"Selection" is local to the word document, but do not understand how to
do a "selection" from my excel module.

any info will be greatly appreciated.

thanks

eholz1



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Browse a multi-pages "microsoft word document" object in Excel 200 dapanpan Excel Discussion (Misc queries) 0 March 22nd 08 06:20 AM
How do I move a document from "recent items" to "documents" John Gerke in Central Oregon New Users to Excel 1 March 2nd 08 08:31 AM
If changed array formula reduce ""\""\""\ - signs to #Missing, will it make ... Maria J-son[_2_] Excel Programming 2 March 5th 06 12:20 PM
how can I make an excel cell "mark" or "unmark" when clicked on? Rick Excel Discussion (Misc queries) 6 January 8th 06 10:15 PM


All times are GMT +1. The time now is 11:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"