View Full Version : word can't find bookmark
sangeeta
05-27-2005, 04:29 AM
hi all! i am back again! i am trying to use the follwoing piece of code to insert data from excel to a table in word.
when i run this code on a doc containing only a few tables ,it works fine.but when i run it on the actual doc which contains many tables,it inserts data at the wrong place inspite of the bookmark being defined.my guess is that this is because of the hidden bookmarks defined for the table of contents.
here's the code:
Sub insert_data()
Set WordApp = CreateObject("Word.Application")
Dim Rindex, Cindex, i
i = 0
Rindex = 0
With WordApp
.Documents.Open "C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\files\test_doc.doc"
.Visible = True
.WindowState = wdWindowStateMaximize
.Selection.GoTo What:=wdGoToBookmark, Name:=bk1
.Selection.Find.ClearFormatting
While Rindex < 11
Rindex = Rindex + 1
Cindex = 0
While Cindex < 11
Cindex = Cindex + 1
.Selection.TypeText text:=Worksheets("Sheet1").Cells(Rindex, Cindex).Value
.Selection.MoveRight
Wend
.Selection.MoveDown
While i < 11
.Selection.MoveLeft
i = i + 1
Wend
Wend
End With
End Sub
i have attached the doc on which i want this macro to run.
any clues as to how i can set this right?
thanks in advance!
MOS MASTER
05-27-2005, 10:04 AM
Hi, :yes
The navigation on this code is terrible! (No offence)
Please do some tests with the code I provided you for cell navigation.
If you're really have no clue on how to fix it after that then please provide a sample of your Word document allready filled in by the Excel values! (So I can see where you want everyting and if theres logic in there!)
Otherwise the one having no clue is me because your code makes no sence to me. :rofl:
Later...
Killian
05-27-2005, 07:50 PM
Hi sangeeta,
I'm afriad I couldn't make much sense of this either...
A couple of pointers on the code:
Using For countervariable = 0 to 10 would be more efficient for your loops and save you having to initialze and increment them.
Joost makes an important point. Moving around a word doc with the selection and typing should be avoided if at all possible and this is probably the source of your problem.
You may want to consider navigating around each part you want to populate with bookmarks and setting the bookmark's Range.Text property to equal the Excel range.text property (or value) rather that using type text.
(Or use the word doc's tables and paragraphs collections in a similar way).
As indicated, understanding the structure of the output document is the key here.
MOS MASTER
05-28-2005, 09:46 AM
Hi Killian, :yes
I've given Sangeeta a example of filling a table from within Excel with the proper navigation tools in this thread: http://www.vbaexpress.com/forum/showthread.php?t=3336
For him it's vital to get his navigational code up to speeds because it's really important to get his tables filled in a way you no upfront is gona work. (people are going to thank you for it later)
The tables used ar not that complicated but again complicated enough to have a lot going wrong there.
Well lets wait to see the needed output in the Word tables before we can look for a good sollution on this question. :whistle:
sangeeta
05-30-2005, 04:59 AM
hi all!
well i am an infant in vba,so please bear with the terrible piece of code i have sent to u. i agree that it's a bad way of doing things but the problem is not that.the problem is that when i am trying to go to a bookmarked position in a word doc from excel,i am not able to do that.and my gut feeling is that it's because of the structure of my doc which contains a toc. please have a look at this piece of code:
Sub insert_some_text()
Set WordApp = CreateObject("Word.Application")
With WordApp
.Documents.Open "C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\files\test_doc.doc"
.Visible = True
.WindowState = wdWindowStateMaximize
.Selection.GoTo What:=wdGoToBookmark, Name:=bk1
.Selection.Find.ClearFormatting
.Selection.TypeText text:="neena"
End With
End Sub
bare bones-no refences to any cell either in word or in excel.this should work but it's not.
hope i have been able to make myself clear.
thanks a lot!
TonyJollans
05-30-2005, 07:34 AM
Hi sangeeta,
.Selection.GoTo What:=wdGoToBookmark, Name:=bk1
will try to find a bookmark with a name equal to the value of your variable, bk1. You don't have a variable of that name in the posted code so either it's a global variable (in which case is its value correct?) or, what I suspect, it's meant to be a literal and you are trying to find a bookmark actually called "bk1" - if so, put the name in quotes ..
.Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
You can avoid this sort of error by forcing variables to be declared. Just add this line at the beginning of your module (before any Subs or Functions):
Option Explicit
To have it automatically entered for you whenever you create a module, go to Tools > Options > Editor tab and check the "Require Variable Declaration" checkbox.
MOS MASTER
05-30-2005, 09:48 AM
Hi, :yes
In my previous question I've asked you for a filled in example Word document so I can look at it and see where all data should go (Excel), and if there's good logic in there to program your tables in reliable manner.
If you post it I would be happy to start on it as soon as there's time. :whistle:
sangeeta
05-31-2005, 03:00 AM
hi all!
thanks.i had tried that. my doc contains a bookmark named bk1 but when i use
.Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
it says ,"bookmark does not exist".
further,i am attaching an example word doc .the initial tables have ben left blank because that's the data i don't need to change.
any help would be greatly appreciated-i am at my wits' end!
TonyJollans
05-31-2005, 12:07 PM
Hi sangeeta,
Use Option Explicit! Then you won't be allowed to make this type of silly mistake.
You do not have a reference to the Word Object Library and so wdGoToBookmark is not recognised as being the Word constant - it is being treated as an undeclared uninitialised variable thus changing the meaning of the GoTo.
MOS MASTER
05-31-2005, 12:46 PM
Hi sangeeta, :yes
I've got your file now and try to set you up with some code tomorrow! :whistle:
sangeeta
05-31-2005, 10:58 PM
hi Tony!
thanks a ton! it's working now.
thank you very much. i guess i should be able to figure out the rest.
will get back to you guys in case of any problem.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.