web-dev-qa-db-ja.com

vbaを使用してWord文書の内容を別のWord文書にコピーする

私はVBを何年も使用していないので、これが明らかであることが判明した場合はご容赦ください。ユーザーフォームを表示するテンプレートで使用するWordvbaマクロを作成しようとしています。次に、ユーザーフォームに応じてfileA.docx、fileB.docx、またはfileC.docxの内容をインポートします(その後、ブックマークを使用してフォームデータを入力しますが、それが適切かどうかはわかりません)。ファイルA、B、およびCには、リストなどの基本的な形式のテキストが含まれますが、特別なものはありません。

私がオンラインで見た解決策は、ファイルの内容を新しいファイルにコピーできますが、理想的には、これらのファイルの1つ全体を、テンプレートから取得している現在名前のない新しいファイルにインポートしたいと思います。問題が発生しているのは、選択範囲をこれらのファイルの1つに切り替えてから、名前のない新しいドキュメントに戻すことだと思いますが、手を使って正しくコピーしていることを確認することもできます。


更新:私は物事を難しくしすぎていましたが、ここでの答えは私を正しい方向に向けさせました(ありがとう!)。結局、私はちょうどしました

ThisDocument.Activate

Selection.InsertFile("fileA")

これにより、必要なものすべての生のダンプが得られます。

3
user3229306

このようなコマンドを使用すると、使用しているドキュメントを切り替えたり、要素をコピーして貼り付けたりできます。

ThisDocument.Activate 'Sets the main document active
Documents("Name.doc").Activate 'Activates another document

コピーコマンドを使用して、ドキュメントの挿入、コピー、貼り付けを行うことができます。

ThisDocument.Range.InsertAfter("String") 'Insert text

Selection.WholeStory 'Select whole document
Selection.Expand wdParagraph 'Expands your selection to current paragraph
Selection.Copy 'Copy your selection
Documents("name.doc").Activate 'Activate the other document
Selection.EndKey wdStory 'Move to end of document
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content

次に、そのようなものに移動してフォーマットするか、以前の元のフォーマットでコピーして貼り付けることができます。

6
Alex D

マクロを記録する...

  1. ソースドキュメントから開始
  2. ctrl-Aを押してすべてを選択します
  3. ctrl-Cを押してクリップボードにコピーします
  4. ターゲットドキュメントに切り替えます
  5. ctrl-vを押してドキュメントに貼り付けます
  6. 録音を停止します

または(Word 2007以降を想定)

  1. ソースドキュメントを閉じた状態でターゲットドキュメントから開始します
  2. リボンで[挿入]> [オブジェクト]> [ファイルからのテキスト...]をクリックします。
  3. ソースドキュメントに移動します
  4. 挿入ボタンをクリックします
  5. 録音を停止します

私は2番目のバージョンが好きなので、最初に置くべきでした

2
casgage

これは、次の理由で組み込む必要のある重要な改善点です(私は思います)。

  1. クリップボードを使用しないため、マクロの実行中にユーザーがクリップボードの内容を変更しても、マクロが脆弱になることはありません。
  2. ファイルを使用しないため、I/Oがなくなり、ファイルシステムのセキュリティや権限などを処理する必要がなくなるため、速度が大幅に向上します。ドキュメントをループする場合は、.InsertFile()を使用しないでください。速度が低下します。自分をダウンさせます。必要な場合にのみ、最後に一度使用してください。以下の例は、.InsertFile()を使用せずに同じ結果を達成する方法を示しています。

アイデアは、1つのソースドキュメントで見つかったテキストの一部を、ソースとは異なる宛先ドキュメントに転送し、ソースのフォーマットを維持することです。

上記を実行するには(コードをスキップしてドキュメントを開く):

For Each oTable In oDoc_Source  
'the above could have been anything that returns a Range object
'such as: ActiveDocument.Content.Find.Execute ....

'...
'logic here to identify the table, or text, you are looking for
'...

'I can't believe the MS Dev Center folks could only think
'of .InsertFile(), which is the last resort I would go for, 
'especially if your code runs on a web server [concurrent web requests]!

'SAFEST
'(no user interference on clipboard possible, no need to deal with file i/o and permissions)
'you need a reference to Document.Content, 
'as the act of obtaining a reference "un-collapses" the range, so the below 3 lines must be in that order.
Set oRange =  oDoc_DestinationDoc.Content
oRange.Collapse Direction:=wdCollapseEnd
oRange.FormattedText = oTable.Range

'BRUTE, AND PRONE TO RANDOM ERRORS AND HANGS DUE TO USER INTERFERENCE WITH CLIPBOARD 
'find a way to implement WIHTOUT using the CLIPBOARD altogether to copy the below range object
'it will be easier for PC users to use the clipboard while the macro runs
'and it will probably be safer for the output of this macro to remain uncorrupted

'oTable.Range.Copy
'Set oRange =  oDoc_DestinationDoc.Content
'oRange.Collapse Direction:=wdCollapseEnd
'oRange.Paste


'THE BELOW DOES NOT WORK
' '1) - cannot add a range from another document
' 'adds only text, not the formats and not the table layout
' oTable.Range.TextRetrievalMode.IncludeFieldCodes = True
' oTable.Range.TextRetrievalMode.IncludeHiddenText = True
'  oDoc_DestinationDoc.Content.InsertAfter oTable.Range
'
' '2) - cannot add a range from another document
'  oDoc_DestinationDoc.Content.Tables.Add oTable.Range, iRowMax, iColMax
' 
' '3) - only puts in plain text, and it replaces the range without the .Collapse call
'  oDoc_DestinationDoc.Content.Text = oTable.Range
1
Fr4nc01s

私は同じことをしていて、他のドキュメントを選択してコピーして貼り付けようとしました。しかし、それは機能しませんでした(おそらく他のアプリケーションがクリップボードを使用していたためにエラーが発生しましたが、よくわかりません)。そこで、少し検索して、Microsoft DevCenterで完璧なソリューションを見つけました。

https://msdn.Microsoft.com/en-us/vba/Word-vba/articles/selection-insertfile-method-Word

Selection.Collapse Direction:=wdCollapseEnd 
Selection.InsertFile FileName:="C:\TEST.DOC"
0
András Kovács