繁体   English   中英

从Excel VBA转到Word文档中的特定行

[英]Going to a specific line in a word doc from excel VBA

我正在写一个宏,它将宏从excel文件粘贴到word中。

Worksheets(worksheetname).Activate
ActiveSheet.ChartObjects(chartname).Activate
Selection.Cut
mydoc.Activate

与工作表名称等正确声明和定义。

我不知道如何在excel VBA中引用Word文档中的一行。 我在文档中引用了一行,可以将其粘贴。

编辑:为澄清起见,我可以在文字中粘贴一个段落。 我希望指定的位置比段落的开头更好:或者在特定的行中,或者指定段落的结尾。 /编辑

我唯一的想法是:

Rng = mydoc.Range(doc.Paragraphs(1).Start, mydoc.Paragraphs(1).End - 1).paste

但我认为这是文字样式的VBA,似乎不起作用。

干杯!

这样行吗?

Dim r As Range
Set r = d.Paragraphs(i).Range
r.Start = r.End
r.Paste

我找到了自己的解决方案,我认为效果很好。

实际上,它会在文档中查找第一个单词,然后向下查找所需行数。

Sub LineSelection()

    Dim WordApp As Object

    Set WordApp = GetObject(, "Word.Application")
    WordApp.Visible = True

    ActiveDocument.Content.Select

    With Word.Selection.Find
        .ClearFormatting
        .Text = "[Type very first word of the Word Document here]"
    End With

    If Word.Selection.Find.Execute Then
        Word.Selection.Select
        Word.Selection.Goto What:=wdGoToLine, Which:=wdGoToNext, Count:=3
        ''instead of [3] type the line you want to go to       >>>     ^^^
    End If

End Sub

希望该解决方案对您有所帮助。

很抱歉,我可能迟到2.5年了;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM