View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.charting,microsoft.public.excel.programming,microsoft.public.office.developer.vba,microsoft.public.word.tables,microsoft.public.word.vba.general
hornbecky83 hornbecky83 is offline
external usenet poster
 
Posts: 11
Default Naming word tables or coding reference library with vba

Hi-

I decided to use the bookmark techinique. The advice everyone gave me
was great, the bookmark method seemed the better approach for my
project. However, now I am having a problem with the bookmarks. I was
able to bookmark my tables in excel for when they are put into my word
document. I need my tables linked, so I pasted the tables in word
linked to the excel. When I save my document and then re-open it and
agree to updating my links to my tables, the bookmark goes away. If I
don't update then everything still works. How can I make it so the
bookmark will be maintained when I update my table links? Is this
possible? Below is a summed up version of my code. Thank you!

Sarah


Dim Wdoc As Word.document
Dim Wapp As Word.Application
Dim rng As Word.Range
Dim xlMissionDesign As Excel.Workbook
Dim x As Integer
Dim y As Integer
Dim BtnName As String
Dim shp As Word.InlineShape
Dim scode As String
Dim TableID As String
Dim ParagID As String

Set Wapp = CreateObject("Word.Application")
Wapp.Visible = True
Wapp.Documents.Add
Set Wdoc = Wapp.ActiveDocument

Set rng = Wdoc.Paragraphs.Last.Range

For i = 1 To x

For i = 1 to y

'copies my table from excel
rng.Style = "Normal"
Worksheets("Report Tables").Activate
Worksheets("Report Tables").Range("MissionOrbitParams").Select
Workbooks.Application.CutCopyMode = False
Workbooks.Application.Selection.Copy

'this paste my table into word, linked. It also creates the table
bookmark.
ParagID = "ParagBookMark" & i & j
TableID = "TableBookMark" & i & j
rng.InsertParagraphAfter
Set rng = Wdoc.Paragraphs.Last.Range
rng.PasteExcelTable linkedtoexcel:=True, wordformatting:=False,
RTF:=False
rng.Tables(1).Range.Select
Wdoc.Bookmarks.Add Name:=TableID,
Range:=Wdoc.Application.Selection.Range

BtnName = "OrbitButton" & i & j

'This creates my button.
Set shp = rng.document.InlineShapes.AddOLEControl _
(ClassType:="Forms.Commandbutton.1",
Range:=Wdoc.Paragraphs.Last.Range)
shp.OLEFormat.Object.Caption = "Add Orbit"
shp.OLEFormat.Object.Name = BtnName

'This puts code behind my button, where it calls the bookmarks I
created and works with them
'in the word document
scode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" &
vbCrLf & _
"If Not ActiveDocument.Bookmarks.Exists( " & Chr(34) & ParagID &
Chr(34) & ") Then" _
& vbCrLf & _
"MsgBox Prompt:=""Bookmark Paragraph not found"", Title:=
""Error""" & vbCrLf & _
"Exit Sub" & vbCrLf & _
"End If" & vbCrLf & _
"If Not ActiveDocument.Bookmarks.Exists(" & Chr(34) & TableID & Chr(34)
& ") Then" _
& vbCrLf & _
"MsgBox Prompt:=""Bookmark Table not found"", Title:= ""Error""" &
vbCrLf & _
"Exit Sub" & vbCrLf & _
"End If" & vbCrLf & _
vbCrLf & _
"ActiveDocument.Bookmarks(" & Chr(34) & TableID & Chr(34) & ").Select"
& vbCrLf & _
"Selection.Copy" & vbCrLf & _
vbCrLf & _
"ActiveDocument.Bookmarks(" & Chr(34) & ParagID & Chr(34) &
").Select" & vbCrLf & _
"ActiveDocument.Application.Selection.MoveUp Unit:=wdLine,
Count:=1" & vbCrLf & _
vbCrLf & _
"Selection.InsertParagraphAfter" & vbCrLf & _
"Selection.Paste" & vbCrLf & _
"Selection.InsertParagraphAfter" & vbCrLf & _
vbCrLf & _
"Selection.MoveRight Unit:=wdCharacter, Count:=1" & vbCrLf & _
vbCrLf & _
"End Sub"

Application.ActivateMicrosoftApp xlMicrosoftWord

'this applys my code

Wdoc.VBProject.VBComponents("ThisDocument").CodeMo dule.AddFromString
scode

Next j
Next i


macropod wrote:
Hi Sarah,

If you assign a bookmark to the table, you can always find the table again via
the bookmark - provided you're careful about putting another table within the
bookmark range and not moving/deleting the bookmark.

Another way would be to put the table inside a text box. Being a shape object,
a textbox can be named (best done when created). Again, you'd have to be
careful about putting another table into the same textbox.

Cheers
--
macropod
[MVP - Microsoft Word]


"hornbecky83" wrote in message
ups.com...
| Hi-
|
| I am working remotely in word vba through excel vba. Basically I have
| programed in excel to create a new word document, add a button, and
| then add code to the word document for when the button in excel is
| clicked. The code I am adding to the word document needs to copy a
| table and paste it (right above the button). I figured out how to copy
| a table if you give it an index number, but with the other code I am
| running the number of tables always changes. Therefore hardcoding an
| index number is a bad idea. I will end up copying different tables
| each time and not the one I want. Is it possible to add a name or
| range to that table, so I can just copy the table name or range?
|
| If this is not possible, I wanted to know if its possible to code
| adding an excel reference library in word. If I have the word document
| button code remote access excel to get the named table range that way,
| I need to figure out how to code an excel reference library into the
| button code. I do not know how or if this is possible. Manually
| setting it is not an option. I really hope someone can help me! Thank
| you so much in advance.
|
| sarah
|