Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 06-15-2018, 02:00 PM
d4okeefe d4okeefe is offline Pasting in word table, below bookmarks Windows 10 Pasting in word table, below bookmarks Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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
Reply With Quote
  #17  
Old 06-15-2018, 02:15 PM
d4okeefe d4okeefe is offline Pasting in word table, below bookmarks Windows 10 Pasting in word table, below bookmarks Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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
Reply With Quote
  #18  
Old 06-15-2018, 03:59 PM
macropod's Avatar
macropod macropod is offline Pasting in word table, below bookmarks Windows 7 64bit Pasting in word table, below bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by klutch View Post
My previous code would paste into the bookmark that I had within the table. What I am now seeking to do now is put in a line of code to paste the data into the next blank space down the column of the table. This macro is for performance reviews. That being said, there is a new review after a set period of time. The table that is being pasted into will fill up over time, meaning that the pasting into the bookmark is okay for the first time but I do not want to replace the data, I want it to paste into the next blank cell (within the column of the table) if there is text in the bookmark and following cells.
When you bookmark even part of a table, you can address the entire table by that bookmark. For example:
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]
Reply With Quote
  #19  
Old 06-15-2018, 04:56 PM
macropod's Avatar
macropod macropod is offline Pasting in word table, below bookmarks Windows 7 64bit Pasting in word table, below bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #20  
Old 06-15-2018, 06:09 PM
Guessed's Avatar
Guessed Guessed is offline Pasting in word table, below bookmarks Windows 10 Pasting in word table, below bookmarks Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #21  
Old 06-18-2018, 12:18 PM
klutch klutch is offline Pasting in word table, below bookmarks Windows 7 32bit Pasting in word table, below bookmarks Office 2016 for Mac
Advanced Beginner
Pasting in word table, below bookmarks
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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
I am getting a mismatch error on line
Code:
Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22))
Reply With Quote
  #22  
Old 06-18-2018, 12:19 PM
klutch klutch is offline Pasting in word table, below bookmarks Windows 7 32bit Pasting in word table, below bookmarks Office 2016 for Mac
Advanced Beginner
Pasting in word table, below bookmarks
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

Quote:
Originally Posted by d4okeefe View Post
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
I am getting a mismatch error on this line
Code:
 Set new_range = wdDoc.Range(bk.Start, bk.End)
Reply With Quote
  #23  
Old 06-18-2018, 02:08 PM
d4okeefe d4okeefe is offline Pasting in word table, below bookmarks Windows 10 Pasting in word table, below bookmarks Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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)
shouldn't give a mismatch error.

This, however, which was posted by guessed,
Code:
Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22))
would give you a type mismatch. "wdDoc.Bookmarks.Exists" returns a boolean (true or false) and not a range object. Change it to
Code:
Set wdRng = wdDoc.Bookmarks.("D" & Abs(dblC22)).Range
Reply With Quote
  #24  
Old 06-18-2018, 07:22 PM
macropod's Avatar
macropod macropod is offline Pasting in word table, below bookmarks Windows 7 64bit Pasting in word table, below bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #25  
Old 06-19-2018, 07:47 AM
d4okeefe d4okeefe is offline Pasting in word table, below bookmarks Windows 10 Pasting in word table, below bookmarks Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

That is a good point, Paul. I did not think of that, and will use in the future.
Reply With Quote
  #26  
Old 06-25-2018, 11:21 AM
klutch klutch is offline Pasting in word table, below bookmarks Windows 7 32bit Pasting in word table, below bookmarks Office 2016 for Mac
Advanced Beginner
Pasting in word table, below bookmarks
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

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
This also
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
This is for pasting into excel tables. Is there an identical way to select cells in a word table via the user selection?
Reply With Quote
  #27  
Old 06-26-2018, 04:16 AM
macropod's Avatar
macropod macropod is offline Pasting in word table, below bookmarks Windows 7 64bit Pasting in word table, below bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #28  
Old 06-26-2018, 05:41 AM
klutch klutch is offline Pasting in word table, below bookmarks Windows 7 32bit Pasting in word table, below bookmarks Office 2016 for Mac
Advanced Beginner
Pasting in word table, below bookmarks
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

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!
Attached Files
File Type: docx PR File.docx (19.8 KB, 11 views)
File Type: xls 7Excel.xls (19.3 KB, 11 views)
Reply With Quote
  #29  
Old 06-26-2018, 05:49 AM
macropod's Avatar
macropod macropod is offline Pasting in word table, below bookmarks Windows 7 64bit Pasting in word table, below bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There seems to be something wrong with your Excel file - Excel reports that it is corrupt.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #30  
Old 06-26-2018, 06:57 AM
klutch klutch is offline Pasting in word table, below bookmarks Windows 7 32bit Pasting in word table, below bookmarks Office 2016 for Mac
Advanced Beginner
Pasting in word table, below bookmarks
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

How about this
Attached Files
File Type: xlsx PR EXCEL.xlsx (8.3 KB, 12 views)
Reply With Quote
Reply

Tags
bookmark, if statement, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pasting in word table, below bookmarks Pasting table data so that it will wrap around the table 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
Pasting in word table, below bookmarks Table of Bookmarks 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:01 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft