Copy selection to temporary variable

K

Kurt

I'm using Word 2003.

I have a macro that copies a string of text from a cell in a Word
table:

Selection.Tables(1).Cell(1, 2).Select
Selection.Copy

I use it later to paste the contents into a cell in another table (...
Selection.Paste)

But, I'd also like to use what's copied as part of the document's new
file name. So, I think I need to save it to a temporary variable
(e.g., strCopied) so I can use it in a SaveAs command like so:

Sub SaveAs()

ActiveDocument.SaveAs FileName:="File ABC - " & " strCopied" & ".doc"

End Sub

Any suggestions about how to do this? Thanks.
 
D

dedawson

You've already figured it out part of the answer, but you need to
allow for the fact that you're copying data from a table. Since every
cell has an end-of-cell marker, you need to strip it off before you
can use the data to name your file.


Activedocument.Tables(1).Cell(1,2).Select
sCopiedString = Activedocument.Tables(1).Cell(1,2).Range.Text ' get
entire cell contents
sCopiedString = Left(sCopiedString, Len(sCopiedString) - 2) ' strip
off end of cell marker
ActiveDocument.SaveAs FileName:="File_Saved_As_1_" + sCopiedString +
".docx"


Assume the text in the cell was "Fred". Executing the statements
above will save the file as File_Saved_As_Fred.docx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top