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
Related
I am trying to open a word .docm via a string with Documents.Open. It works after the second run of the script. In the first run the document simply doesn't open up, and the script doesn't load the doc file. Any thoughts or tips?
Sub gerarproposta()
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Dim objWord As Word.Application, wdDoc As Word.Document
'banco de dados
Dim xl As New Excel.Application
Dim xlw As Excel.Workbook
'Variável texto
Dim Tex As String
On Error Resume Next
Set basedados = Sheets("Base de Dados")
Set objWord = GetObject(, "Word.Application")
objWord.DisplayAlerts = wdAlertsNone
objWord.Visible = True
'xl.Visible = True
'Word - proposta modelo
Tex = """" & basedados.Range("B30") & "\" & basedados.Range("B31") & """"
'Abre o arquivo do Word
Set wdDoc = objWord.Documents.Open(Tex)
.
.
.
End Sub
Already tried to change the TEX variable to a String manually
Every run after the first one works normally
The following statement for retrieving the Word Application instance can be used if Word is already launched:
Set objWord = GetObject(, "Word.Application")
Instead, you could an early binding like you did in case of Excel:
Set objWord = New Word.Application
Read more about early and late binding technology in the Early and Late Binding (Visual Basic) article.
The commentary of #Shrotter solved my problem. I changed the code:
Set objWord = GetObject(, "Word.Application")
To:
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
End If
I tried to get the object from a non-existent Word session.
I am new in VBA and would like to asking some help.
I have a list of word document in excel in range B3:B40. I would like to copy the document in the list and paste to a new document without changing the page format.
I already tried the code below, it give me "run time error 13". Can anybody help with this situation?
Thanks in advance for any help.
Application.ScreenUpdating=false
set objword = createobject("Word.Application")
set objdoc = objword.Documents.Add
objword.visible = true
set objselection = objword.Selection
Folderpath = "C:\desktop" 'where I save the word document that would be combined
set objtempword = createobject("Word.Application")
set tempdoc = objword.documents.open (Folderpath & "\" & Sheet1.Range ("B3:B40")
set objtempselection = objtempword.selection
tempdoc.range.select
tempdoc.range.copy
objselection.typeparagraph
objselection.paste
tempdoc.close
I think this could work for you. What was missing is a cycle to work for each file (cell in the range).
Option Explicit
Sub JoinDocs()
Application.ScreenUpdating = False
Dim objword As Object, objdoc As Object, objselection As Object
Set objword = CreateObject("Word.Application")
Set objdoc = objword.Documents.Add
objword.Visible = True
Dim Folderpath As String
Set objselection = objword.Selection
Folderpath = "C:\desktop\" 'where I save the word document that would be combined
Dim vDoc As Variant
Dim objtempword As Object, tempdoc As Object, objtempselection As Object
Set objtempword = CreateObject("Word.Application")
For Each vDoc In Sheet1.Range("B3:B40").Value
Set tempdoc = objword.Documents.Open(Folderpath & vDoc)
Set objtempselection = objtempword.Selection
tempdoc.Range.Select
tempdoc.Range.Copy
objselection.TypeParagraph
objselection.Paste
tempdoc.Close
Next vDoc
End Sub
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
In EXCEL, I have some VBA codes to open a Word Document A and copy its content from certain page to a new document. Currently, I can copy its text. I am wondering how to copy both context and formatting. Below is my current code and I appreciate any suggestions!
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
'Prepare Document B
objDoc.SaveAs (Folderpath to Document B)
Set objTempWord = CreateObject("Word.Application")
Set tempDoc = objWord.Documents.Open(Folderpath to Document A)
'copy context from Document A
With tempDoc.Application
.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:="2"
.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
.Selection.Copy
End With
objSelection.TypeParagraph
objSelection.Paste
objSelection.InsertBreak Type:=wdSectionBreakNextPage
tempDoc.Close
objDoc.Application.Statusbar = False
objDoc.Save
This here does the same, without the superfluous extra Application object and without the use of Selection:
Dim objWord As Word.Application
Dim objDoc As Word.Document, newDoc As Word.Document
Dim r As Word.Range, r2 As Word.Range
Set objWord = CreateObject("Word.Application") 'or Set objWord = new Word.Application
Set objDoc = objWord.Documents.Open(FolderpathToDocumentA)
Set newDoc = objWord.Documents.Add
newDoc.SaveAs FolderpathToDocumentB
Set r = objDoc.GoTo(what:=wdGoToPage, which:=wdGoToAbsolute, Name:=2)
r.End = objDoc.Range.End
'copy context from Document A
r.Copy
newDoc.Content.InsertBreak Type:=wdSectionBreakNextPage
newDoc.Range(newDoc.Content.Start, newDoc.Content.Start).Paste
newDoc.Content.InsertBefore vbCrLf
newDoc.Save
objWord.Quit
Does that do what you need?
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