![]() |
#16
|
|||
|
|||
![]()
No need. I think I get it. Try this on your word document. Ignore the spreadsheet for the moment. See if you get what you want. Use Ctrl-Z to undo if necessary.
Code:
Sub expand_bookmark_to_include_insertion() Dim bk As Bookmark Dim new_range As Range Set bk = ActiveDocument.Bookmarks(1) Dim bk_name As String: bk_name = bk.Name Set new_range = ActiveDocument.Range(bk.Start, bk.End) new_range.InsertAfter (vbCrLf & "Inserted text") bk.Delete Dim bk_new As Bookmark Set bk_new = Bookmarks.Add(bk_name, new_range) End Sub |
#17
|
|||
|
|||
![]()
There's a lot going on in your original code, so I'll do my best. I can't recreate it perfectly without the original files. Here goes:
Code:
Sub test_ok() Dim myfile, wdApp As New Word.Application, wdDoc As Word.Document myfile = Application.GetOpenFilename(, , "Browse for Document") Dim i As Integer i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0) Range("E" & i).Select Dim excel_selection As String excel_selection = Selection.Text wdApp.Visible = True Set wdDoc = wdApp.Documents.Open(myfile) 'select the word range you want to paste into Dim bk As Bookmark, bk_new As Bookmark Dim new_range As Range Dim bk_name As String If Range("c2") = 22 Then Set bk = wdDoc.Bookmarks("d22") bk_name = bk.Name Set new_range = wdDoc.Range(bk.Start, bk.End) End If If Range("c2") = 5 Then Set bk = wdDoc.Bookmarks("d5") bk_name = bk.Name Set new_range = wdDoc.Range(bk.Start, bk.End) End If If Range("c2") = -20 Then Set bk = wdDoc.Bookmarks("d20") bk_name = bk.Name Set new_range = wdDoc.Range(bk.Start, bk.End) End If new_range.InsertAfter (vbCrLf & excel_selection) bk.Delete Set bk_new = wdDoc.Bookmarks.Add(bk_name, new_range) End Sub |
#18
|
||||
|
||||
![]() Quote:
Code:
With ActiveDocument.Bookmarks("myTable").Range.Tables(1) .Rows.Add .Cell(.Rows.Count, 1).Range.Text = "Hello World" End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#19
|
||||
|
||||
![]()
Cross-posted at: https://www.office-forums.com/thread...l-vba.2350340/
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#20
|
||||
|
||||
![]()
Is the code meant to add a row under the row where the bookmark exists? If not, how do you know there is a cell there and it is empty?
Assuming there is always a cell below the bookmark, I would do the code like this. Read through this carefully because it is considerably different from what you were previously doing and includes some of the testing that is required to deal with the errors that you might encounter Code:
Sub CopyToWord() Dim myfile As String, wdApp As New Word.Application, wdDoc As Word.Document Dim i As Integer, dblC22 As Double, wdRng As Word.Range, strAve As String i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0) strAve = Range("E" & i).Value dblC22 = Range("c2") myfile = Application.GetOpenFilename(, , "Browse for Document") wdApp.Visible = True Set wdDoc = wdApp.Documents.Open(myfile) Select Case dblC22 Case 5, 22, -20 If wdDoc.Bookmarks.Exists("D" & Abs(dblC22)) Then Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22)) If wdRng.Information(wdWithInTable) Then wdRng.MoveEnd Unit:=wdCell, Count:=4 wdRng.InsertAfter strAve Else wdRng.InsertAfter strAve End If End If End Select End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#21
|
|||
|
|||
![]() Quote:
Code:
Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22)) |
#22
|
|||
|
|||
![]() Quote:
Code:
Set new_range = wdDoc.Range(bk.Start, bk.End) |
#23
|
|||
|
|||
![]()
Let me know where you're at in the process.
What does your subroutine look like right now? Btw, this line of code Code:
Set new_range = wdDoc.Range(bk.Start, bk.End) This, however, which was posted by guessed, Code:
Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22)) Code:
Set wdRng = wdDoc.Bookmarks.("D" & Abs(dblC22)).Range |
#24
|
||||
|
||||
![]()
Why are you messing around with the bookmark range when as I've already said, you can address anywhere in the table without doing so?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#25
|
|||
|
|||
![]()
That is a good point, Paul. I did not think of that, and will use in the future.
|
#26
|
|||
|
|||
![]()
OK, I have decided that pasting through bookmarks will not work for what I am trying to accomplish. I have found this code that allows the user to select the range they want to paste into mid-macro.
Code:
Sub GetUserRange() Dim UserRange As Range Output = 565 Prompt = "Select a cell for the output." Title = "Select a cell" ' Display the Input Box On Error Resume Next Set UserRange = Application.InputBox( _ Prompt:=Prompt, _ Title:=Title, _ Default:=ActiveCell.Address, _ Type:=8) 'Range selection ' Was the Input Box canceled? If UserRange Is Nothing Then MsgBox "Canceled." Else UserRange.Range("A1") = Output End If End Sub Code:
Dim userResponce As Range On Error Resume Next Set userResponce = Application.InputBox("select a range with the mouse", Default:=Selection.Address, Type:=8) On Error GoTo 0 If userResponce Is Nothing Then MsgBox "Cancel clicked" Else MsgBox "You selected " & userResponce.Address |
#27
|
||||
|
||||
![]()
We seem to be chasing an ever-moving target here. How about posting a copy of both the workbook and the destination document (delete anything sensitive), together with a description of what goes where. You can do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#28
|
|||
|
|||
![]()
The PR file is only the table I am pasting into, there is too much sensitive data in the rest of the doc to upload it. There is also the excel file. Hope this works!
|
#29
|
||||
|
||||
![]()
There seems to be something wrong with your Excel file - Excel reports that it is corrupt.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#30
|
|||
|
|||
![]()
How about this
|
![]() |
Tags |
bookmark, if statement, vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
rebmaboss | Word | 1 | 11-25-2016 02:30 AM |
Pasting text from Excel cell into word without creating a table, and keeping the in-cell formatting | hanvyj | Excel Programming | 0 | 08-28-2015 01:15 AM |
How to insert a table using bookmarks in a document | Catty | Word VBA | 3 | 05-04-2015 03:05 AM |
![]() |
Niy | Word | 3 | 03-28-2012 12:18 AM |
Pasting table in Photoshop cutting off table | azdolfan | Word Tables | 0 | 05-16-2010 01:52 PM |