• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

add content in word textarea with vba in excel

Hi,

I have an excel file where I would be inputting client informations.
In vba I have some code to actually print selected word documents (in a listbox) to pdf.

Before printing it to pdf I would like the inputs of the client to be pasted in the textarea situated at the top of the .doc page.

How is that possible? I found some hints on the web I should be using bookmarks. But how?
0
Mutsop
Asked:
Mutsop
  • 5
  • 4
1 Solution
 
ExcelGuideConsultantCommented:
Create a bookmark in your Word file (google for it, example: http://www.computorcompanion.com/LPMArticle.asp?ID=320)  and then use something like this (see code snippet, where a chart is code pasted in Word):

Dim bMark As Bookmark
If Not IsNull(ActiveChart) Then
    'move the cursor to the correct location
    WDApp.Selection.GoTo What:=wdGoToBookmark, Name:="myMark"
    'Paste chart at cursor position
    WDApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, Placement:=wdInLine, DisplayAsIcon:=False
End If

Open in new window

0
 
ExcelGuideConsultantCommented:
I mean copy/pasted in stead of code pasted...lol
0
 
MutsopAuthor Commented:
Ok so I tried inputting it but it says it can't find the bookmark.

So in excel (in the print function) I have the code below.

The moment excel vba opens up the .doc file after a few seconds it gives me the error it can't seem to find the bookmark. Although the Name:="KlantGegevens" is correct.
wdo.ActivePrinter = "PDFcreator"
 
  ' Open the Word document
  Set wdoc = wdocs.Open(sDocFile)
    wdo.Selection.GoTo What:=wdGoToBookmark, Name:="KlantGegevens"
    'Paste chart at cursor position
    wdo.Selection.Paste

  wdo.ActiveDocument.PrintOut False, , , sTempFile

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ExcelGuideConsultantCommented:
Hey puntmuts ;) geintje ;)

I just tested the code and it works fine by me, although your code looks different, see code snippet.


Dim wdApp As Word.Application, wdDoc As Word.Document

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

Set wdDoc = wdApp.Documents.Open("C:\blabla.doc")

wdApp.Visible = True

    wdApp.Selection.GoTo What:=wdGoToBookmark, Name:="KlantGegevens"
    'Nothing to paste, so I tested this with just copy a piece of text and then run this macro
    wdApp.Selection.Paste
 

Open in new window

0
 
MutsopAuthor Commented:
hehe geen probleem :)

Well your code seems to work after I actually included a reference to "Microsoft Word 11.0 Object library".
BUT Apparently it opens two word documents: one empty and one with the pasted text.

And now for the final version of my script :D
Could you check the complete code of the function and see what I'm doing wrong?
Function DOC2PDF(sDocFile, sPDFFile)
  Dim fso ' As FileSystemObject
  Dim wdo ' As Word.Application
  Dim wdoc ' As Word.Document
  Dim wdocs ' As Word.Documents
  Dim sPrevPrinter ' As String
 
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set wdo = CreateObject("Word.Application")
  wdo.Visible = True
  
  
  Set wdocs = wdo.Documents
 
  sTempFile = fso.GetSpecialFolder(TemporaryFolder) + "\" + fso.GetTempName()
 
  sDocFile = "Z:\Templates\Formulier\" & sDocFile
 
  sFolder = "Z:\Templates\Formulier\"
 
  If Len(sPDFFile) = 0 Then
    sPDFFile = fso.GetBaseName(sDocFile) + ".pdf"
  End If
 
  If Len(fso.GetParentFolderName(sPDFFile)) = 0 Then
    sPDFFile = sFolder + "\" + sPDFFile
  End If
 
  ' Remember current active printer
  sPrevPrinter = wdo.ActivePrinter
 
  wdo.ActivePrinter = "PDFcreator"
 
  ' Open the Word document
  Set wdoc = wdocs.Open(sDocFile)
  
    wdo.Selection.GoTo What:=wdGoToBookmark, Name:="KlantGegevens"
    'Paste chart at cursor position
    wdo.Selection.Paste

  wdo.ActiveDocument.PrintOut False, , , sTempFile

 
  wdoc.Close WdDoNotSaveChanges
  wdo.ActivePrinter = sPrevPrinter
  wdo.Quit WdDoNotSaveChanges
  Set wdo = Nothing
 
End Function

Open in new window

0
 
MutsopAuthor Commented:
Oh I got it to work...
BUT And yet another BUT!!!!!!

It can't seem to find bookmarks in a textarea ("tekstVak")
It does work when i set the bookmark outside of it
0
 
ExcelGuideConsultantCommented:
ahhh...so would you like to paste your information in the textarea or outside of that...or even both? Because you do not need any bookmarks if you use a textarea. Please let me know.
0
 
MutsopAuthor Commented:
Never mind found the problem :)

I should be using the code below

THanks for your help though...

Regards
wdo.ActiveDocument.Bookmarks("KlantGegevens").Select

Open in new window

0
 
MutsopAuthor Commented:
Merci :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now