I'm writing a macro to paste a bunch of tables and charts into a Word doc to be uploaded to our underwriters' system. I've tried to do a small sample and keep getting an error that the Word Application can't be activated.
Sub PasteToWord()
Dim wordapp As Word.Appliation
Dim worddoc As Word.Document
Dim xlrng As Range
Set wordapp = New Word.Application
wordapp.Activate
wordapp.Visible = True
Set worddoc = wordapp.Documents.Add
Set xlrng = ActiveSheet.Range("A1:A10")
xlrng.copy
worddoc.Paragraphs(1).Range.PasteExcelTable False, True, False
End Sub
You need to display the application before activating anything:
Sub PasteToWord()
Dim wordapp As Word.Appliation
Dim worddoc As Word.Document
Dim xlrng As Range
Set wordapp = New Word.Application
wordapp.Visible = True
wordapp.Activate
Set worddoc = wordapp.Documents.Add
Set xlrng = ActiveSheet.Range("A1:A10")
xlrng.copy
worddoc.Paragraphs(1).Range.PasteExcelTable False, True, False
End Sub
Related
I'm trying to create a userform that transfers the values of TextBoxes to a bookmarked location in a Word file but I'm getting an error. I tried some examples which I found on Google but I still getting the error.
I get the Error "VBA Object Doesn’t Support this Property or Method Error (Error 438)"
Am now at this point where I'm just trying a shorter macro which opens the Word file and should write "Test" into one bookmark:
Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0
wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)
With wordDoc.Selection
.Bookmarks("Zeile1").Range.Text = "Test"
End With
End Sub
The error ocures at the line
With wordDoc.Selection
I also tried this code and got also the same error:
Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0
wordApp.documents.Open VorlagePfad
wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)
wordApp.Bookmarks("Zeile1").Range.Text = "Test"
End Sub
I hope you can help me.
Kind regards
You just need to remove the ".Selection". Like this:
Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0
wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)
With wordDoc
.Bookmarks("Zeile1").Range.Text = "Test"
End With
End Sub
I am trying to convert a pdf file to an excel file (xlsx) using excel VBA.
The problem is the code seems to be perfectly fine as I have seen it working on other computers in action, but for some reason, I am getting a run time error and I am trying to solve this for a week.
Below is the code
Option Explicit
Function ClearCipboard()
'Early binding will requires a Reference to 'Microsoft Forms 2.0 Object Library'
Dim oData As Object 'New MSForms.DataObject
Set oData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
oData.SetText Text:=Empty
oData.PutInClipboard
Set oData = Nothing
End Function
Sub Automate()
Dim PathforPDFfiles As String
Dim PathforExcelfiles As String
PathforPDFfiles = "C:\Users\kvenkat2\Desktop\Trails 18.06.2021\Test File Excel\PDF-to-Excel-Converter\"
PathforExcelfiles = "C:\Users\kvenkat2\Desktop\Trails 18.06.2021\Test File Excel\PDF-to-Excel-Converter\"
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Set myFolder = fso.GetFolder(PathforPDFfiles)
Dim WordApp As Object
Dim WordDoc As Object
Dim WordRange As Object
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WordApp = CreateObject("word.application")
'Set WordDoc = WordApp.documents.Add
'Set WordApp = New Word.Application
WordApp.Visible = True
Dim nwb As Workbook
Dim nsh As Worksheet
For Each myFile In myFolder.Files
Set WordDoc = WordApp.documents.Open(myFile.Path, False, Format:="PDF Files")
Set WordRange = WordDoc.Paragraphs(1).Range
WordRange.WholeStory
Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
WordRange.Copy
nsh.Paste
nwb.SaveAs (PathforExcelfiles & Replace(myFile.Name, ".pdf", ".xlsx"))
Application.CutCopyMode = False
Call ClearCipboard
WordDoc.Close True
nwb.Close True
Next
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
Application.displayAlters = True
Application.ScreenUpdating = False
MsgBox ("Done for real")
End Sub
Set WordDoc = WordApp.documents.Open(myFile.Path, False, Format:="PDF Files")
This is the part where my code stops running and I try to see the opened word and nothing happens from here. I am unable to get past this line.
It shows as a run time error as shown in the image
I am trying to copy a table from excel to word and then back again to excel using VBA. I have a script to do both of those things but how can I make the copy from word back to excel from the active word file that got created with "Copy2word" so that I dont have to specify the location of the word document in "Copy2excel"?
Sub Copy2word()
Dim wdApp As Object
Dim wdDoc As Object
Dim wkSht As Worksheet
'\\ Stay on any sheet from which you want to copy data
Set wkSht = ActiveSheet
wkSht.UsedRange.Copy
'\\ Start word and create new document to paste data
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
End If
Set wdDoc = wdApp.Documents.Add
'\\ Paste Data from Excel
wdDoc.Range.PasteExcelTable False, False, True
'\\ Stop Excel's cut copy mode
Application.CutCopyMode = False
MsgBox "Copy to Word Finished!", vbInformation, "Copy to Word"
End Sub
Sub Copy2excel()
Const DOC_PATH As String = "C:\Users\MASS\Desktop\Test\TK1.docx"
Dim sht As Worksheet
Dim WordDoc As Word.Document
Dim WordApp As Word.Application
Dim i As Long, r As Long, c As Long
Dim rng As Range, t As Word.Table
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = False
Set WordDoc = WordApp.Documents.Open(DOC_PATH, ReadOnly:=True)
Set sht = Sheets("Sheet4")
Set rng = sht.Range("A20")
sht.Activate
For Each t In WordDoc.Tables
t.Range.Copy
rng.Select
rng.Parent.PasteSpecial Format:="Text", Link:=False, _
DisplayAsIcon:=False
With rng.Resize(t.Rows.Count, t.Columns.Count)
.Cells.UnMerge
.Cells.ColumnWidth = 14
.Cells.RowHeight = 14
.Cells.Font.Size = 10
End With
Set rng = rng.Offset(t.Rows.Count + 2, 0)
Next t
WordDoc.Close
WordApp.Quit
End Sub
I need to copy a table into a existing word document
I need to paste the data into a specific place in the word document, e.g. after a bookmark
I have a code that copy and paste, but not into an existing document.
I have tried to expand / change the code, but can not figure out how to paste to the target.
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add
Set ExcRng = ActiveSheet.Range("testdata")
ExcRng.copy
WrdDoc.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=True, RTF:=False
Application.CutCopyMode = False
End Sub
This works, and paste into a new document.
But I would like to have data pasted into this document: wordApp.Documents.Open "c:\users\peter\documents\Data skal ind her.docm"
I need to have it here:
Here is text part 1
And I would like to have my “testdata” pasted here:
Xxx
This is bookmark ”xxx”
Best regards
Peter
pg#pb.dk
I found this Word MVP doc that gives a function for updating text at a bookmark. I have added it to your example code:
Sub PasteIntoWord()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim objWord
Dim ExcRng As Range
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = wordApp.Documents.Open "c:\users\peter\documents\Data skal ind her.docm"
Set ExcRng = ActiveSheet.Range("testdata")
UpdateBookmark "xxx", ExcRng
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, PasteRange As Variant)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange = PasteRange
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
SOURCE: https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
I'm copying a range of cells from Excel as a picture to a Word document. It pastes at the beginning of the document.
How could I paste in a specific area? The area could be denoted by some text that I'd later find/replace.
Range("A1:H5").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("MyFile.docx")
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.Paste
End Sub
I just came accross the same problem and used the following code. I use a bookmark called "here" which is saved in my Word document. HTH, Mitch.
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("MyFile.docx")
Range("A1:H5").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("here").Select
Set objSelection = WordApp.Selection
objSelection.Paste