sylvietoin
Gebruiker
- Lid geworden
- 5 feb 2007
- Berichten
- 56
Hallo,
Ik heb een vraag,
Ik wil een bep. range in EXCEL automatisch copieren naar WORD met een bep SJABLOON
met hierin bedrijfslogo en in de koptekst de naam van het bestand.
Wat ik heb gedaan:
WORD opgestart en m.b.v. macro opnemen:
1: nieuw document
2: sjabloon gekozen op harde schijf
3: macro opnemen gestopt
Documents.Add Template: =_ =====> MACRO OPGENOMEN IN WORD
"C:\Documents and Settings\Toin\Application Data\Microsoft\Sjablonen\test-1.dot" _
, NewTemplate:=False, DocumentType:=0
Vervolgens deze code ingevoegd in de VBA van EXCEL.
Macro opent WORD BESTAND, maar zonder SJABLOON.
Foutcode 424 object vereist?
Ik weet niet wat hiermee bedoeld wordt.
Kan ik de gemaakte code in WORD niet 1 op 1 overnemen on EXCEL ?
Voor de volledigheid heb ik de code toegevoegd
Sub Verzendadvies() ====> MACRO OPGENOMEN IN EXCEL
'This routine will export a range to a Word document with sjabloon
'It opens Word, paste the Excel range, stores the Word document and closes the Word application
Dim oWordApp As Object
Dim oWordDoc As Object
Dim oWSH As Object
Dim rExport As Range
Dim sFileName As String, sPath As String
Dim i As Long, i2 As Long
Set rExport = Worksheets("verzendadvies").Range("c9:j51") '<- Change as needed
'Find the path to "C:/8606/VERZENDADVIEZEN"
Set oWSH = CreateObject("WScript.Shell")
sPath = "c:\" & Worksheets("verzendadvies").Range("d29") & "\" & "verzendadviezen\"
'Assemble a filename for the Word document
'In this case use the value that is in the first cell of the exported range and append date
sFileName = "verzendadvies"
'Optional: make sure you have a unique filename
i = 1
While FileExists(sPath & "\" & sFileName & ".doc")
i2 = InStr(1, sFileName & ".doc", "(", vbTextCompare)
If i2 = 0 Then
sFileName = sFileName & "(" & i & ")"
Else
sFileName = Left(sFileName & "verzendadvies.doc", i2) & i & ")"
End If
i = i + 1
Wend
'Create a Word document.
Set oWordApp = CreateObject("Word.Application")
'Make the newly created Word instance visible
oWordApp.Visible = True
'Create a new document ============> TOT HIER MACRO OPGENOMEN IN EXCEL
Set oWordDoc = oWordApp.Documents.Add
Documents.Add Template: =_ ============> CODE UIT WORD INGEVOEGD
"C:\Documents and Settings\Toin\Application Data\Microsoft\Sjablonen\test-1.dot" _
, NewTemplate:=False, DocumentType:=0
'Copy data ============> VERVOLG OPGENOMEN MACRO IN EXCEL
rExport.Copy
'Paste Excel range in Word (will be pasted in a table)
oWordApp.Selection.PasteSpecial Link:=False, Placement:=wdInLine
'Save the Word document
oWordDoc.SaveAs sPath & sFileName, wdFormatDocument
'Close Word Application
oWordApp.Quit SaveChanges:=False
End Sub
'FileExists Function -> geeft TRUE terug als de file bestaat
Function FileExists(fname As String) As Boolean
FileExists = Dir(fname, vbNormal) > Empty
End Function
End Sub
Wie weet raad ?
Toin
Ik heb een vraag,
Ik wil een bep. range in EXCEL automatisch copieren naar WORD met een bep SJABLOON
met hierin bedrijfslogo en in de koptekst de naam van het bestand.
Wat ik heb gedaan:
WORD opgestart en m.b.v. macro opnemen:
1: nieuw document
2: sjabloon gekozen op harde schijf
3: macro opnemen gestopt
Documents.Add Template: =_ =====> MACRO OPGENOMEN IN WORD
"C:\Documents and Settings\Toin\Application Data\Microsoft\Sjablonen\test-1.dot" _
, NewTemplate:=False, DocumentType:=0
Vervolgens deze code ingevoegd in de VBA van EXCEL.
Macro opent WORD BESTAND, maar zonder SJABLOON.
Foutcode 424 object vereist?
Ik weet niet wat hiermee bedoeld wordt.
Kan ik de gemaakte code in WORD niet 1 op 1 overnemen on EXCEL ?
Voor de volledigheid heb ik de code toegevoegd
Sub Verzendadvies() ====> MACRO OPGENOMEN IN EXCEL
'This routine will export a range to a Word document with sjabloon
'It opens Word, paste the Excel range, stores the Word document and closes the Word application
Dim oWordApp As Object
Dim oWordDoc As Object
Dim oWSH As Object
Dim rExport As Range
Dim sFileName As String, sPath As String
Dim i As Long, i2 As Long
Set rExport = Worksheets("verzendadvies").Range("c9:j51") '<- Change as needed
'Find the path to "C:/8606/VERZENDADVIEZEN"
Set oWSH = CreateObject("WScript.Shell")
sPath = "c:\" & Worksheets("verzendadvies").Range("d29") & "\" & "verzendadviezen\"
'Assemble a filename for the Word document
'In this case use the value that is in the first cell of the exported range and append date
sFileName = "verzendadvies"
'Optional: make sure you have a unique filename
i = 1
While FileExists(sPath & "\" & sFileName & ".doc")
i2 = InStr(1, sFileName & ".doc", "(", vbTextCompare)
If i2 = 0 Then
sFileName = sFileName & "(" & i & ")"
Else
sFileName = Left(sFileName & "verzendadvies.doc", i2) & i & ")"
End If
i = i + 1
Wend
'Create a Word document.
Set oWordApp = CreateObject("Word.Application")
'Make the newly created Word instance visible
oWordApp.Visible = True
'Create a new document ============> TOT HIER MACRO OPGENOMEN IN EXCEL
Set oWordDoc = oWordApp.Documents.Add
Documents.Add Template: =_ ============> CODE UIT WORD INGEVOEGD
"C:\Documents and Settings\Toin\Application Data\Microsoft\Sjablonen\test-1.dot" _
, NewTemplate:=False, DocumentType:=0
'Copy data ============> VERVOLG OPGENOMEN MACRO IN EXCEL
rExport.Copy
'Paste Excel range in Word (will be pasted in a table)
oWordApp.Selection.PasteSpecial Link:=False, Placement:=wdInLine
'Save the Word document
oWordDoc.SaveAs sPath & sFileName, wdFormatDocument
'Close Word Application
oWordApp.Quit SaveChanges:=False
End Sub
'FileExists Function -> geeft TRUE terug als de file bestaat
Function FileExists(fname As String) As Boolean
FileExists = Dir(fname, vbNormal) > Empty
End Function
End Sub
Wie weet raad ?
Toin
Laatst bewerkt: