Selecting a range and selecting a document to transfer it to

DavidODonovan

New Member
Joined
Sep 4, 2013
Messages
11
Hello to All,
I have a macro that opens a word document by requesting that I open the document. It then transfers data from an open excel workbook to the word document (in the given case below it is just cell A1). I want to get it to take a range of cells selected by the user and input these in the word file. The code to get a user defined range is as follows: Set SelectionRange = Application.InputBox(prompt:="Select Data Range", _ Type:=8)

So my question is, How do I get the above line of code to work in the macro below?



Public Sub ModifyWordDoc()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range
Dim sFilename As String
Dim SelectionRange As Range

Set wordApp = CreateObject("Word.Application")
sFilename = Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*")
wordApp.Visible = True
If sFilename = "False" Then Exit Sub



With wordApp
Set wordDoc = .Documents.Open(sFilename)
Set wordRange = wordDoc.Range

With wordRange
.Move wdStory, 1
.InsertAfter ThisWorkbook.Sheets(1).Range("A1").Value & vbCrLf
End With


.ActiveDocument.Save
.ActiveDocument.Close
.Application.Quit
End With

Set wordDoc = Nothing 'Not necessary here but good practice to minimise memory use in the case of several variables refering to the same object
Set wordApp = Nothing

MsgBox "Done: " & sFilename & " modified" & Space(10), vbOKOnly + vbInformation

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
Public Sub ModifyWordDoc()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range
Dim sFilename As String
Dim SelectionRange As Range
Set myrange = Application.InputBox(prompt:="Select Data Range", Type:=8)
myrange.Copy
Set wordApp = CreateObject("Word.Application")

sFilename = Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*")
wordApp.Visible = True
If sFilename = "False" Then Exit Sub
With wordApp
  Set wordDoc = .Documents.Open(sFilename)
  Set wordRange = wordDoc.Range
  wordRange.Paste
  .ActiveDocument.Save
  .ActiveDocument.Close
  .Application.Quit
End With

Set wordDoc = Nothing 'Not necessary here but good practice to minimise memory use in the case of several variables refering to the same object
Set wordApp = Nothing

MsgBox "Done: " & sFilename & " modified" & Space(10), vbOKOnly + vbInformation

End Sub
 
Upvote 0
This macro suffices:

Code:
Public Sub M_snb()
   Application.InputBox(prompt:="Select Data Range", Type:=8).Copy
 
   with getobject(Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*"))
      .content.paste  
      .Close 1
   End With
End Sub
 
Upvote 0
Ok snb_
very short, but .Close without 1.
This code replace the content of document, how can we append below the content ?
 
Upvote 0
In word a macro to shift to the far right of an existing table of data and paste as new column is as follows.


Selection.EndKey Unit:=wdRow, Extend:=True
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste


This wont work as the 'Selection' is a column I have already copied from the word document. Any ideas?


This macro suffices:


Code:
Public Sub M_snb()
   Application.InputBox(prompt:="Select Data Range", Type:=8).Copy
 
   with getobject(Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*"))
      .content.paste  
      .Close 1
   End With
End Sub
 
Upvote 0
To append:

Code:
Public Sub M_snb()
   Application.InputBox(prompt:="Select Data Range", Type:=8).Copy

   with getobject(Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*"))
      .content.insertafter vbcr
      .paragraphs.last.range.paste  
      .Close 1
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top