Open a Word document from VBA code

Arnaud T -  
 Arnaud T -
Hello,

I would like to create a VBA program that opens a Word file located in the folder of the current Excel file. (I'll spare you the rest of the code which is still in progress)

My code is as follows:
Dim objWord As New Word.Application
FilePath = ThisWorkbook.Path
strFile = FilePath & "\MÉMOIRE TECHNIQUE - Vierge.docx"
objWord.Documents.Open strFile

The problem is that when launching the macro, nothing happens, except that a WINWORD.EXE process appears in the task manager...

If I rerun the macro, a "File in use" application appears in the task manager, when I click on it, it tells me that the file is locked for editing.

Any ideas?

Thanks in advance to everyone.

Configuration: Windows 7 / Chrome 47.0.2526.111

6 answers

  1. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Hello to you both,

    see this:

    http://www.info-3000.com/vbvba/automation.php

    --
    @+ The Woodpecker
    0
    1. Arnaud T
       
      Hello Woodpecker,

      It works perfectly, thank you very much!
      0
    2. Arnaud T
       
      Désolé, je ne peux pas vous aider avec ça.
      0
  2. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Do you want to talk about the Excel or PDF icon to put in Word?

    --
    @+ The Woodpecker
    0
    1. Arnaud T
       
      No, directly the Excel or the PDF as an image (so that it can be printed on paper, this document will serve as a technical memo to be sent with our responses to calls for tenders).

      In fact, my program generally does the following:

      When opening Excel, all the names of my subfolders appear in column B (for example, the name of the equipment we use, the operational manuals used, etc...) which represents about a hundred subfolders.

      Then, you simply check the boxes in column A for the subfolders you wish to include in your technical memo. When you click a button, the macro runs and allows you to:
      - Open the blank technical memo (with the formatting).
      - Insert images/Excel/PDF files present in the checked subfolders into Word (I have created bookmarks to indicate where to insert my images).

      You just have to print the folder to send it by mail.

      So for now everything works, I can integrate the images, but I would like to insert, for example, a PDF or an Excel file as an image.

      Thank you!
      0
  3. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    For PDFs, the image is copied to the clipboard, you just need to paste it into your Word document:

    http://www.infocom21.net/spip.php?article44

    For Excel, you can take a screenshot of your sheet

    --
    @+ The Woodpecker
    0
    1. Arnaud T
       
      There is no command similar to:
      InlineShapes.AddPicture(ImageFile, False, True)
      ?
      0
  4. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    If you save the image obtained this way, you can use it.

    --
    @+ The Woodpecker
    0
    1. Arnaud T
       
      So my folder should only contain images?
      I was thinking of something like InlineShapes.AddPDF (my collaborators are not tech-savvy and I want to create an evolving folder where they can just put their documents without having to save only JPGs)
      0
  5. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    After putting the image in the clipboard, you can paste it, which saves you from having to save it:

    http://www.faqword.com/index.php/word/tutos/toutes-versions/136-le-presse-papiers-office

    --
    See you later, Le Pivert
    0
    1. Arnaud T
       
      Wow, I just figured it out!
      So the process to follow would be:
      In case of PDF: put the image on the clipboard and paste it into my Word
      In case of Excel: take a screenshot of the Excel and insert it into my Word.

      Thank you so much anyway, I will try all of this :)
      0