Save a word doc as .jpg from Excel macro?

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

Is it possible to save a word docx as .jpg file from Excel?

Or .pdf?

Regards
Espen
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What version of Office do you use?
 
Upvote 0
Then, this code is an example to save as PDF. The code needs to be in Word VBA:

Code:
Sub SaveAsPDF()

    ActiveDocument.ExportAsFixedFormat "C:\MyFile.PDF", wdExportFormatPDF

End Sub

You can also do this from Excel VBA, the same ExportAsFixedFormat method needs to be used, but the reference to the document to be saved (here: ActiveDocument) will need to be done differently. There will be no ActiveDocument in the Excel VBA.
 
Upvote 0
Great, thats very short and presice.

How can I run the macro in Word from a excel macro, is it just an ordinary

Call SaveAsPDF

?

Or do you know how I rewrite the ActiveDocument, in my case it's named Intro.docx

Regards
Espen
 
Upvote 0
Code:
Option Explicit
 'the document
Dim Inv_doc As Object
 'the application
Dim WD As Object
Dim FName As String
Dim DesktopB As String
Option Base 1
Sub AutoNameEdit()
    Const wdReplaceAll = 2
    Dim objSelection
    Dim WDarray As Variant
    Dim WDcnt As Long, myCnt As Long, i As Long
    Dim cmdPrice_Click As Long
    i = 1
    WDarray = Array("txtCompName", "txtCompNo", "txtCompRef", "txtRefTitle", _
    "txtCompName", "txtStorage", "txtSpecial", "txtCompRef", "txtSafeRef", "txtStreet", "txtStreetNo", "txtPostNo", "txtCity")
    FName = ActiveWorkbook.Sheets("Ark3").RAnge("B1").Value
    DesktopB = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    
    Dim which_document As String
    which_document = DesktopB & "\SalesTools\Intro.docx"
    
             'need an instance of word
    Set WD = CreateObject("Word.Application") 'Set objWord = CreateObject("Word.Application")
    WD.Visible = True
    Set Inv_doc = WD.Documents.Open(which_document) 'Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
    Set objSelection = WD.Selection
    For myCnt = 1 To UBound(WDarray)
        objSelection.Find.Text = WDarray(myCnt)
        objSelection.Find.Forward = True
        objSelection.Find.MatchWholeWord = True
        If objSelection.Find.Execute Then
            objSelection.Find.Replacement.Text = Sheets("Ark3").Cells(i, 2).Text
            objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
        End If
        i = i + 1
    Next

    WD.Activate
    Inv_doc.SaveAs DesktopB & "\SalesTools\" & FName & " - " & Date & ".docx"
    Inv_doc.Close
    WD.Quit
    Set Inv_doc = Nothing
    Set WD = Nothing
    
End Sub
 
Upvote 0
This code invokes Word from Excel, so yes, with similar code you can also save a Word doc as PDF.
 
Upvote 0
I have tried this but I get error msg:

Invalid procedure or call argument.


Code:
Option Explicit
 'the document
Dim Inv_doc As Object
 'the application
Dim WD As Object
Dim FName As String
Dim DesktopB As String
Option Base 1
Sub AutoNameEdit()
    Const wdReplaceAll = 2
    Dim objSelection
    Dim WDarray As Variant
    Dim WDcnt As Long, myCnt As Long, i As Long
    Dim cmdPrice_Click As Long
    Const wdExportFormatPDF = 2
    i = 1
    WDarray = Array("txtCompName", "txtCompNo", "txtCompRef", "txtRefTitle", _
    "txtCompName", "txtStorage", "txtSpecial", "txtCompRef", "txtSafeRef", "txtStreet", "txtStreetNo", "txtPostNo", "txtCity")
    FName = ActiveWorkbook.Sheets("Ark3").RAnge("B1").Value
    DesktopB = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    
    Dim which_document As String
    which_document = DesktopB & "\SalesTools\Intro.docx"
    
             'need an instance of word
    Set WD = CreateObject("Word.Application") 'Set objWord = CreateObject("Word.Application")
    WD.Visible = True
    Set Inv_doc = WD.Documents.Open(which_document) 'Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
    Set objSelection = WD.Selection
    For myCnt = 1 To UBound(WDarray)
        objSelection.Find.Text = WDarray(myCnt)
        objSelection.Find.Forward = True
        objSelection.Find.MatchWholeWord = True
        If objSelection.Find.Execute Then
            objSelection.Find.Replacement.Text = Sheets("Ark3").Cells(i, 2).Text
            objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
        End If
        i = i + 1
    Next

    WD.Activate
    Inv_doc.ExportAsFixedFormat DesktopB & "\SalesTools\" & FName & " - " & Date & ".PDF", wdExportFormatPDF
    'Inv_doc.SaveAs DesktopB & "\SalesTools\" & FName & " - " & Date & ".docx"
    Inv_doc.Close
    WD.Quit
    Set Inv_doc = Nothing
    Set WD = Nothing
    
End Sub
 
Upvote 0
What happens if you substitute wdExportFormatPDF for the number 17?
 
Upvote 0

Forum statistics

Threads
1,222,532
Messages
6,166,587
Members
452,055
Latest member
ibale

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