0

Hi the following code is used to export values form Excel into a word document:

 Sub Createrapport()
    Dim WS As Worksheet

    Set WS = Worksheets("Rapport")
    Application.ScreenUpdating = False
        Sheets("Rapport").Visible = True

    Dim UserName As String
    UserName = InputBox(Prompt:="Var vänligen och ange ditt namn nedan:")
    If UserName = vbNullString Then
    Exit Sub
    Else
    WS.Range("I1").Value = UserName
    End If

        Dim wdApp As Object
        Dim wd As Object
        Dim Tbl As Object

        On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
        If Err.Number <> 0 Then
            Set wdApp = CreateObject("Word.Application")
        End If
        On Error GoTo 0

        Sheets("Rapport").Activate
        Set wd = wdApp.Documents.Add
        wdApp.Visible = True

        'sidhuvud
        wdApp.ActiveWindow.ActivePane.View.SeekView = 9
        With wd
            Set Tbl = .tables.Add(wdApp.Selection.Range, 2, 3, wdWord8TableBehavior)
            Tbl.cell(1, 1).Range.Text = WS.Range("K4").Text
            Tbl.cell(1, 2).Range.Text = WS.Range("L4").Text
            Tbl.cell(1, 3).Range.ParagraphFormat.Alignment = 2 'wdAlignParagraphRight
            Tbl.cell(1, 3).Range.Text = WS.Range("I1").Text
            Tbl.cell(2, 1).Range.Text = WS.Range("K5").Text
            Tbl.cell(2, 3).Range.ParagraphFormat.Alignment = 2 'wdAlignParagraphRight
            Tbl.cell(2, 3).Range.Text = WS.Range("M5").Text
        End With
        wdApp.ActiveWindow.ActivePane.View.SeekView = 0

        'sidnummer
        'Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE  ", PreserveFormatting:=True

        '***** copy image from cell H11:M411 in Excel
        Worksheets("Rapport").Range("H11:M41").Copy
        '***** past image at the current position in Word
        wdApp.Selection.Paste

    Set rng = Worksheets("Rapport").Range("A1:E203")

    rng.Copy
       With wd.Range
            .collapse Direction:=0                  'Slutet av dokumentet
            .InsertParagraphAfter                   'Lägg till rad
            .collapse Direction:=0                  'Slutet av dokumentet
            .PasteSpecial False, False, True        'Pasta som Enhanced Metafile
       End With
       Sheets("Rapport").Visible = False
       Application.ScreenUpdating = True


Set newDoc = Documents.Add
Set myTable = _
    newDoc.Tables.Add(Range:=Selection.Range, NumRows:=3, _
    NumColumns:=3)
myTable.Cell(2, 1).SetWidth _
    ColumnWidth:=InchesToPoints(1.5), _
    RulerStyle:=wdAdjustNone

    End Sub

The report obtained then looks like this:

sample picture

How can I make the values fit within the margins of the word document? this is the prt that does it and it is "InchesToPoints(1.5)" which bugs:

Set newDoc = Documents.Add
    Set myTable = _
        newDoc.Tables.Add(Range:=Selection.Range, NumRows:=3, _
        NumColumns:=3)
    myTable.Cell(2, 1).SetWidth _
        ColumnWidth:=InchesToPoints(1.5), _
        RulerStyle:=wdAdjustNone

EDITED CODE

Set newDoc = Documents.Add
        Set myTable = _
    newDoc.Tables.Add(Range:=Selection.Range, NumRows:=3, _
    NumColumns:=3)
'***** Word constant wdPreferredWidthPercent = 2
myTable.PreferredWidthType = 2
myTable.PreferredWidth = 100
    myTable.Cell(2, 1).SetWidth _
        ColumnWidth:=InchesToPoints(1.5), _
        RulerStyle:=wdAdjustNone
3
  • I'm afraid help rewriting my code is not what SO is about. Try to ask specific questions about specific problems. General tips: try debugging your code by stepping through it line by line to see where it goes wrong (using F8), try searching SO or elsewhere, try the macro recorder. Then come back to SO with any problems, error messages etc. Jun 2, 2014 at 9:30
  • That said, the first section in this link should get you started. You set the table width to 100% of the page width. See separate answer. It says Word 2000 but works in newer versions of Word as well. I found it using google and tried it in Word 2010! :) Jun 2, 2014 at 9:40
  • Hi Olle. I will rewrite my above post to as SO prefers it. Thank you.
    – RiMa
    Jun 2, 2014 at 9:47

2 Answers 2

0

Found through the magic of google:

Selection.Tables(1).PreferredWidthType = wdPreferredWidthPercent
Selection.Tables(1).PreferredWidth = 100

Says Word 2000, tried and works in Word 2010.

(In your case you probably have to add wdApp. before the Selection object, and make sure that Selection points to the place in the document where the table is.)

EDIT

My code sample updated to match your updated code:

'***** Word constant wdPreferredWidthPercent = 2
myTable.PreferredWidthType = 2
myTable.PreferredWidth = 100

Place it right after the Set myTable = line.

EDIT 2

The underscore in VBA means that the one code line extends over several lines, so you can't put code in between (if that is what's not working).

Code sample in context:

Set myTable = _
    newDoc.Tables.Add(Range:=Selection.Range, NumRows:=3, _
    NumColumns:=3)
'***** Word constant wdPreferredWidthPercent = 2
myTable.PreferredWidthType = 2
myTable.PreferredWidth = 100

EDIT 3

Set newDoc = wdApp.Documents.Add
Set myTable = _
    newDoc.Tables.Add(Range:=wdApp.Selection.Range, NumRows:=3, _
    NumColumns:=3)

'***** Word constant wdPreferredWidthPercent = 2
myTable.PreferredWidthType = 2
myTable.PreferredWidth = 100

myTable.Cell(2, 1).SetWidth _
    ColumnWidth:=wdApp.InchesToPoints(1.5), _
    RulerStyle:=0 '*****wdAdjustNone
10
  • I try usign the code above but It says "Value outside interval", thank you.
    – RiMa
    Jun 2, 2014 at 10:04
  • @RinorM Try the updated code. You had to make sure Selection.Tables(1) pointed to the right Table object, but since you alreadey have the myTable reference, use that instead. Also, you probably need to replace the constant wdPreferredWidthPercent with its numeric value, since you ware working from Excel using late binding. Jun 2, 2014 at 10:08
  • I am having trouble to place the code right after, Set myTable line,could you assist me with that also? thank you.
    – RiMa
    Jun 2, 2014 at 10:21
  • @RinorM Updated again. Please tell exactly what the problem is, having trouble to place the code right after doesn't say much, which leaves me guessing. Jun 2, 2014 at 10:43
  • Ok sorry, I have edited the code above, but the problem occurs in the line which says ColumnWidth:=InchesToPoints(1.5), _ and it says "Sub or function have not been defined"
    – RiMa
    Jun 2, 2014 at 10:52
0

Try this:

myTable.AutoFitBehavior wdAutoFitWindow

of if you are late binding:

myTable.AutoFitBehavior 2

This will autofit the table within the word document margins.
Is this what you're trying?

2
  • Yes that is what I am trying to do. When i run your sudgestion I get an error message that says : "Object needed, runtimeerror 424". Thank you for your time
    – RiMa
    Jun 2, 2014 at 10:02
  • You need to properly declare your table variable. In your first code, it should be Tbl.AutoFitBehavior 2. mytable should be a table object.
    – L42
    Jun 3, 2014 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.