I have an embedded MS-Word document in an Excel Worksheet which name is SalaryPaycheck.
The MS-word document contains several linked fields to Worksheet cells.
I have update the linked cells, several times and perform updating above fields.
Then I need perform exporting the embedded MS-Word document each time the fields have updated, as PDF.
So I need all exported files are appending in a single pdf file.
I using below code:
Sub PrintIt()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim i as Integer
ActiveSheet.OLEObjects("SalaryPaycheck").Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = False
Set objDoc = objWord.ActiveDocument
objWord.Application.DisplayAlerts = wdAlertsNone
objWord.Application.ScreenUpdating = False
For i = 1 to 10
Range("Key").value = i
objDoc.Fields.Update
objDoc.ExportAsFixedFormat _
outputfileName:=ThisWorkbook.path & "\Results\" & "rep" & i & ".pdf" _
, exportformat:=wdExportFormatPDF _
, openafterexport:=False _
, optimizefor:=wdExportOptimizeForPrint _
, Range:=wdExportAllDocument _
, Item:=wdExportDocumentContent _
, includedocprops:=False _
, keepirm:=True _
, createbookmarks:=wdExportCreateNoBookmarks _
, docstructuretags:=True _
, bitmapmissingfonts:=True _
, useiso19005_1:=False
Next i
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
End Sub 'Print it
How can using objDoc.SaveAs2 or objDoc.ExportAsFixedFormat (shown above) same as objDoc.PrintOut' whith 'Append:=True argument?
Or How can using .PrintOut with 'Append:=True' argument in this case (PDF) which working quietly (using OutputFileName:=path & filename and PrintToFile:=True)
As said in the other question, just append the documents in word
Sub PrintIt()
Dim objWord As Word.Application
Dim objDocTotal As Word.Document
Dim objDoc As Word.Document
Dim i As Integer
Dim strOutfile As String
Dim rg As Word.Range
ActiveSheet.OLEObjects("SalaryPaycheck").Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
Set objDoc = objWord.ActiveDocument
Set objDocTotal = Documents.Add
objWord.Application.DisplayAlerts = wdAlertsNone
objWord.Application.ScreenUpdating = True
For i = 1 To 10
Range("Key").Value = i
With objDoc
.Fields.Update
.Content.Copy
End With
Set rg = objDocTotal.Content
With rg
.Collapse Direction:=wdCollapseEnd
If i > 1 Then .InsertBreak wdPageBreak
.PasteAndFormat wdFormatOriginalFormatting
End With
Next i
strOutfile = "<Path>\Salary.pdf"
objDocTotal.ExportAsFixedFormat outputfileName:= _
strOutfile, exportformat:=wdExportFormatPDF, _
openafterexport:=False, optimizefor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent
objDocTotal.Close False
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
End Sub
Related
I have a code which copies data from a spreadsheet into specific bookmarks on a particular document. When it is run it works fine, but the bookmarks are deleted from the spreadsheet. Is there a way I can keep the bookmarks in the document
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet6")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\GR1 CPA Test1.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("CN1").Range.Text = ws.Range("C25").Value
.Bookmarks("CN2").Range.Text = ws.Range("C25").Value
.Bookmarks("CNo").Range.Text = ws.Range("C26").Value
.Bookmarks("CL1").Range.Text = ws.Range("C27").Value
.Bookmarks("Ex1").Range.Text = ws.Range("C28").Value
.Bookmarks("Ex2").Range.Text = ws.Range("C28").Value
.Bookmarks("Su1").Range.Text = ws.Range("C29").Value
.Bookmarks("Su2").Range.Text = ws.Range("C29").Value
.Bookmarks("Su3").Range.Text = ws.Range("C29").Value
.Save
.Close
End With
Set objWord = Nothing
End Sub
I've used this in the past:
'Replace the text in a bookmark or insert text into an empty (zero-length) bookmark
Sub SetBookmarkText(oDoc As Word.Document, sBookmark As String, sText As String)
Dim BMRange As Word.Range
If oDoc.Range.Bookmarks.Exists(sBookmark) Then
Set BMRange = oDoc.Range.Bookmarks(sBookmark).Range
BMRange.Text = sText
oDoc.Range.Bookmarks.Add sBookmark, BMRange
Else
MsgBox "Bookmark '" & sBookmark & "' not found in document '" & oDoc.Name & "'" & _
vbCrLf & "Content not updated"
End If
End Sub
Usage:
Dim ws As Worksheet, doc as object
Set ws = ThisWorkbook.Sheets("Sheet6")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set doc = objWord.Documents.Open("C:\GR1 CPA Test1.docx")
SetBookmarkText doc, "CN1", ws.Range("C25").Value
SetBookmarkText doc, "CN2", ws.Range("C25").Value
'etc etc
doc.Save
doc.Close
Set objWord = Nothing
End Sub
I am able to add multiple images to a Word document using VBA and at the same time I am not able to add multiple cross references using VBA.
Sub checking()
Dim strFolderPath
strFolderPath = "C:\images"
Dim objWord
Dim objDoc
Dim objSelection
Dim objShapes
Dim objFSO
Dim objFolder
Set objWord = CreateObject("Word.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderPath)
Set objDoc = objWord.Documents.Open("D:\myfile.docx")
objWord.Visible = True
Set objSelection = objWord.Selection
For Each Img In objFolder.Files
ImgPath = Img.Path
objSelection.InlineShapes.AddPicture (ImgPath
objSelection.insertbreak
Next
End Sub
I am going to show you an example. You will have to amend it to suit your need. I am going to insert 2 images and create a Cross Reference. I have commented the code so you should not have a problem understanding it.
Logic:
Add image
Add caption to image
Create the Cross Reference
Insert Page Break?
Repeat the above step
Code:
Sub Sample()
Dim shp As InlineShape
Dim n As Long
'
'~~> Insert Image 1
'
Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageA.Png", _
LinkToFile:=False, _
SaveWithDocument:=True)
'~~> Adding a caption
n = 1
CaptionLabels.Add Name:="MyImage" & n
shp.Select
Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.InsertBreak
'
'~~> Insert Image 2
'
Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageB.Png", _
LinkToFile:=False, _
SaveWithDocument:=True)
'~~> Adding a caption
n = n + 1
CaptionLabels.Add Name:="MyImage" & n
shp.Select
Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.InsertBreak
'~~> Creating cross reference
For n = 1 To 2
Selection.InsertCrossReference ReferenceType:="MyImage" & n, ReferenceKind:= _
wdEntireCaption, ReferenceItem:=1, InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
Next n
End Sub
I want to print a macro-enabled MS-Word Document that is embedded in a Excel Worksheet which it's name is SalaryPaycheck, from the excel macro module.
I using code below:
Sub PrintIt()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim Oshp As Object
Dim strCurrentPrinter As String
ActiveSheet.OLEObjects("SalaryPaycheck").Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = False
Set objDoc = objWord.ActiveDocument
objWord.Application.DisplayAlerts = wdAlertsNone
objDoc.Application.ActivePrinter = "Adobe PDF on Ne06:"
objDoc.PrintOut Background:=False
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
Application.ScreenUpdating = True
Exit Sub
End Sub 'Print it
The PrintOut opens a dialog box which asks about path and file name.
I want to made file name and path predefined so PrintOut runs quietly.
If you have an up-to-date Word version then you could export/save the file to a PDF document directly. Change your code to
Sub PrintIt()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim Oshp As Object
Dim strCurrentPrinter As String
ActiveSheet.OLEObjects("SalaryPaycheck").Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = False
Set objDoc = objWord.ActiveDocument
objWord.Application.DisplayAlerts = wdAlertsNone
' objDoc.Application.ActivePrinter = "Adobe PDF on Ne06:"
' objDoc.PrintOut Background:=False
Dim strOutFile As String
strOutFile = "<filename>.pdf"
objDoc.ExportAsFixedFormat OutputFileName:= _
strOutFile, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
Application.ScreenUpdating = True
Exit Sub
End Sub
I have the below code that copy range cell values from excel and paste as picture in a new word document.
I want to save the active document as pdf file with name of file as value in cell "A2".
If you can help me in adding the same in the below code it would be a great help.
Sub Picture()
Dim objWord, objDoc As Object
ActiveWindow.View = xlNormalView
Range("A2:K25").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Selection.Paste
objWord.Selection.TypeParagraph
End Sub
Try this,
Sub SaveAsPDF()
Dim objWord, objDoc As Object
Dim A2 As String
Dim Crng As Range
A2 = Range("A2")
Set Crng = Range("A2:K25")
Crng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Selection.Paste
objWord.Selection.TypeParagraph
With objDoc
.ExportAsFixedFormat OutputFileName:= _
"C:\Users\Dave\Downloads\" & A2 & ".pdf", ExportFormat:=17, _
OpenAfterExport:=True, OptimizeFor:=0, Range:= _
0, From:=1, To:=1, Item:=0, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
0, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
.Close saveChanges:=False
End With
objWord.Quit
Set objWord = Nothing
End Sub
Instead of using word to PDF use excel
Sub SaveAsPDFxlStyle()
Dim objWord, objDoc As Object
Dim A2 As String
A2 = Range("A2")
ActiveSheet.PageSetup.PrintArea = "$A$2:$K$25"
With ActiveSheet.PageSetup
.PrintGridlines = True
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Dave\Downloads\" & A2 & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=0
End Sub
I am running an excel programme that generates graphs based on data. I then want to export it to word and save it as a pdf.
I am having issues with pasting the diagrams into word as I often get the error 'Run time error '4198' Command Failed' on the Word Paste Special line. I have included by code for the word export below.
Option Explicit
Sub word_export(numscans As Integer, rootpath As String, poleid As String)
Dim n As Integer
Dim i As Integer
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Set WDApp = CreateObject("Word.Application")
Set WDDoc = WDApp.Documents.Add
Application.Wait (Now + TimeValue("0:00:01"))
WDApp.DisplayAlerts = wdAlertsNone
For n = 1 To numscans
For i = 1 To Sheets("Scan" & n).ChartObjects.Count
Sheets("Scan" & n).ChartObjects(i).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
WDApp.Selection.Range.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False
WDApp.Selection.MoveEnd wdStory
WDApp.Selection.Move
Next
Next
WDDoc.SaveAs rootpath & "\" & poleid & " Summary.pdf", wdFormatPDF
WDApp.Quit wdDoNotSaveChanges
Set WDDoc = Nothing
Set WDApp = Nothing
End Sub
Any help would be appreciated.
Not entirely sure about this one, but give it a go:
WDApp.Selection.Range.Select
WDApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False