Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1506 connectés 

  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  [Resolu] Word 2010, ouvrir un fichier .doc et l'enregistrer en .pdf

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Resolu] Word 2010, ouvrir un fichier .doc et l'enregistrer en .pdf

n°2132785
Mathew17
steam : matosman
Posté le 25-03-2012 à 12:49:44  profilanswer
 

Bonjour à tous,
 
Avec Word 2010, on peut enregistrer les documents en pdf.
 
J'aimerais créer un script .vbs qui ouvre un fichier .doc pour l'enregistrer en pdf.
 
voici la base (qui ne marche pas, bien entendu)  :
 

Code :
  1. Path = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, WScript.ScriptName)-1)
  2. Const wdGoToBookmark = -1
  3. Const wdDoNotSaveChanges = 0
  4. Const ForReading = 1, ForWriting = 2
  5. Const bWaitOnReturn = true
  6. Dim WshShell, wordApp, wordDoc, wordRange, fso, f
  7. Set wordApp = CreateObject("Word.Application" )
  8. wordApp.Visible = False
  9. Set wordDoc = wordApp.Documents.Open(path & "Vide.doc" )
  10. 'enregistrement
  11. wordDoc.SaveAs(path & "test.pdf" )
  12. wordApp.Quit


 
Avec word 2010, j'ai fait une macro pour enregistrer un document en .pdf et voici le résultat :
 

Code :
  1. ActiveDocument.ExportAsFixedFormat OutputFileName:="C:\CV\Vide.pdf", _
  2. ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
  3. wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
  4. Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
  5. CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
  6. BitmapMissingFonts:=True, UseISO19005_1:=False


 
J'ai évidemment tester cette macro en .vbs qui n'a pas marcher.
 
Comment traduire cette macro en .Vbs .
 
Merci d'avance


Message édité par Mathew17 le 30-03-2012 à 10:01:34
mood
Publicité
Posté le 25-03-2012 à 12:49:44  profilanswer
 

n°2133213
kiki29
Posté le 27-03-2012 à 13:36:39  profilanswer
 

Salut, trouvé sur le web, pas testé car pas installé Word 2007, à toi de l'adapter
 

Option Explicit
 
Doc2PDF "C:\Documents and Settings\MyUserID\My Documents\resume.doc"
 
Sub Doc2PDF( myFile )
' This subroutine opens a Word document, then saves it as PDF, and closes Word.
' If the PDF file exists, it is overwritten.
' If Word was already active, the subroutine will leave the other document(s)
' alone and close only its "own" document.
'
' Requirements:
' This script requires the "Microsoft Save as PDF or XPS Add-in for 2007
' Microsoft Office programs", available at:
' http://www.microsoft.com/downloads/details.aspx?
'        familyid=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
 
    ' Standard housekeeping
    Dim objDoc, objFile, objFSO, objWord, strFile, strPDF
 
    Const wdFormatDocument                    =  0
    Const wdFormatDocument97                  =  0
    Const wdFormatDocumentDefault             = 16
    Const wdFormatDOSText                     =  4
    Const wdFormatDOSTextLineBreaks           =  5
    Const wdFormatEncodedText                 =  7
    Const wdFormatFilteredHTML                = 10
    Const wdFormatFlatXML                     = 19
    Const wdFormatFlatXMLMacroEnabled         = 20
    Const wdFormatFlatXMLTemplate             = 21
    Const wdFormatFlatXMLTemplateMacroEnabled = 22
    Const wdFormatHTML                        =  8
    Const wdFormatPDF                         = 17
    Const wdFormatRTF                         =  6
    Const wdFormatTemplate                    =  1
    Const wdFormatTemplate97                  =  1
    Const wdFormatText                        =  2
    Const wdFormatTextLineBreaks              =  3
    Const wdFormatUnicodeText                 =  7
    Const wdFormatWebArchive                  =  9
    Const wdFormatXML                         = 11
    Const wdFormatXMLDocument                 = 12
    Const wdFormatXMLDocumentMacroEnabled     = 13
    Const wdFormatXMLTemplate                 = 14
    Const wdFormatXMLTemplateMacroEnabled     = 15
    Const wdFormatXPS                         = 18
    Const wdFormatOfficeDocumentTemplate      = 23  
    Const wdFormatMediaWiki                   = 24  
 
    ' Create a File System object
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
 
    ' Create a Word object
    Set objWord = CreateObject( "Word.Application" )
 
    With objWord
        ' True: make Word visible; False: invisible
        .Visible = True
 
        ' Check if the Word document exists
        If objFSO.FileExists( myFile ) Then
            Set objFile = objFSO.GetFile( myFile )
            strFile = objFile.Path
        Else
            WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
            ' Close Word
            .Quit
            Exit Sub
        End If
 
        ' Build the fully qualified HTML file name
        strPDF = objFSO.BuildPath( objFile.ParentFolder, _
                 objFSO.GetBaseName( objFile ) & ".pdf" )
 
        ' Open the Word document
        .Documents.Open strFile
 
        ' Make the opened file the active document
        Set objDoc = .ActiveDocument
 
        ' Save as HTML
        objDoc.SaveAs strPDF, wdFormatPDF
 
        ' Close the active document
        objDoc.Close
 
        ' Close Word
        .Quit
    End With
End Sub


Message édité par kiki29 le 27-03-2012 à 13:40:45

---------------
Myanmar 90/91 : http://gadaud.gerard.free.fr/publi [...] index.html
n°2133815
Mathew17
steam : matosman
Posté le 30-03-2012 à 10:00:02  profilanswer
 

Merci pour ta réponse kiki.
 
Une personne m'a donné la réponse sur le forum de microsoft :
 
Voici le script complet :
 

Code :
  1. 'Variable
  2. NomFICHIERPDF="nomfichierpdf"
  3. 'constantes
  4. Const wdExportFormatPDF = 17
  5. Const wdExportOptimizeForPrint = 0
  6. Const wdExportAllDocument = 0
  7. Const wdExportDocumentContent = 0
  8. Const wdExportCreateNoBookmarks = 0
  9. Path = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, WScript.ScriptName)-1)
  10. Const wdGoToBookmark = -1
  11. Const wdDoNotSaveChanges = 0
  12. Const ForReading = 1, ForWriting = 2
  13. Const bWaitOnReturn = true
  14. Dim WshShell, wordApp, wordDoc, wordRange, fso, f
  15. Set wordApp = CreateObject("Word.Application" )
  16. wordApp.Visible = False
  17. Set wordDoc = wordApp.Documents.Open(path & "Vide.doc" )
  18. 'enregistrement
  19. wordDoc.SaveAs(path & "test.doc" )
  20. 'sauvegarde en pdf
  21. strDocPDF = path & NomFICHIERPDF & ".pdf"
  22. wordDoc.ExportAsFixedFormat strDocPDF, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument, 1, 1, wdExportDocumentContent, True, True, wdExportCreateNoBookmarks, True, True, False
  23. wordApp.Quit
  24. 'fermer processus word
  25. strComputer = "."
  26. Set objWMIService = GetObject("winmgmts:" _
  27.     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" )
  28. Set colProcessList = objWMIService.ExecQuery _
  29.     ("Select * from Win32_Process Where Name = 'winword.exe'" )
  30. For Each objProcess in colProcessList
  31.     objProcess.Terminate()
  32. Next
  33. 'fenetre ok
  34. wscript.echo "Enregistrement fichier :" & CHR(10) & " " & CHR(10) & path & NomFICHIERPDF & ".pdf"
  35. 'suppression fichier.txt
  36. fso.DeleteFile(path & "test.doc" )


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  [Resolu] Word 2010, ouvrir un fichier .doc et l'enregistrer en .pdf

 

Sujets relatifs
Allocation fichier en entrée / sortieProbleme pour compiler un fichier java
couleur de cellule tableau automatique fichier odt phpcomment au mieux effectuer une recherche dans un fichier XML?
Découpe fichier Word toute les X pagesHelp comparaison de deux fichier perl
Comment peut-on lire un fichier pdf en php?[Résolu]Copie d'une cellule d'un fichier à l'autre
Plus de sujets relatifs à : [Resolu] Word 2010, ouvrir un fichier .doc et l'enregistrer en .pdf


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR