Excel til word
Ønsker lidt hjælp til at komme videre...Jeg har nedenstående kode som jeg forsøger med.
Det jeg kan oplyse er jeg har i cellerne a1:A200 og celle $b$1,$c$1 fast tekst - som jeg gerne ville have overført således:
Word skal oprette tabel med valget fra inputboksen og indsætte bookmarks og excel skal udfylde disse bookmarks.
Sub test()
Dim ObjWord As Object, WrdDoc As Object
On Error GoTo Erfix
Set ObjWord = CreateObject("Word.Application")
Set WrdDoc = ObjWord.Documents.Open(Filename:="C:\test.docx")
Dim x As Integer
Dim i As Integer
Dim y As Integer
Dim antal As Integer
Dim valg As Integer
x = InputBox("Indtast antal spørgsmål?")
antal = x
Randomize
valg = WorksheetFunction.Floor(Rnd * antal, 1) + 1
i = 1
For i = 1 To antal
WrdDoc.Bookmarks("spm" & i).Range.Text = Range("a" & valg).Value
Next i
WrdDoc.Bookmarks("spm101").Range.Text = Cells(1, 2)
WrdDoc.Bookmarks("spm102").Range.Text = Cells(1, 3)
WrdDoc.Save
WrdDoc.Close savechanges:=True
Set WrdDoc = Nothing
ObjWord.Quit
Set ObjWord = Nothing
MsgBox "Finished"
Exit Sub
Erfix:
On Error GoTo 0
MsgBox "error"
Set WrdDoc = Nothing
ObjWord.Quit
Set ObjWord = Nothing
End Sub
Således at jeg får en tabel med 3 kolonner og x antal rækker i word.
En makro fra word hvor jeg forsøger at finde ud af hvordan det ser ud.
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=8, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Tabel - Gitter" Then
.Style = "Tabel - Gitter"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="spm1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCell
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="spm101"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Er der nogen som har forsøgt sig med dette før vil jeg meget gerne have lidt hjælp?
