PDA

View Full Version : How to copy from a specific cell in a table into another table in another document



exce
02-17-2016, 12:42 PM
Lets say I have a table that is 3x3. I want to copy the cell in Row 2, Column 2.








Bookmark here
Text is here










The data I want is editable, so placing a bookmark there doesn't work because a user can delete it. So if I put a bookmark in the cell to the left (which isn't editable), how can I copy the cell to the right of the bookmark?
I will then paste this data into another document, I believe I have the steps for that figured out just fine, it's just collecting the data I am having a problem with.

I wanted to use a bookmark instead of referencing the range of the table in case I need to dynamically add rows to the table.

ActiveDocument.Bookmarks("myBookmark").Range.Select will select the current cell just fine. I just don't know how to go right 1 cell.

Word 2010.

gmaxey
02-17-2016, 06:17 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("MyBookmark").Range
oRng.Move wdCell, 1
Set oRng = oRng.Cells(1).Range
MsgBox Left(oRng, Len(oRng) - 2)
lbl_Exit:
Exit Sub
End Sub

exce
02-18-2016, 10:08 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("MyBookmark").Range
oRng.Move wdCell, 1
Set oRng = oRng.Cells(1).Range
MsgBox Left(oRng, Len(oRng) - 2)
lbl_Exit:
Exit Sub
End Sub



Thanks Greg. I got it to work. If you or anyone could help out with one last problem; when I use the following paste command

tripBookmark.Range.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)

It doesn't match the destination, it uses the origins formatting. Any idea why?

gmaxey
02-18-2016, 05:39 PM
Sorry but I don't know why.

gmayor
02-18-2016, 10:38 PM
when I use the following paste command

tripBookmark.Range.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)

It doesn't match the destination, it uses the origins formatting. Any idea why?How does this relate to the rest of the thread? Is this the bookmark you are pasting to? Personally I wouldn't use copy and paste. I would simply write the range to its new destination, which will adopt the style at the target bookmark e.g.

Option Explicit
Sub CopyACell()
Dim oCell As Range
Dim oTarget As Document
Set oCell = ActiveDocument.Bookmarks("MyBookmarkName").Range.Rows(1).Cells(2).Range
oCell.End = oCell.End - 1
Set oTarget = Documents.Open("C:\Path\Doc2.docx")
FillBM oTarget.Bookmarks("Bookmark2"), oCell.Text
lbl_Exit:
Set oCell = Nothing
Set oTarget = Nothing
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

exce
02-19-2016, 12:24 PM
Thank you Graham. This works a lot better. I don't know why the copy paste method has so many issues.