View Full Version : Solved: Bookmarks and text are ok on the first execution, they disappear on the second
silvkhan
10-04-2012, 02:58 AM
http://www.vbaexpress.com/forum/showthread.php?t=43837
The main post is on the forum with the previous link
silvkhan
10-04-2012, 09:41 AM
Here I copy the post
I generate by vba the document and vba inserts some bookmarks for each cell of a table.
- The bookmark must enclose the text coming from a field table rst!subproponente.
- when i open the application access and my beginning form to execute the button command with the following code, all is Ok. I generate the doc with the right bookmarks and the relative text.
- the generated doc is an output, but if I again execute the code with this output doc open, everything is Ok.
- If I close manually this doc and I repeat the code, the bookmarks disappear and with them the text.
- If I close the application access and again open it and repeat the code everything is again OK.
- I don't succeed to understand what to do.
doc2.Documents.Open (rst!subattivita)
doc2.Selection.WholeStory
doc2.Selection.Copy
doc2.Documents.Close
tblTable.Cell(i, 3).Range.PasteAndFormat (wdPasteDefault)
Set rng = tblTable.Cell(i, 3).Range
rng.Select Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertAfter (rst!subproponente) Selection.Font.Bold = True
Selection.Bookmarks.Add "proponente" & i - 1
Frosty
10-04-2012, 12:09 PM
Couple comments:
1. Change your variable name. Your "doc2" variable is clearly an Application object. Change the name to "appWord"
2. Set up a true document object... call it "oDoc" and set it to the result of your .Open function, so that
doc2.Documents.Open (rst!subattivita)
becomes
Set oDoc = appWord.Documents.Open (rst!subattivita)
3. Don't use the Selection object. Use range objects instead. So your next 3 lines
doc2.Selection.WholeStory
doc2.Selection.Copy
doc2.Documents.Close
become
oDoc.Content.Copy
oDoc.Close
4. Post more code. Your description of the problem in the other post seems to indicate that you have more than one instance of Word running (which is finally "released" when Access is closed). We need to see how you create the WINWORD.EXE process.
silvkhan
10-04-2012, 02:40 PM
-The aim of my application is: to generate automatically a maxi word doc
assembled from many subdoc
-The maxi has in the first page: an index with data coming from a database
in the next pages: a table with data coming from a database
in the third column of the table:
each cell contains:
the formatted text pasted from one word subdoc
(which the code open, copy, paste)
(the database contains the pathname to the subdoc)
at the bottom there is the signature of that doc
(coming from a field of the database)
-When the user get the generated maxi, she can edit it.
after editing, the code must update the subdoc
- and the code makes the inverse operation: copy from each cell the content and paste it in the subdoc.
But there is the problem of the signature coming from the database. The signature for each subdoc must be on the subdoc on the maxi not on the single word subdocs.
The solution is to mark it by a bookmark which is permanent. So when I update the subdoc, I delete the text with the bookmarks and everything is Okay. But the Devil is on the details as you know.
silvkhan
10-04-2012, 02:58 PM
Hi,
I changed:
doc2.selection.WholeStory
doc2.selection.copy --> to oDoc.content.copy
but when i paste with the instruction:
tblTable.Cell(i, 3).Range.PasteAndFormat (wdPasteDefault)
i don't get the right text.
----------------------------------------------------------
I have only one instance of word. I create winword.exe as you told me: by getobject... ( I controlled the task manager, I have one only word instance in the processes). The problem has emerged when i added the code to create the bookmark. I made many different instructions, at the end i found those in the main post. I tried the range without success. I tried to collapse the range without success.
When I finish to generate the maxi word doc I must leave the doc open for the user.
silvkhan
10-05-2012, 01:16 PM
Hi, Frosty
here is the fragment of the code: the part which functions first time and not the second is relative to the bookmarks (before I inserted these instructions of the bookmarks, everything was Okay)
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set appWord = CreateObject("Word.Application")
End If
On Error GoTo 0
appWord.Visible = True
Set doc = appWord.Documents.Add
appWord.Documents.Open (rst!subattivita)
' I select the subdoc, copy it, paste, and close
appWord.Selection.WholeStory
appWord.Selection.Copy
Dim nome1 As String
nome1 = ultimastringa(rst!subattivita)
appWord.Application.Documents(nome1).Close SaveChanges:=wdDoNotSaveChanges
tblTable.Cell(2, 3).Range.PasteAndFormat (wdPasteDefault)
Set rng = tblTable.Cell(2, 3).Range
'rng.Collapse Direction:=(wdCollapseStart)
rng.Select
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertAfter (rst!subproponente)
Selection.Font.Bold = True
Selection.Bookmarks.Add "proponente" & 1
fumei
10-05-2012, 07:09 PM
You appear to be using Set for doc...but never using it.
tblTable.Cell(2, 3).Range.PasteAndFormat (wdPasteDefault) needs to be qualified to something.
Not sure what you are doing with any bookmarks.
silvkhan
10-06-2012, 06:09 AM
The code shown is only an extract of all the code.
There is also the instruction:
Set tblTable = doc.Tables.Add(Range:=myRange, numrows:=maxposizione + 1, numcolumns:=3)
The bookmarks are useful for, after the code generated the maxi doc, the user can edit it, and other code updates the subdocuments starting from the maxi. I do two operations, the first is linking together word docs (subDocuments) to create the maxi word doc. The second operation is inverse: I update the subDocuments starting from the editing of the maxi.
Why the bookmarks ? When I create the maxi, I enter the formatted text of each subdocuments in each cell of a table of the maxi. But at the bottom of this text I insert an other text (the signature) coming from a database.
I want to bookmark this text, to recover it (to distinguish it from the other text) when I do the inverse operation of updating the subDocuments.
The question is that the first time I execute the code I find the correct bookmarks inserted into my maxi document; and until I leave this doc open, and I repeat the generation the bookmarks are generated correctly, but as soon as I close the doc, and I repeat the generation the bookmarks disappear.
silvkhan
10-06-2012, 08:26 AM
I make the debug of the previous code using also the sub DebugshowRange of the book "writing Word Macros" Oreilly, Steven Roman, page 218 to show blinking the range. I get the following results:
1) first time execute the program (without previous word doc open) the wright range blink and there is the cursos at the bottom of the cell where it must be (for I insertAfter some text)
2) I leave open the first doc generated and I repeat the code. All is going Ok like before.
3) I open a blank word doc, I leave the previous doc generated open, I repeat Ok.
4) I close the generated doc leaving the blank doc open, I repeat and Ok
5) I close all the word doc and I repeat, I see all the cell selected and blinking I don't see the cursor, and obviously when I insertAfter the text, this text is hidden ( i don't see it). Problem.
6) I repeat with an open doc as in 3) , but I get the same 5) result, there is no more the cursor on the range selected.
7) now Each time I repeat with previous doc open or closed, I get always the same wrong result, there is no more the cursor.
8) To reset this situation and restore the correct one, I must close Access Application
9) when I enter again in Access and I launch my code The history repeats starting from point 1).
I ask if there is some people who can tell me what is the solution of this problem. Thank you very much.
silvkhan
10-06-2012, 10:54 AM
Good news. I made the following job. I have coded a second program much more smaller than the true, but with the main code: here is
Private Sub cmdBookmarks_Click()
Dim appWord As Word.Application
Dim maxiDoc As Word.Document
Dim subDoc As Word.Document
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strsql As String
Dim numberRows As Integer
Dim myRange As Range
Dim nome As String
Dim rng As Range
Dim i As Integer
Dim tblTable As Table
Set conn = CurrentProject.Connection
Set rst = New ADODB.Recordset
' I read the database table with the pathnames of word subDocs
strsql = "Select subattivita,subproponente from sub"
'the field subattivita is the pathname of the word subDoc document
'the field subproponente is the text I want insert after the formatted text of a subDoc
rst.CursorLocation = adUseClient
rst.Open strsql, conn, adOpenForwardOnly, adLockReadOnly
numberRows = rst.RecordCount
'-------------------------------------------------------------------------------
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set appWord = CreateObject("Word.Application")
End If
On Error GoTo 0
appWord.Visible = True
'this is the document maxi empty at the beginning
Set maxiDoc = appWord.Documents.Add
'--------------------------------------------------------------------------------
Set myRange = maxiDoc.Range(Start:=0, End:=0)
Set tblTable = maxiDoc.Tables.Add(Range:=myRange, numRows:=numberRows + 1, numcolumns:=1)
tblTable.PreferredWidthType = wdPreferredWidthPercent
tblTable.PreferredWidth = 100
' now I iterate recordset rst and for each I open the subDocs word document
i = 1
Do Until rst.EOF
appWord.Documents.Open (rst!subattivita)
'seleziono tutto il documento e lo copio negli appunti e poi lo chiudo
appWord.Selection.WholeStory
appWord.Selection.Copy
nome = ultimastringa(rst!subattivita)
appWord.Application.Documents(nome).Close SaveChanges:=wdDoNotSaveChanges
tblTable.Cell(i, 1).Range.PasteAndFormat (wdPasteDefault)
' to collapse the range and let it becoming a cursor at the cell bottom
Set rng = tblTable.Cell(i, 1).Range
rng.Select
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertAfter (rst!subproponente)
Selection.Font.Bold = True
Selection.Bookmarks.Add "proponente" & i
i = i + 1
rst.MoveNext
Loop
End Sub
Compiling this code I got ther runtime error 462, which I know very well.
And really on the instructions Selection...
So I understand what to do to reference by the object appword.Selection.
I changed the code on my big program where the compiler didn't give me that error, but the unusual behaviour which i segnaleted, anderything now is Okay!!!!
The right code is
appWord.Selection.EndKey Unit:=wdLine
appWord.Selection.MoveLeft Unit:=wdCharacter, Count:=1
appWord.Selection.InsertAfter (rst!subproponente)
appWord.Selection.Font.Bold = True
appWord.Selection.Bookmarks.Add "proponente" & i
Thank you very much
bye Silvano
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.