Gem flere worddokumenter som pdf med celleværdi som navn
Hej.Jeg har prøvet at lave en kode som åbner et word dokument, kopiere et område fra et excel ark, sætter ind og gemmer som pdf, med navn som værdien i celle ("y18"), og derefter går videre til næste excel ark og gør præcis det samme, dog gemmer med et nyt navn. Det går fint med at få de over i word, men den gemmer ikke. Nogen der kan hjælpe? :) Her er min kode:
Sub toWord()
Dim ws As Worksheet
Dim Wkbk1 As Workbook
Dim wdapp As Object
Dim wddoc As Object
Dim orng As Object
Dim strdocname As String
Dim navn As Range
Dim filnavn As String
Set navn = ActiveSheet.Range("y18")
For Each ws In ActiveWorkbook.Worksheets
Set Wkbk1 = ActiveWorkbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
strdocname = "C:\Users\Morten\Documents\Arbejde\prøve med kode\royalty skabalon.docx"
filnavn = navn.Value
'file name & folder path
On Error Resume Next
'error number 429
Set wdapp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
'create new instance of word application
Set wdapp = CreateObject("Word.Application")
End If
wdapp.Visible = True
'define paths to file
If Dir(strdocname) = "" Then
'MsgBox "The file" & strdocname & vbCrLf & "was not found " & vbCrLf & "C:\Path\Name.doc", _
' vbExclamation, "The document does not exist "
'Exit Sub
Set wddoc = wdapp.Documents.Add
Else
Set wddoc = wdapp.Documents.Open(strdocname)
End If
If ws.Name <> "hovedark" Then
ws.Range("x13:AA44").Copy
Set orng = wddoc.Range
orng.Paste
orng.End = wddoc.Range.End
End If
wddoc.ExportAsFixedFormat OutputFileName:="C:\Users\Morten\Documents\Arbejde\prøve med kode\" & filnavn, ExportFormat:=wdExportFormatPDF
oWord.Quit
Set oWord = Nothing
Next ws
lbl_Exit:
Set orng = Nothing
Set wddoc = Nothing
Set wdapp = Nothing
Set Wkbk1 = Nothing
Set ws = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Exit Sub
End Sub