Avatar billede Falentin Seniormester
28. august 2018 - 15:06 Der er 1 kommentar og
1 løsning

Får output i aktivt dokument istedet for "immediate window"

Jeg har følgende script, der virker perfekt, bortset fra, at jeg gerne vil outputtet i det aktive Word dokument. Nogen, der kan hjælpe?

Sub Test7()

Dim Mydir As String
Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function -

Dim mypath As String
mypath = ActiveDocument.Path

MyFile = Dir$(mypath & "\*.*")
Do While MyFile <> ""
    DirectoryListArray(Counter) = MyFile
    MyFile = Dir$
    Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
'To prove it worked you could run the following:
For Counter = 0 To UBound(DirectoryListArray)
    'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
   
    Debug.Print DirectoryListArray(Counter)
Next Counter
End Sub
Avatar billede Lene Fredborg Ekspert
28. august 2018 - 15:31 #1
Hvis du erstatter koden:

For Counter = 0 To UBound(DirectoryListArray)
    'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
   
    Debug.Print DirectoryListArray(Counter)
Next Counter


…med følgende:

    With ActiveDocument
        'insert empty paragraph at end of active document
        .Range.InsertAfter vbCr
        'set style to Normal
        .Paragraphs.Last.Style = "Normal"
       
        'iterate through array and insert a paragraph per item at end of document
        For Counter = LBound(DirectoryListArray) To UBound(DirectoryListArray)
            .Range.InsertAfter DirectoryListArray(Counter) & vbCr
        Next Counter
    End With

…vil resultatet blive indsat sidst i det aktive dokument, et afsnit for hver, formateret med typografien Normal.
Avatar billede Falentin Seniormester
28. august 2018 - 15:39 #2
Wow,, mange tak :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester