I wrote below code to take screenshot form Excel document and paste on word document.It works fine However I am unable to convert This Word document into PDF and error displays"Object does not support this property or method".It seems that i defined Variable (Objword as variant) not correct.Please any one can help.
Sub CopyToWordPicture()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim objword As variant
Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open "C:\Automation\BH Report\Daily&BH RAN KPI report_ok.docx"
Workbooks.Open Filename:="C:\Automation\BH Report\Daily_Hourly KPI Template.xlsx"
Sheets("BH_Graphs").Select
Range("A1:k50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'WDApp.Visible = True
' WDApp.Selection.GoToNext wdGoToPage
Windows("Daily_Hourly KPI Template.xlsx").Activate
Sheets("Daily_Graphs").Select
Range("A1:J50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'export as PDF
objword.ExportAsFixedFormat OutputFileName:="C:\Automation\BH Report\Daily&BH RAN KPI report.pdf", _
ExportFormat:=wdExportFormatPDF
end sub
Related
I have a list of pages that I want to delete in MS Word such as Page number : 5 to 10 , 12 to 16 etc. through MS Excel VBA.
I found a code to delete continuous pages through MS Excel VBA but when I run it gives "The Requested member of the collection does not exist" error." How can it be resolved ?
Sub DeletePages()
Dim WordApp As Word.Application
Dim myDoc As Word.Document
' Open the Word document
Set WordApp = New Word.Application
Set myDoc = WordApp.Documents.Open("C:\mydocument.docx")
' Delete pages 3 to 5
myDoc.Range(Start:=myDoc.Bookmarks("Page3").Range.Start, _
End:=myDoc.Bookmarks("Page5").Range.End).Delete
'Unbind
Set WordApp = Nothing
End Sub
For example:
Sub Demo()
Dim wdApp As New Word.Application, wdDoc As Word.Document, i As Long
With wdApp
.Visible = False
.DisplayAlerts = wdAlertsNone
Set wdDoc = .Documents.Open(FileName:="C:\mydocument.docx", AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
With wdDoc
For i = .ComputeStatistics(wdStatisticPages) To 1 Step -1
Select Case i
Case 5 To 10, 12 To 16
.Range.GoTo(What:=wdGoToPage, Name:=i).GoTo(What:=wdGoToBookmark, Name:="\page").Delete
End Select
Next
.Close SaveChanges:=True
End With
.DisplayAlerts = wdAlertsAll
.Quit
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
To understand why you don't need to create any bookmarks - and to understand what the code is doing - see:
https://learn.microsoft.com/en-us/office/vba/word/concepts/miscellaneous/predefined-bookmarks
Sub CopyToWord()
Dim objWord As New Word.Application
'copying the range that I want to paste in Word
With ThisWorkbook.Worksheets("grid_copy")
.Range("b1:AA42").CopyPicture xlScreen
End With
'pasting the picture in a new Word document
With objWord
.Documents.Add
.Selection.Paste
.Selection.ShapeRange.Height = 651
.Selection.ShapeRange.Width = 500
'Those two lines don't work to resize the picture that i'm pasting in word
.Visible = True
End With
End Sub
The code is actualy working but I'm not capable of applying the resize of the image that I want. Do you guys know a way that I can resize the picture that i'm pasting in Word coming form a range in Excel?
Try:
Sub CopyToWord()
'copying the range that I want to paste in Word
ThisWorkbook.Worksheets("grid_copy").Range("b1:AA42").CopyPicture xlScreen
'pasting the picture in a new Word document
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdImg As Word.InlineShape
With wdApp
.Visible = True
.ScreenUpdating = False
Set wdDoc = .Documents.Add
With wdDoc
.Range.Paste
Set wdImg = .InlineShapes(1)
With wdImg
.Height = 651
.Width = 500
End With
End With
.ScreenUpdating = True
End With
End Sub
I'm creating a tool in Excel
Which is going to read in some data and the create a word document based on that data.
So far I've got excel to create the word document and add a few lines of text without any issue.
The next bit though to add a table is causing issues.
I can add the table in fine, but for some reason it deletes the lines of text that I added in the first place.
This is my code:
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSelection As Object
Dim objRange As Object
Dim objTable As Object
Dim ctr as long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
Set objSelection = objWord.Selection
Set objRange = objDoc.Range
'Adding some heading Text
objSelection.Style = objDoc.Styles("Heading 1")
objSelection.Font.Bold = True
objSelection.TypeText ("Heading Text")
objSelection.TypeParagraph
'Adding some normal Text
objSelection.Style = objDoc.Styles("Normal")
objSelection.Font.Bold = False
objSelection.TypeText ("Normal Text")
objSelection.TypeParagraph
Stop
'Adding the table
objDoc.Tables.Add objRange, 10, 2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I put in a stop points after my heading and normal text are added and they appear in the word document fine.(screenshot below)
But as soon as the code reaches the Tables.Add bit, all my text disappears and the document has nothing but the table. (also screenshot below)
I looked around online and tried putting
objSelection.Collapse WdCollapseDirection.wdCollapseEnd
before the Tables.Add line of code, but that didn't help.
Your code to add a table fails because you are adding the table into objRange which you defined as the entire document.
You should also get into the habit of avoiding use of the Selection object, both in Word and Excel. Not only is it ineffecient (the screen has to be redrawn constantly) it is also error prone as the selection could be changed by the user to something you're not expecting.
The code below should work for you.
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objRange As Word.Range
Dim objTable As Word.Table
Dim ctr As Long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
'Adding some heading Text
With objDoc.Paragraphs(1).Range
.Style = objDoc.Styles(wdStyleHeading1)
.Font.Bold = True
.Text = "Heading Text"
.InsertParagraphAfter
End With
'Adding some normal Text
With objDoc.Paragraphs(2).Range
.Style = objDoc.Styles(wdStyleNormal)
.Font.Bold = False
.Text = "Normal Text"
.InsertParagraphAfter
End With
Set objRange = objDoc.Paragraphs.Last.Range
'Adding the table
Set objTable = objDoc.Tables.Add(objRange, 10, 2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I did the test with the code below and it works :
Pre requisite : add reference "Microsoft Word xx.x Object Library" in your VBA project
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
' create an instance of MS Word
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
Range("A1:A2").Copy
WordApp.Selection.TypeText ("Here are my comment")
WordApp.Selection.Paste
' fit the table with window
WordDoc.Tables(1).AutoFitBehavior wdAutoFitWindow
' Save the content into the .doc file
WordDoc.SaveAs2 ("C:\mypath\myDocument.doc")
I've been struggling to get a conversion of word files to PDFS (Ran using excel) to work.
I've tried the below(From other answers on here) and get the error(On the ExportAsFixedFormat Line), "Invalid procedure call or argument":
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open "C:\Test.docx"
objWord.ActiveDocument.ExportAsFixedFormat OutputFileName:="C:\test.pdf", ExportFormat:=wdExportFormatPDF
objWord.Quit
I've also tried this setup however this doesn't error when ran, but the PDFs error when trying to open.
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open "C:\Test.docx"
objWord.ActiveDocument.SaveAs2 FileName:="C:\Test.pdf", FileFormat:=wdFormatPDF
objWord.Quit
I have used the code I found here and tried to extract a PDF file. The code extracts the PDF perfectly in Excel, but I keep getting notification messages. I've disable Display Alerts, but it does not make a difference.
Sub ImportPDF()
Dim objWord As Object
Dim objDoc As Object
Dim wdFileName
Set objWord = CreateObject("word.Application")
wdFileName = "C:\42046_120_2077802.pdf"
Application.DisplayAlerts = False
Set objDoc = GetObject(wdFileName)
objWord.Documents.Open (wdFileName)
objWord.Selection.WholeStory
objWord.Selection.Copy
Sheets(1).Select
[A1].Select
ActiveWorkbook.ActiveSheet.Paste
'objDoc.Close ' I get an error message if I add this (Object does not support this property or method)
objWord.Quit
Application.DisplayAlerts = True
End Sub
The messages I get are the following:
Is there a way to get rid of the messages?
Change your code to:
Sub ImportPDF()
Dim objWord As Object
Dim objDoc As Object
Dim wdFileName
Set objWord = CreateObject("word.Application")
wdFileName = "C:\42046_120_2077802.pdf"
Application.DisplayAlerts = False
Set objDoc = objWord.Documents.Open(wdFileName)
objWord.Selection.WholeStory
objWord.Selection.Copy
Sheets(1).Select
[A1].Select
ActiveWorkbook.ActiveSheet.Paste
objDoc.Close SaveChanges:=False
objWord.Quit
Application.DisplayAlerts = True
End Sub
Application.DisplayAlerts refers to the Excel application, not the instance of Word, which is displaying the alerts.
To avoid the first two alerts, use the additional parameters of Documents.Open
ConfirmConversions - "True to display the Convert File dialog box if the file isn't in Microsoft Word format" - so False.
ReadOnly - "True to open the document as read-only" - so True.
Closing the document without saving changes seems to also avoid the third pop-up. This might be an option as well.
Sub ImportPDF()
Dim objWord As Object, objDoc As Object
Dim wdFileName As String
Set objWord = CreateObject("word.Application")
wdFileName = "C:\42046_120_2077802.pdf"
Set objDoc = objWord.Documents.Open(wdFileName, False, True)
objWord.Selection.WholeStory
objWord.Selection.Copy
ThisWorkbook.Sheets(1).Range("A1").Select
ThisWorkbook.Sheets(1).Paste
objDoc.Close False
objWord.Quit
End Sub